Subject: vi: Revise memory handling
The macros in mem.h use "{ ... }" for protection of multiple statements,
instead of the safer "do { ... } while (0)" variant. The following diff
changes the macros to use the latter one and fixes a missing semicolon
at a ADD_SPACE_RET() call site, which has been revealed by that change.
The casts in the allocation macros can be removed, because they are a
workaround for systems whos malloc() implementation doesn't return
'void *'. MALLOC_NOMSG and CALLOC_NOMSG do the same as malloc/calloc
(after the casts have been removed), thus they should be removed to not
confuse the casual reader. The MEMMOVE macro is only used by one caller
and the MEMSET macro is never called. Both can be removed (expanding the
MEMMOVE caller in place). Also, the diff features some strlen + malloc +
strlcpy to strdup, and strlen + malloc + manual memcpy + strlcpy to
asprintf conversion.
See the diff below.
cheers,
natano
Index: cl/cl_main.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/cl/cl_main.c,v
retrieving revision 1.22
diff -u -r1.22 cl_main.c
--- cl/cl_main.c 12 Nov 2014 04:28:41 -0000 1.22
+++ cl/cl_main.c 12 Nov 2014 06:36:52 -0000
@@ -151,11 +151,10 @@
name = p + 1;
/* Allocate the global structure. */
- CALLOC_NOMSG(NULL, gp, GS *, 1, sizeof(GS));
+ gp = calloc(1, sizeof(GS));
if (gp == NULL)
perr(name, NULL);
-
gp->progname = name;
return (gp);
}
@@ -171,7 +170,7 @@
int fd;
/* Allocate the CL private structure. */
- CALLOC_NOMSG(NULL, clp, CL_PRIVATE *, 1, sizeof(CL_PRIVATE));
+ clp = calloc(1, sizeof(CL_PRIVATE));
if (clp == NULL)
perr(gp->progname, NULL);
gp->cl_private = clp;
Index: cl/cl_screen.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/cl/cl_screen.c,v
retrieving revision 1.21
diff -u -r1.21 cl_screen.c
--- cl/cl_screen.c 12 Nov 2014 04:28:41 -0000 1.21
+++ cl/cl_screen.c 12 Nov 2014 06:36:52 -0000
@@ -517,7 +517,7 @@
if ((t = tigetstr(name)) != NULL &&
t != (char *)-1 && (len = strlen(t)) != 0) {
- MALLOC_RET(sp, *elementp, char *, len + 1);
+ MALLOC_RET(sp, *elementp, len + 1);
memmove(*elementp, t, len + 1);
}
return (0);
Index: common/api.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/common/api.c,v
retrieving revision 1.16
diff -u -r1.16 api.c
--- common/api.c 12 Nov 2014 04:28:41 -0000 1.16
+++ common/api.c 12 Nov 2014 06:51:36 -0000
@@ -380,7 +380,7 @@
case OPT_0BOOL:
case OPT_1BOOL:
len = strlen(op->name) + 2 + 1;
- MALLOC_RET(sp, *value, char *, len);
+ MALLOC_RET(sp, *value, len);
(void)snprintf(*value, len,
"%s%s", O_ISSET(sp, offset) ? "" : "no", op->name);
if (boolvalue != NULL)
@@ -388,17 +388,16 @@
break;
case OPT_NUM:
len = 20;
- MALLOC_RET(sp, *value, char *, len);
+ MALLOC_RET(sp, *value, len);
(void)snprintf(*value, len, "%lu", (u_long)O_VAL(sp, offset));
break;
case OPT_STR:
if (O_STR(sp, offset) == NULL) {
- MALLOC_RET(sp, *value, char *, 2);
+ MALLOC_RET(sp, *value, 1);
value[0] = '\0';
} else {
len = strlen(O_STR(sp, offset)) + 1;
- MALLOC_RET(sp,
- *value, char *, len);
+ MALLOC_RET(sp, *value, len);
(void)snprintf(*value, len, "%s", O_STR(sp, offset));
}
break;
Index: common/cut.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/common/cut.c,v
retrieving revision 1.13
diff -u -r1.13 cut.c
--- common/cut.c 12 Nov 2014 04:28:41 -0000 1.13
+++ common/cut.c 12 Nov 2014 06:36:52 -0000
@@ -119,7 +119,7 @@
* Otherwise, if it's not an append, free its current contents.
*/
if (cbp == NULL) {
- CALLOC_RET(sp, cbp, CB *, 1, sizeof(CB));
+ CALLOC_RET(sp, cbp, 1, sizeof(CB));
cbp->name = name;
TAILQ_INIT(&cbp->textq);
LIST_INSERT_HEAD(&sp->gp->cutq, cbp, q);
@@ -300,12 +300,12 @@
{
TEXT *tp;
- CALLOC(sp, tp, TEXT *, 1, sizeof(TEXT));
+ CALLOC(sp, tp, 1, sizeof(TEXT));
if (tp == NULL)
return (NULL);
/* ANSI C doesn't define a call to malloc(3) for 0 bytes. */
if ((tp->lb_len = total_len) != 0) {
- MALLOC(sp, tp->lb, CHAR_T *, tp->lb_len);
+ MALLOC(sp, tp->lb, tp->lb_len);
if (tp->lb == NULL) {
free(tp);
return (NULL);
Index: common/exf.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/common/exf.c,v
retrieving revision 1.30
diff -u -r1.30 exf.c
--- common/exf.c 12 Nov 2014 04:28:41 -0000 1.30
+++ common/exf.c 12 Nov 2014 06:36:52 -0000
@@ -86,7 +86,7 @@
}
/* Allocate and initialize the FREF structure. */
- CALLOC(sp, frp, FREF *, 1, sizeof(FREF));
+ CALLOC(sp, frp, 1, sizeof(FREF));
if (frp == NULL)
return (NULL);
@@ -153,7 +153,7 @@
* Default recover mail file fd to -1.
* Set initial EXF flag bits.
*/
- CALLOC_RET(sp, ep, EXF *, 1, sizeof(EXF));
+ CALLOC_RET(sp, ep, 1, sizeof(EXF));
ep->c_lno = ep->c_nlines = OOBLNO;
ep->rcv_fd = ep->fcntl_fd = -1;
F_SET(ep, F_FIRSTMODIFY);
@@ -497,7 +497,7 @@
/* If we found it, build a new pathname and discard the old one. */
if (found) {
- MALLOC_RET(sp, p, char *, len + 1);
+ MALLOC_RET(sp, p, len + 1);
memcpy(p, path, len + 1);
free(frp->name);
frp->name = p;
Index: common/key.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/common/key.c,v
retrieving revision 1.13
diff -u -r1.13 key.c
--- common/key.c 12 Nov 2014 04:28:41 -0000 1.13
+++ common/key.c 12 Nov 2014 06:36:52 -0000
@@ -347,8 +347,8 @@
if (total >= gp->i_nelem && v_event_grow(sp, MAX(total, 64)))
return (1);
if (gp->i_cnt)
- MEMMOVE(gp->i_event + TERM_PUSH_SHIFT + nitems,
- gp->i_event + gp->i_next, gp->i_cnt);
+ memmove(gp->i_event + TERM_PUSH_SHIFT + nitems,
+ gp->i_event + gp->i_next, gp->i_cnt * sizeof(EVENT));
gp->i_next = TERM_PUSH_SHIFT;
/* Put the new items into the queue. */
Index: common/main.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/common/main.c,v
retrieving revision 1.23
diff -u -r1.23 main.c
--- common/main.c 12 Nov 2014 04:28:41 -0000 1.23
+++ common/main.c 12 Nov 2014 06:36:52 -0000
@@ -348,13 +348,11 @@
if (sp->frp != NULL) {
size_t l;
/* Cheat -- we know we have an extra argv slot. */
- l = strlen(sp->frp->name) + 1;
- MALLOC_NOMSG(sp, *--argv, char *, l);
+ *--argv = strdup(sp->frp->name);
if (*argv == NULL) {
v_estr(gp->progname, errno, NULL);
goto err;
}
- (void)strlcpy(*argv, sp->frp->name, l);
}
sp->argv = sp->cargv = argv;
F_SET(sp, SC_ARGNOFREE);
@@ -554,14 +552,8 @@
if (argv[0] == NULL)
goto nomem;
} else {
- p = argv[0];
- len = strlen(argv[0]);
- MALLOC_NOMSG(NULL, argv[0], char *, len + 2);
- if (argv[0] == NULL)
+ if (asprintf(&argv[0], "-c%s", argv[0] + 1) ==
-1)
goto nomem;
- argv[0][0] = '-';
- argv[0][1] = 'c';
- (void)strlcpy(argv[0] + 2, p + 1, len);
}
} else if (argv[0][0] == '-') {
if (argv[0][1] == '\0') {
Index: common/mark.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/common/mark.c,v
retrieving revision 1.9
diff -u -r1.9 mark.c
--- common/mark.c 12 Nov 2014 04:28:41 -0000 1.9
+++ common/mark.c 12 Nov 2014 06:36:52 -0000
@@ -160,7 +160,7 @@
*/
lmp = mark_find(sp, key);
if (lmp == NULL || lmp->name != key) {
- MALLOC_RET(sp, lmt, LMARK *, sizeof(LMARK));
+ MALLOC_RET(sp, lmt, sizeof(LMARK));
if (lmp == NULL) {
LIST_INSERT_HEAD(&sp->ep->marks, lmt, q);
} else
Index: common/mem.h
===================================================================
RCS file: /cvs/src/usr.bin/vi/common/mem.h,v
retrieving revision 1.6
diff -u -r1.6 mem.h
--- common/mem.h 14 Oct 2014 22:23:12 -0000 1.6
+++ common/mem.h 12 Nov 2014 07:39:02 -0000
@@ -14,7 +14,7 @@
/* Increase the size of a malloc'd buffer. Two versions, one that
* returns, one that jumps to an error label.
*/
-#define BINC_GOTO(sp, lp, llen, nlen) {
\
+#define BINC_GOTO(sp, lp, llen, nlen) do {
\
void *L__bincp; \
if ((nlen) > (llen)) { \
if ((L__bincp = binc((sp), (lp), &(llen), (nlen))) \
@@ -26,8 +26,8 @@
*/ \
(lp) = L__bincp; \
} \
-}
-#define BINC_RET(sp, lp, llen, nlen) {
\
+} while (0)
+#define BINC_RET(sp, lp, llen, nlen) do {
\
void *L__bincp; \
if ((nlen) > (llen)) { \
if ((L__bincp = binc((sp), (lp), &(llen), (nlen))) \
@@ -39,14 +39,14 @@
*/ \
(lp) = L__bincp; \
} \
-}
+} while (0)
/*
* Get some temporary space, preferably from the global temporary buffer,
* from a malloc'd buffer otherwise. Two versions, one that returns, one
* that jumps to an error label.
*/
-#define GET_SPACE_GOTO(sp, bp, blen, nlen) {
\
+#define GET_SPACE_GOTO(sp, bp, blen, nlen) do {
\
GS *L__gp = (sp) == NULL ? NULL : (sp)->gp; \
if (L__gp == NULL || F_ISSET(L__gp, G_TMP_INUSE)) { \
(bp) = NULL; \
@@ -58,8 +58,8 @@
(blen) = L__gp->tmp_blen; \
F_SET(L__gp, G_TMP_INUSE); \
} \
-}
-#define GET_SPACE_RET(sp, bp, blen, nlen) {
\
+} while (0)
+#define GET_SPACE_RET(sp, bp, blen, nlen) do {
\
GS *L__gp = (sp) == NULL ? NULL : (sp)->gp; \
if (L__gp == NULL || F_ISSET(L__gp, G_TMP_INUSE)) { \
(bp) = NULL; \
@@ -71,13 +71,13 @@
(blen) = L__gp->tmp_blen; \
F_SET(L__gp, G_TMP_INUSE); \
} \
-}
+} while (0)
/*
* Add space to a GET_SPACE returned buffer. Two versions, one that
* returns, one that jumps to an error label.
*/
-#define ADD_SPACE_GOTO(sp, bp, blen, nlen) {
\
+#define ADD_SPACE_GOTO(sp, bp, blen, nlen) do {
\
GS *L__gp = (sp) == NULL ? NULL : (sp)->gp; \
if (L__gp == NULL || (bp) == L__gp->tmp_bp) { \
F_CLR(L__gp, G_TMP_INUSE); \
@@ -87,8 +87,8 @@
F_SET(L__gp, G_TMP_INUSE); \
} else \
BINC_GOTO((sp), (bp), (blen), (nlen)); \
-}
-#define ADD_SPACE_RET(sp, bp, blen, nlen) {
\
+} while (0)
+#define ADD_SPACE_RET(sp, bp, blen, nlen) do {
\
GS *L__gp = (sp) == NULL ? NULL : (sp)->gp; \
if (L__gp == NULL || (bp) == L__gp->tmp_bp) { \
F_CLR(L__gp, G_TMP_INUSE); \
@@ -98,76 +98,56 @@
F_SET(L__gp, G_TMP_INUSE); \
} else \
BINC_RET((sp), (bp), (blen), (nlen)); \
-}
+} while (0)
/* Free a GET_SPACE returned buffer. */
-#define FREE_SPACE(sp, bp, blen) {
\
+#define FREE_SPACE(sp, bp, blen) do {
\
GS *L__gp = (sp) == NULL ? NULL : (sp)->gp; \
if (L__gp != NULL && (bp) == L__gp->tmp_bp) \
F_CLR(L__gp, G_TMP_INUSE); \
else \
free(bp); \
-}
+} while (0)
/*
- * Malloc a buffer, casting the return pointer. Various versions.
- *
- * !!!
- * The cast should be unnecessary, malloc(3) and friends return void *'s,
- * which is all we need. However, some systems that nvi needs to run on
- * don't do it right yet, resulting in the compiler printing out roughly
- * a million warnings. After awhile, it seemed easier to put the casts
- * in instead of explaining it all the time.
+ * Malloc a buffer. Various versions.
*/
-#define CALLOC(sp, p, cast, nmemb, size) {
\
- if (((p) = (cast)calloc((nmemb), (size))) == NULL) \
+#define CALLOC(sp, p, nmemb, size) do {
\
+ if (((p) = calloc((nmemb), (size))) == NULL) \
msgq((sp), M_SYSERR, NULL); \
-}
-#define CALLOC_GOTO(sp, p, cast, nmemb, size) {
\
- if (((p) = (cast)calloc((nmemb), (size))) == NULL) \
+} while (0)
+#define CALLOC_GOTO(sp, p, nmemb, size) do {
\
+ if (((p) = calloc((nmemb), (size))) == NULL) \
goto alloc_err; \
-}
-#define CALLOC_NOMSG(sp, p, cast, nmemb, size) {
\
- (p) = (cast)calloc((nmemb), (size)); \
-}
-#define CALLOC_RET(sp, p, cast, nmemb, size) {
\
- if (((p) = (cast)calloc((nmemb), (size))) == NULL) { \
+} while (0)
+#define CALLOC_RET(sp, p, nmemb, size) do {
\
+ if (((p) = calloc((nmemb), (size))) == NULL) { \
msgq((sp), M_SYSERR, NULL); \
return (1); \
} \
-}
+} while (0)
-#define MALLOC(sp, p, cast, size) {
\
- if (((p) = (cast)malloc(size)) == NULL) \
+#define MALLOC(sp, p, size) do {
\
+ if (((p) = malloc(size)) == NULL) \
msgq((sp), M_SYSERR, NULL); \
-}
-#define MALLOC_GOTO(sp, p, cast, size) {
\
- if (((p) = (cast)malloc(size)) == NULL) \
+} while (0)
+#define MALLOC_GOTO(sp, p, size) do {
\
+ if (((p) = malloc(size)) == NULL) \
goto alloc_err; \
-}
-#define MALLOC_NOMSG(sp, p, cast, size) {
\
- (p) = (cast)malloc(size); \
-}
-#define MALLOC_RET(sp, p, cast, size) {
\
- if (((p) = (cast)malloc(size)) == NULL) { \
+} while (0)
+#define MALLOC_RET(sp, p, size) do {
\
+ if (((p) = malloc(size)) == NULL) { \
msgq((sp), M_SYSERR, NULL); \
return (1); \
} \
-}
+} while (0)
-#define REALLOC(sp, p, cast, size) {
\
- if (((p) = (cast)(realloc((p), (size)))) == NULL) \
+#define REALLOC(sp, p, size) do {
\
+ if (((p) = (realloc((p), (size)))) == NULL) \
msgq((sp), M_SYSERR, NULL); \
-}
+} while (0)
-#define REALLOCARRAY(sp, p, cast, nelem, size) {
\
- if (((p) = (cast)(reallocarray((p), (nelem), (size)))) == NULL) \
+#define REALLOCARRAY(sp, p, nelem, size) do {
\
+ if (((p) = (reallocarray((p), (nelem), (size)))) == NULL) \
msgq((sp), M_SYSERR, NULL); \
-}
-
-/*
- * Versions of memmove(3) and memset(3) that use the size of the
- * initial pointer to figure out how much memory to manipulate.
- */
-#define MEMMOVE(p, t, len) memmove((p), (t), (len) * sizeof(*(p)))
-#define MEMSET(p, value, len) memset((p), (value), (len) *
sizeof(*(p)))
+} while (0)
Index: common/screen.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/common/screen.c,v
retrieving revision 1.11
diff -u -r1.11 screen.c
--- common/screen.c 12 Nov 2014 04:28:41 -0000 1.11
+++ common/screen.c 12 Nov 2014 07:20:51 -0000
@@ -39,7 +39,7 @@
size_t len;
*spp = NULL;
- CALLOC_RET(orig, sp, SCR *, 1, sizeof(SCR));
+ CALLOC_RET(orig, sp, 1, sizeof(SCR));
*spp = sp;
/* INITIALIZED AT SCREEN CREATE. */
@@ -84,16 +84,16 @@
goto mem;
sp->subre_len = orig->subre_len;
if (orig->repl != NULL && (sp->repl =
- v_strdup(sp, orig->repl, orig->repl_len)) == NULL)
- goto mem;
+ v_strdup(sp, orig->repl, orig->repl_len)) == NULL) {
+mem: msgq(orig, M_SYSERR, NULL);
+ goto err;
+ }
sp->repl_len = orig->repl_len;
if (orig->newl_len) {
len = orig->newl_len * sizeof(size_t);
- MALLOC(sp, sp->newl, size_t *, len);
- if (sp->newl == NULL) {
-mem: msgq(orig, M_SYSERR, NULL);
+ MALLOC(sp, sp->newl, len);
+ if (sp->newl == NULL)
goto err;
- }
sp->newl_len = orig->newl_len;
sp->newl_cnt = orig->newl_cnt;
memcpy(sp->newl, orig->newl, len);
Index: common/seq.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/common/seq.c,v
retrieving revision 1.9
diff -u -r1.9 seq.c
--- common/seq.c 12 Nov 2014 04:28:41 -0000 1.9
+++ common/seq.c 12 Nov 2014 06:36:52 -0000
@@ -65,7 +65,7 @@
}
/* Allocate and initialize SEQ structure. */
- CALLOC(sp, qp, SEQ *, 1, sizeof(SEQ));
+ CALLOC(sp, qp, 1, sizeof(SEQ));
if (qp == NULL) {
sv_errno = errno;
goto mem1;
Index: common/util.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/common/util.c,v
retrieving revision 1.9
diff -u -r1.9 util.c
--- common/util.c 12 Nov 2014 04:28:41 -0000 1.9
+++ common/util.c 12 Nov 2014 06:36:52 -0000
@@ -40,7 +40,7 @@
return (bp);
csize = *bsizep + MAX(min, 256);
- REALLOC(sp, bp, void *, csize);
+ REALLOC(sp, bp, csize);
if (bp == NULL) {
/*
@@ -121,7 +121,7 @@
{
CHAR_T *copy;
- MALLOC(sp, copy, CHAR_T *, len + 1);
+ MALLOC(sp, copy, len + 1);
if (copy == NULL)
return (NULL);
memcpy(copy, str, len * sizeof(CHAR_T));
Index: ex/ex_args.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/ex/ex_args.c,v
retrieving revision 1.10
diff -u -r1.10 ex_args.c
--- ex/ex_args.c 12 Nov 2014 04:28:41 -0000 1.10
+++ ex/ex_args.c 12 Nov 2014 06:36:52 -0000
@@ -81,8 +81,7 @@
sp->cargv = NULL;
/* Create a new list. */
- CALLOC_RET(sp,
- sp->argv, char **, cmdp->argc + 1, sizeof(char *));
+ CALLOC_RET(sp, sp->argv, cmdp->argc + 1, sizeof(char *));
for (ap = sp->argv,
argv = cmdp->argv; argv[0]->len != 0; ++ap, ++argv)
if ((*ap =
@@ -292,7 +291,7 @@
char **ap, **s_argv;
argc = cmdp == NULL ? 1 : cmdp->argc;
- CALLOC(sp, s_argv, char **, argc + 1, sizeof(char *));
+ CALLOC(sp, s_argv, argc + 1, sizeof(char *));
if ((ap = s_argv) == NULL)
return (NULL);
Index: ex/ex_argv.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/ex/ex_argv.c,v
retrieving revision 1.15
diff -u -r1.15 ex_argv.c
--- ex/ex_argv.c 12 Nov 2014 04:28:41 -0000 1.15
+++ ex/ex_argv.c 12 Nov 2014 06:36:52 -0000
@@ -404,7 +404,7 @@
off = exp->argsoff;
if (exp->argscnt == 0 || off + 2 >= exp->argscnt - 1) {
cnt = exp->argscnt + INCREMENT;
- REALLOCARRAY(sp, exp->args, ARGS **, cnt, sizeof(ARGS *));
+ REALLOCARRAY(sp, exp->args, cnt, sizeof(ARGS *));
if (exp->args == NULL) {
(void)argv_free(sp);
goto mem;
@@ -415,7 +415,7 @@
/* First argument. */
if (exp->args[off] == NULL) {
- CALLOC(sp, exp->args[off], ARGS *, 1, sizeof(ARGS));
+ CALLOC(sp, exp->args[off], 1, sizeof(ARGS));
if (exp->args[off] == NULL)
goto mem;
}
@@ -425,7 +425,7 @@
ap->len = 0;
if (ap->blen < len + 1) {
ap->blen = len + 1;
- REALLOCARRAY(sp, ap->bp, CHAR_T *, ap->blen, sizeof(CHAR_T));
+ REALLOCARRAY(sp, ap->bp, ap->blen, sizeof(CHAR_T));
if (ap->bp == NULL) {
ap->bp = NULL;
ap->blen = 0;
@@ -438,7 +438,7 @@
/* Second argument. */
if (exp->args[++off] == NULL) {
- CALLOC(sp, exp->args[off], ARGS *, 1, sizeof(ARGS));
+ CALLOC(sp, exp->args[off], 1, sizeof(ARGS));
if (exp->args[off] == NULL)
goto mem;
}
Index: ex/ex_at.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/ex/ex_at.c,v
retrieving revision 1.11
diff -u -r1.11 ex_at.c
--- ex/ex_at.c 12 Nov 2014 04:28:41 -0000 1.11
+++ ex/ex_at.c 12 Nov 2014 06:36:52 -0000
@@ -79,9 +79,9 @@
* the range, continue to execute after a file/screen switch, which
* means @ buffers are still useful in a multi-screen environment.
*/
- CALLOC_RET(sp, ecp, EXCMD *, 1, sizeof(EXCMD));
+ CALLOC_RET(sp, ecp, 1, sizeof(EXCMD));
TAILQ_INIT(&ecp->rq);
- CALLOC_RET(sp, rp, RANGE *, 1, sizeof(RANGE));
+ CALLOC_RET(sp, rp, 1, sizeof(RANGE));
rp->start = cmdp->addr1.lno;
if (F_ISSET(cmdp, E_ADDR_DEF)) {
rp->stop = rp->start;
@@ -105,7 +105,7 @@
len += tp->len + 1;
}
- MALLOC_RET(sp, ecp->cp, char *, len * 2);
+ MALLOC_RET(sp, ecp->cp, len * 2);
ecp->o_cp = ecp->cp;
ecp->o_clen = len;
ecp->cp[len] = '\0';
Index: ex/ex_cscope.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/ex/ex_cscope.c,v
retrieving revision 1.21
diff -u -r1.21 ex_cscope.c
--- ex/ex_cscope.c 12 Nov 2014 04:28:41 -0000 1.21
+++ ex/ex_cscope.c 12 Nov 2014 06:36:52 -0000
@@ -242,7 +242,7 @@
/* Allocate a cscope connection structure and initialize its fields. */
len = strlen(dname);
- CALLOC_RET(sp, csc, CSC *, 1, sizeof(CSC) + len);
+ CALLOC_RET(sp, csc, 1, sizeof(CSC) + len);
csc->dname = csc->buf;
csc->dlen = len;
memcpy(csc->dname, dname, len);
@@ -302,7 +302,7 @@
if (stat(buf, &sb) == 0) {
/* Read in the CSCOPE_PATHS file. */
len = sb.st_size;
- MALLOC_RET(sp, csc->pbuf, char *, len + 1);
+ MALLOC_RET(sp, csc->pbuf, len + 1);
if ((fd = open(buf, O_RDONLY, 0)) < 0 ||
read(fd, csc->pbuf, len) != len) {
msgq_str(sp, M_SYSERR, buf, "%s");
@@ -319,8 +319,7 @@
++nentries;
/* Build an array of pointers to the paths. */
- CALLOC_GOTO(sp,
- csc->paths, char **, nentries + 1, sizeof(char **));
+ CALLOC_GOTO(sp, csc->paths, nentries + 1, sizeof(char **));
for (pathp = csc->paths, p = strtok(csc->pbuf, ":");
p != NULL; p = strtok(NULL, ":"))
*pathp++ = p;
@@ -335,7 +334,7 @@
msgq(sp, M_SYSERR, NULL);
return (1);
}
- CALLOC_GOTO(sp, csc->paths, char **, 2, sizeof(char *));
+ CALLOC_GOTO(sp, csc->paths, 2, sizeof(char *));
csc->paths[0] = csc->pbuf;
return (0);
@@ -446,11 +445,11 @@
rtqp = NULL;
if (TAILQ_EMPTY(&exp->tq)) {
/* Initialize the `local context' tag queue structure. */
- CALLOC_GOTO(sp, rtqp, TAGQ *, 1, sizeof(TAGQ));
+ CALLOC_GOTO(sp, rtqp, 1, sizeof(TAGQ));
TAILQ_INIT(&rtqp->tagq);
/* Initialize and link in its tag structure. */
- CALLOC_GOTO(sp, rtp, TAG *, 1, sizeof(TAG));
+ CALLOC_GOTO(sp, rtp, 1, sizeof(TAG));
TAILQ_INSERT_HEAD(&rtqp->tagq, rtp, q);
rtqp->current = rtp;
}
@@ -613,7 +612,7 @@
tlen = strlen(p);
/* Allocate and initialize the TAGQ structure. */
- CALLOC(sp, tqp, TAGQ *, 1, sizeof(TAGQ) + tlen + 3);
+ CALLOC(sp, tqp, 1, sizeof(TAGQ) + tlen + 3);
if (tqp == NULL)
return (NULL);
TAILQ_INIT(&tqp->tagq);
@@ -715,7 +714,7 @@
* length cscope information that follows it.
*/
CALLOC_RET(sp, tp,
- TAG *, 1, sizeof(TAG) + dlen + 2 + nlen + 1 + slen + 1);
+ 1, sizeof(TAG) + dlen + 2 + nlen + 1 + slen + 1);
tp->fname = tp->buf;
if (dlen != 0) {
memcpy(tp->fname, dname, dlen);
Index: ex/ex_global.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/ex/ex_global.c,v
retrieving revision 1.13
diff -u -r1.13 ex_global.c
--- ex/ex_global.c 12 Nov 2014 04:28:41 -0000 1.13
+++ ex/ex_global.c 12 Nov 2014 06:36:52 -0000
@@ -155,7 +155,7 @@
return (1);
/* Get an EXCMD structure. */
- CALLOC_RET(sp, ecp, EXCMD *, 1, sizeof(EXCMD));
+ CALLOC_RET(sp, ecp, 1, sizeof(EXCMD));
TAILQ_INIT(&ecp->rq);
/*
@@ -170,7 +170,7 @@
len = 1;
}
- MALLOC_RET(sp, ecp->cp, char *, len * 2);
+ CALLOC_RET(sp, ecp->cp, 2, len);
ecp->o_cp = ecp->cp;
ecp->o_clen = len;
memcpy(ecp->cp + len, p, len);
@@ -231,7 +231,7 @@
}
/* Allocate a new range, and append it to the list. */
- CALLOC(sp, rp, RANGE *, 1, sizeof(RANGE));
+ CALLOC(sp, rp, 1, sizeof(RANGE));
if (rp == NULL)
return (1);
rp->start = rp->stop = start;
@@ -297,7 +297,7 @@
free(rp);
}
} else {
- CALLOC_RET(sp, nrp, RANGE *, 1, sizeof(RANGE));
+ CALLOC_RET(sp, nrp, 1, sizeof(RANGE));
nrp->start = lno + 1;
nrp->stop = rp->stop + 1;
rp->stop = lno - 1;
Index: ex/ex_init.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/ex/ex_init.c,v
retrieving revision 1.12
diff -u -r1.12 ex_init.c
--- ex/ex_init.c 12 Nov 2014 04:28:41 -0000 1.12
+++ ex/ex_init.c 12 Nov 2014 06:36:52 -0000
@@ -46,7 +46,7 @@
EX_PRIVATE *oexp, *nexp;
/* Create the private ex structure. */
- CALLOC_RET(orig, nexp, EX_PRIVATE *, 1, sizeof(EX_PRIVATE));
+ CALLOC_RET(orig, nexp, 1, sizeof(EX_PRIVATE));
sp->ex_private = nexp;
/* Initialize queues. */
@@ -275,7 +275,7 @@
gp = sp->gp;
if (EXCMD_RUNNING(gp)) {
- CALLOC_RET(sp, ecp, EXCMD *, 1, sizeof(EXCMD));
+ CALLOC_RET(sp, ecp, 1, sizeof(EXCMD));
LIST_INSERT_HEAD(&gp->ecq, ecp, q);
} else
ecp = &gp->excmd;
Index: ex/ex_script.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/ex/ex_script.c,v
retrieving revision 1.20
diff -u -r1.20 ex_script.c
--- ex/ex_script.c 12 Nov 2014 04:28:41 -0000 1.20
+++ ex/ex_script.c 12 Nov 2014 07:23:30 -0000
@@ -88,7 +88,7 @@
if (opts_empty(sp, O_SHELL, 0))
return (1);
- MALLOC_RET(sp, sc, SCRIPT *, sizeof(SCRIPT));
+ MALLOC_RET(sp, sc, sizeof(SCRIPT));
sp->script = sc;
sc->sh_prompt = NULL;
sc->sh_prompt_len = 0;
@@ -363,11 +363,7 @@
TAILQ_FOREACH(tsp, &gp->dq, q)
if (F_ISSET(sp, SC_SCRIPT))
nfds++;
- pfd = calloc(nfds, sizeof(struct pollfd));
- if (pfd == NULL) {
- msgq(sp, M_SYSERR, "malloc");
- return (1);
- }
+ CALLOC_RET(sp, pfd, nfds, sizeof(struct pollfd));
/* Setup events bitmasks. */
pfd[0].fd = STDIN_FILENO;
@@ -434,11 +430,7 @@
nfds++;
if (nfds == 0)
return (0);
- pfd = calloc(nfds, sizeof(struct pollfd));
- if (pfd == NULL) {
- msgq(sp, M_SYSERR, "malloc");
- return (1);
- }
+ CALLOC_RET(sp, pfd, nfds, sizeof(struct pollfd));
/* Setup events bitmasks. */
nfds = 0;
@@ -575,7 +567,7 @@
sc = sp->script;
if (sc->sh_prompt)
free(sc->sh_prompt);
- MALLOC(sp, sc->sh_prompt, char *, len + 1);
+ MALLOC(sp, sc->sh_prompt, len + 1);
if (sc->sh_prompt == NULL) {
sscr_end(sp);
return (1);
Index: ex/ex_source.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/ex/ex_source.c,v
retrieving revision 1.9
diff -u -r1.9 ex_source.c
--- ex/ex_source.c 12 Nov 2014 04:28:41 -0000 1.9
+++ ex/ex_source.c 12 Nov 2014 06:36:52 -0000
@@ -58,7 +58,7 @@
goto err;
}
- MALLOC(sp, bp, char *, (size_t)sb.st_size + 1);
+ MALLOC(sp, bp, (size_t)sb.st_size + 1);
if (bp == NULL) {
(void)close(fd);
return (1);
Index: ex/ex_subst.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/ex/ex_subst.c,v
retrieving revision 1.21
diff -u -r1.21 ex_subst.c
--- ex/ex_subst.c 12 Nov 2014 04:28:41 -0000 1.21
+++ ex/ex_subst.c 12 Nov 2014 07:40:09 -0000
@@ -301,7 +301,7 @@
#define NEEDNEWLINE(sp) {
\
if ((sp)->newl_len == (sp)->newl_cnt) { \
(sp)->newl_len += 25; \
- REALLOCARRAY((sp), (sp)->newl, size_t *, \
+ REALLOCARRAY((sp), (sp)->newl, \
(sp)->newl_len, sizeof(size_t)); \
if ((sp)->newl == NULL) { \
(sp)->newl_len = 0; \
@@ -313,7 +313,7 @@
#define BUILD(sp, l, len) {
\
if (lbclen + (len) > lblen) { \
lblen += MAX(lbclen + (len), 256); \
- REALLOC((sp), lb, char *, lblen); \
+ REALLOC((sp), lb, lblen); \
if (lb == NULL) { \
lbclen = 0; \
return (1); \
@@ -326,7 +326,7 @@
#define NEEDSP(sp, len, pnt) {
\
if (lbclen + (len) > lblen) { \
lblen += MAX(lbclen + (len), 256); \
- REALLOC((sp), lb, char *, lblen); \
+ REALLOC((sp), lb, lblen); \
if (lb == NULL) { \
lbclen = 0; \
return (1); \
@@ -737,7 +737,7 @@
goto err;
if (db_get(sp, lno, DBG_FATAL, &s, &llen))
goto err;
- ADD_SPACE_RET(sp, bp, blen, llen)
+ ADD_SPACE_RET(sp, bp, blen, llen);
memcpy(bp, s, llen);
s = bp;
len = llen - offset;
@@ -949,7 +949,7 @@
* Regcomp isn't 8-bit clean, so the pattern is nul-terminated
* for now. There's just no other solution.
*/
- MALLOC(sp, *ptrnp, char *, plen + 1);
+ MALLOC(sp, *ptrnp, plen + 1);
if (*ptrnp != NULL) {
memcpy(*ptrnp, ptrn, plen);
(*ptrnp)[plen] = '\0';
Index: ex/ex_tag.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/ex/ex_tag.c,v
retrieving revision 1.19
diff -u -r1.19 ex_tag.c
--- ex/ex_tag.c 12 Nov 2014 04:28:41 -0000 1.19
+++ ex/ex_tag.c 12 Nov 2014 06:36:52 -0000
@@ -139,11 +139,11 @@
rtqp = NULL;
if (TAILQ_EMPTY(&exp->tq)) {
/* Initialize the `local context' tag queue structure. */
- CALLOC_GOTO(sp, rtqp, TAGQ *, 1, sizeof(TAGQ));
+ CALLOC_GOTO(sp, rtqp, 1, sizeof(TAGQ));
TAILQ_INIT(&rtqp->tagq);
/* Initialize and link in its tag structure. */
- CALLOC_GOTO(sp, rtp, TAG *, 1, sizeof(TAG));
+ CALLOC_GOTO(sp, rtp, 1, sizeof(TAG));
TAILQ_INSERT_HEAD(&rtqp->tagq, rtp, q);
rtqp->current = rtp;
}
@@ -667,7 +667,7 @@
{
TAGF *tfp;
- MALLOC_RET(sp, tfp, TAGF *, sizeof(TAGF));
+ MALLOC_RET(sp, tfp, sizeof(TAGF));
*tfp = *otfp;
/* XXX: Allocate as part of the TAGF structure!!! */
@@ -693,7 +693,7 @@
len = sizeof(TAGQ);
if (otqp->tag != NULL)
len += otqp->tlen + 1;
- MALLOC_RET(sp, tqp, TAGQ *, len);
+ MALLOC_RET(sp, tqp, len);
memcpy(tqp, otqp, len);
TAILQ_INIT(&tqp->tagq);
@@ -720,7 +720,7 @@
len += otp->fnlen + 1;
if (otp->search != NULL)
len += otp->slen + 1;
- MALLOC_RET(sp, tp, TAG *, len);
+ MALLOC_RET(sp, tp, len);
memcpy(tp, otp, len);
if (otp->fname != NULL)
@@ -829,8 +829,8 @@
for (p = t = str;; ++p) {
if (*p == '\0' || isblank(*p)) {
if ((len = p - t) > 1) {
- MALLOC_RET(sp, tfp, TAGF *, sizeof(TAGF));
- MALLOC(sp, tfp->name, char *, len + 1);
+ MALLOC_RET(sp, tfp, sizeof(TAGF));
+ MALLOC(sp, tfp->name, len + 1);
if (tfp->name == NULL) {
free(tfp);
return (1);
@@ -949,7 +949,7 @@
/* Allocate and initialize the tag queue structure. */
len = strlen(tag);
- CALLOC_GOTO(sp, tqp, TAGQ *, 1, sizeof(TAGQ) + len + 1);
+ CALLOC_GOTO(sp, tqp, 1, sizeof(TAGQ) + len + 1);
TAILQ_INIT(&tqp->tagq);
tqp->tag = tqp->buf;
memcpy(tqp->tag, tag, (tqp->tlen = len) + 1);
@@ -1103,7 +1103,7 @@
ctag_file(sp, tfp, name, &dname, &dlen);
CALLOC_GOTO(sp, tp,
- TAG *, 1, sizeof(TAG) + dlen + 2 + nlen + 1 + slen + 1);
+ 1, sizeof(TAG) + dlen + 2 + nlen + 1 + slen + 1);
tp->fname = tp->buf;
if (dlen != 0) {
memcpy(tp->fname, dname, dlen);
Index: perl_api/perlsfio.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/perl_api/perlsfio.c,v
retrieving revision 1.5
diff -u -r1.5 perlsfio.c
--- perl_api/perlsfio.c 12 Nov 2014 04:28:41 -0000 1.5
+++ perl_api/perlsfio.c 12 Nov 2014 06:36:53 -0000
@@ -62,7 +62,7 @@
{
Sfdisc_t* disc;
- MALLOC(scrp, disc, Sfdisc_t*, sizeof(Sfdisc_t));
+ MALLOC(scrp, disc, sizeof(Sfdisc_t));
if (!disc) return disc;
disc->readf = (Sfread_f)NULL;
Index: vi/v_init.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/vi/v_init.c,v
retrieving revision 1.6
diff -u -r1.6 v_init.c
--- vi/v_init.c 12 Nov 2014 04:28:41 -0000 1.6
+++ vi/v_init.c 12 Nov 2014 06:36:53 -0000
@@ -37,7 +37,7 @@
VI_PRIVATE *ovip, *nvip;
/* Create the private vi structure. */
- CALLOC_RET(orig, nvip, VI_PRIVATE *, 1, sizeof(VI_PRIVATE));
+ CALLOC_RET(orig, nvip, 1, sizeof(VI_PRIVATE));
sp->vi_private = nvip;
/* Invalidate the line size cache. */
@@ -50,7 +50,7 @@
/* User can replay the last input, but nothing else. */
if (ovip->rep_len != 0) {
- MALLOC_RET(orig, nvip->rep, EVENT *, ovip->rep_len);
+ MALLOC_RET(orig, nvip->rep, ovip->rep_len);
memmove(nvip->rep, ovip->rep, ovip->rep_len);
nvip->rep_len = ovip->rep_len;
}
Index: vi/v_paragraph.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/vi/v_paragraph.c,v
retrieving revision 1.7
diff -u -r1.7 v_paragraph.c
--- vi/v_paragraph.c 12 Nov 2014 04:28:41 -0000 1.7
+++ vi/v_paragraph.c 12 Nov 2014 06:36:53 -0000
@@ -323,7 +323,7 @@
if (p_len == 0 && s_len == 0)
return (0);
- MALLOC_RET(sp, p, char *, p_len + s_len + 1);
+ MALLOC_RET(sp, p, p_len + s_len + 1);
vip = VIP(sp);
if (vip->ps != NULL)
Index: vi/vi.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/vi/vi.c,v
retrieving revision 1.15
diff -u -r1.15 vi.c
--- vi/vi.c 12 Nov 2014 04:28:41 -0000 1.15
+++ vi/vi.c 12 Nov 2014 06:36:53 -0000
@@ -962,7 +962,7 @@
sp->woff = 0;
/* Create a screen map. */
- CALLOC_RET(sp, HMAP, SMAP *, SIZE_HMAP(sp), sizeof(SMAP));
+ CALLOC_RET(sp, HMAP, SIZE_HMAP(sp), sizeof(SMAP));
TMAP = HMAP + (sp->t_rows - 1);
HMAP->lno = sp->lno;
HMAP->coff = 0;
Index: vi/vs_msg.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/vi/vs_msg.c,v
retrieving revision 1.13
diff -u -r1.13 vs_msg.c
--- vi/vs_msg.c 12 Nov 2014 04:28:41 -0000 1.13
+++ vi/vs_msg.c 12 Nov 2014 06:36:53 -0000
@@ -876,8 +876,8 @@
* allocate memory here, we're genuinely screwed, dump the message
* to stderr in the (probably) vain hope that someone will see it.
*/
- CALLOC_GOTO(sp, mp_n, MSGS *, 1, sizeof(MSGS));
- MALLOC_GOTO(sp, mp_n->buf, char *, len);
+ CALLOC_GOTO(sp, mp_n, 1, sizeof(MSGS));
+ MALLOC_GOTO(sp, mp_n->buf, len);
memmove(mp_n->buf, p, len);
mp_n->len = len;
Index: vi/vs_split.c
===================================================================
RCS file: /cvs/src/usr.bin/vi/vi/vs_split.c,v
retrieving revision 1.12
diff -u -r1.12 vs_split.c
--- vi/vs_split.c 12 Nov 2014 04:28:41 -0000 1.12
+++ vi/vs_split.c 12 Nov 2014 06:36:53 -0000
@@ -59,7 +59,7 @@
half = 6;
/* Get a new screen map. */
- CALLOC(sp, _HMAP(new), SMAP *, SIZE_HMAP(sp), sizeof(SMAP));
+ CALLOC(sp, _HMAP(new), SIZE_HMAP(sp), sizeof(SMAP));
if (_HMAP(new) == NULL)
return (1);
_HMAP(new)->lno = sp->lno;
@@ -422,7 +422,7 @@
nsp->defscroll = nsp->t_maxrows / 2;
/* Allocate a new screen map. */
- CALLOC_RET(nsp, _HMAP(nsp), SMAP *, SIZE_HMAP(nsp), sizeof(SMAP));
+ CALLOC_RET(nsp, _HMAP(nsp), SIZE_HMAP(nsp), sizeof(SMAP));
_TMAP(nsp) = _HMAP(nsp) + (nsp->t_rows - 1);
/* Fill the map. */