Add xreallocarray and remove nmemb argument from xrealloc.

This commit is contained in:
nicm 2014-10-08 17:35:58 +00:00
parent 77efcf8bdd
commit a27ba6e380
17 changed files with 65 additions and 48 deletions

View File

@ -39,10 +39,10 @@
fatalx("size too big"); \
if ((a)->space == 0) { \
(a)->space = ARRAY_INITIALSPACE(a); \
(a)->list = xrealloc((a)->list, 1, (a)->space); \
(a)->list = xrealloc((a)->list, (a)->space); \
} \
while ((a)->space <= ((a)->num + (n)) * ARRAY_ITEMSIZE(a)) { \
(a)->list = xrealloc((a)->list, 2, (a)->space); \
(a)->list = xreallocarray((a)->list, 2, (a)->space); \
(a)->space *= 2; \
} \
} while (0)

View File

@ -48,7 +48,7 @@ const struct cmd_entry cmd_capture_pane_entry = {
char *
cmd_capture_pane_append(char *buf, size_t *len, char *line, size_t linelen)
{
buf = xrealloc(buf, 1, *len + linelen + 1);
buf = xrealloc(buf, *len + linelen + 1);
memcpy(buf + *len, line, linelen);
*len += linelen;
return (buf);

View File

@ -147,7 +147,7 @@ do_print:
size = pb->size - used;
msglen = size * 4 + 1;
msg = xrealloc(msg, 1, msglen);
msg = xrealloc(msg, msglen);
strvisx(msg, start, size, VIS_OCTAL|VIS_TAB);
cmdq_print(cmdq, "%s", msg);

View File

@ -104,7 +104,7 @@ cmd_set_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
memcpy(pdata, pb->data, psize);
}
pdata = xrealloc(pdata, 1, psize + newsize);
pdata = xrealloc(pdata, psize + newsize);
memcpy(pdata + psize, args->argv[0], newsize);
psize += newsize;

View File

@ -107,10 +107,11 @@ cmd_string_parse(const char *s, struct cmd_list **cmdlist, const char *file,
case ' ':
case '\t':
if (buf != NULL) {
buf = xrealloc(buf, 1, len + 1);
buf = xrealloc(buf, len + 1);
buf[len] = '\0';
argv = xrealloc(argv, argc + 1, sizeof *argv);
argv = xreallocarray(argv, argc + 1,
sizeof *argv);
argv[argc++] = buf;
buf = NULL;
@ -151,7 +152,7 @@ cmd_string_parse(const char *s, struct cmd_list **cmdlist, const char *file,
if (len >= SIZE_MAX - 2)
goto error;
buf = xrealloc(buf, 1, len + 1);
buf = xrealloc(buf, len + 1);
buf[len++] = ch;
break;
}
@ -179,7 +180,7 @@ cmd_string_copy(char **dst, char *src, size_t *len)
srclen = strlen(src);
*dst = xrealloc(*dst, 1, *len + srclen + 1);
*dst = xrealloc(*dst, *len + srclen + 1);
strlcpy(*dst + *len, src, srclen + 1);
*len += srclen;
@ -231,11 +232,11 @@ cmd_string_string(const char *s, size_t *p, char endch, int esc)
if (len >= SIZE_MAX - 2)
goto error;
buf = xrealloc(buf, 1, len + 1);
buf = xrealloc(buf, len + 1);
buf[len++] = ch;
}
buf = xrealloc(buf, 1, len + 1);
buf = xrealloc(buf, len + 1);
buf[len] = '\0';
return (buf);
@ -278,7 +279,7 @@ cmd_string_variable(const char *s, size_t *p)
return (t);
}
buf = xrealloc(buf, 1, len + 1);
buf = xrealloc(buf, len + 1);
buf[len++] = ch;
for (;;) {
@ -288,7 +289,7 @@ cmd_string_variable(const char *s, size_t *p)
else {
if (len >= SIZE_MAX - 3)
goto error;
buf = xrealloc(buf, 1, len + 1);
buf = xrealloc(buf, len + 1);
buf[len++] = ch;
}
}
@ -299,7 +300,7 @@ cmd_string_variable(const char *s, size_t *p)
if (ch != EOF && fch != '{')
cmd_string_ungetc(p); /* ch */
buf = xrealloc(buf, 1, len + 1);
buf = xrealloc(buf, len + 1);
buf[len] = '\0';
envent = environ_find(&global_environ, buf);

6
cmd.c
View File

@ -222,7 +222,7 @@ cmd_stringify_argv(int argc, char **argv)
for (i = 0; i < argc; i++) {
len += strlen(argv[i]) + 1;
buf = xrealloc(buf, 1, len);
buf = xrealloc(buf, len);
if (i == 0)
*buf = '\0';
@ -1302,11 +1302,11 @@ cmd_template_replace(const char *template, const char *s, int idx)
ptr++;
len += strlen(s);
buf = xrealloc(buf, 1, len + 1);
buf = xrealloc(buf, len + 1);
strlcat(buf, s, len + 1);
continue;
}
buf = xrealloc(buf, 1, len + 2);
buf = xrealloc(buf, len + 2);
buf[len++] = ch;
buf[len] = '\0';
}

View File

@ -267,7 +267,7 @@ format_replace(struct format_tree *ft, const char *key, size_t keylen,
/* Expand the buffer and copy in the value. */
while (*len - *off < valuelen + 1) {
*buf = xrealloc(*buf, 2, *len);
*buf = xreallocarray(*buf, 2, *len);
*len *= 2;
}
memcpy(*buf + *off, value, valuelen);
@ -298,7 +298,7 @@ format_expand(struct format_tree *ft, const char *fmt)
while (*fmt != '\0') {
if (*fmt != '#') {
while (len - off < 2) {
buf = xrealloc(buf, 2, len);
buf = xreallocarray(buf, 2, len);
len *= 2;
}
buf[off++] = *fmt++;
@ -326,7 +326,7 @@ format_expand(struct format_tree *ft, const char *fmt)
continue;
case '#':
while (len - off < 2) {
buf = xrealloc(buf, 2, len);
buf = xreallocarray(buf, 2, len);
len *= 2;
}
buf[off++] = '#';
@ -339,7 +339,7 @@ format_expand(struct format_tree *ft, const char *fmt)
s = format_lower[ch - 'a'];
if (s == NULL) {
while (len - off < 3) {
buf = xrealloc(buf, 2, len);
buf = xreallocarray(buf, 2, len);
len *= 2;
}
buf[off++] = '#';

14
grid.c
View File

@ -172,7 +172,8 @@ grid_scroll_history(struct grid *gd)
u_int yy;
yy = gd->hsize + gd->sy;
gd->linedata = xrealloc(gd->linedata, yy + 1, sizeof *gd->linedata);
gd->linedata = xreallocarray(gd->linedata, yy + 1,
sizeof *gd->linedata);
memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
gd->hsize++;
@ -187,7 +188,8 @@ grid_scroll_history_region(struct grid *gd, u_int upper, u_int lower)
/* Create a space for a new line. */
yy = gd->hsize + gd->sy;
gd->linedata = xrealloc(gd->linedata, yy + 1, sizeof *gd->linedata);
gd->linedata = xreallocarray(gd->linedata, yy + 1,
sizeof *gd->linedata);
/* Move the entire screen down to free a space for this line. */
gl_history = &gd->linedata[gd->hsize];
@ -221,7 +223,7 @@ grid_expand_line(struct grid *gd, u_int py, u_int sx)
if (sx <= gl->cellsize)
return;
gl->celldata = xrealloc(gl->celldata, sx, sizeof *gl->celldata);
gl->celldata = xreallocarray(gl->celldata, sx, sizeof *gl->celldata);
for (xx = gl->cellsize; xx < sx; xx++)
grid_put_cell(gd, xx, py, &grid_default_cell);
gl->cellsize = sx;
@ -610,7 +612,7 @@ grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx,
}
while (len < off + size + codelen + 1) {
buf = xrealloc(buf, 2, len);
buf = xreallocarray(buf, 2, len);
len *= 2;
}
@ -685,7 +687,7 @@ grid_reflow_join(struct grid *dst, u_int *py, struct grid_line *src_gl,
nx = ox + to_copy;
/* Resize the destination line. */
dst_gl->celldata = xrealloc(dst_gl->celldata, nx,
dst_gl->celldata = xreallocarray(dst_gl->celldata, nx,
sizeof *dst_gl->celldata);
dst_gl->cellsize = nx;
@ -724,7 +726,7 @@ grid_reflow_split(struct grid *dst, u_int *py, struct grid_line *src_gl,
to_copy = src_gl->cellsize;
/* Expand destination line. */
dst_gl->celldata = xrealloc(NULL, to_copy,
dst_gl->celldata = xreallocarray(NULL, to_copy,
sizeof *dst_gl->celldata);
dst_gl->cellsize = to_copy;
dst_gl->flags |= GRID_LINE_WRAPPED;

View File

@ -909,7 +909,7 @@ input_ground(struct input_ctx *ictx)
if (ictx->input_space > INPUT_BUF_START) {
ictx->input_space = INPUT_BUF_START;
ictx->input_buf = xrealloc(ictx->input_buf, 1, INPUT_BUF_START);
ictx->input_buf = xrealloc(ictx->input_buf, INPUT_BUF_START);
}
}
@ -974,7 +974,7 @@ input_input(struct input_ctx *ictx)
ictx->flags |= INPUT_DISCARD;
return (0);
}
ictx->input_buf = xrealloc(ictx->input_buf, 1, available);
ictx->input_buf = xrealloc(ictx->input_buf, available);
ictx->input_space = available;
}
ictx->input_buf[ictx->input_len++] = ictx->ch;

View File

@ -279,7 +279,7 @@ paste_make_sample(struct paste_buffer *pb, int utf8flag)
len = pb->size;
if (len > width)
len = width;
buf = xrealloc(NULL, len, 4 + 4);
buf = xreallocarray(NULL, len, 4 + 4);
if (utf8flag)
used = utf8_strvis(buf, pb->data, len, flags);

View File

@ -215,8 +215,8 @@ screen_resize_y(struct screen *s, u_int sy)
}
/* Resize line arrays. */
gd->linedata = xrealloc(
gd->linedata, gd->hsize + sy, sizeof *gd->linedata);
gd->linedata = xreallocarray(gd->linedata, gd->hsize + sy,
sizeof *gd->linedata);
/* Size increasing. */
if (sy > oldy) {

View File

@ -992,7 +992,7 @@ status_prompt_key(struct client *c, int key)
/* Insert the new word. */
size += strlen(s);
off = first - c->prompt_buffer;
c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + 1);
c->prompt_buffer = xrealloc(c->prompt_buffer, size + 1);
first = c->prompt_buffer + off;
memmove(first + strlen(s), first, n);
memcpy(first, s, strlen(s));
@ -1170,7 +1170,7 @@ status_prompt_key(struct client *c, int key)
break;
}
c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + n + 1);
c->prompt_buffer = xrealloc(c->prompt_buffer, size + n + 1);
if (c->prompt_index == size) {
memcpy(c->prompt_buffer + c->prompt_index, pb->data, n);
c->prompt_index += n;
@ -1210,7 +1210,7 @@ status_prompt_key(struct client *c, int key)
case MODEKEY_OTHER:
if ((key & 0xff00) != 0 || key < 32 || key == 127)
break;
c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + 2);
c->prompt_buffer = xrealloc(c->prompt_buffer, size + 2);
if (c->prompt_index == size) {
c->prompt_buffer[c->prompt_index++] = key;

3
tmux.h
View File

@ -2408,7 +2408,8 @@ __dead void printflike1 log_fatalx(const char *, ...);
char *xstrdup(const char *);
void *xcalloc(size_t, size_t);
void *xmalloc(size_t);
void *xrealloc(void *, size_t, size_t);
void *xrealloc(void *, size_t);
void *xreallocarray(void *, size_t, size_t);
int printflike2 xasprintf(char **, const char *, ...);
int xvasprintf(char **, const char *, va_list);
int printflike3 xsnprintf(char *, size_t, const char *, ...);

8
utf8.c
View File

@ -419,7 +419,7 @@ utf8_fromcstr(const char *src)
n = 0;
while (*src != '\0') {
dst = xrealloc(dst, n + 1, sizeof *dst);
dst = xreallocarray(dst, n + 1, sizeof *dst);
if (utf8_open(&dst[n], *src)) {
more = 1;
while (*++src != '\0' && more)
@ -436,7 +436,7 @@ utf8_fromcstr(const char *src)
n++;
}
dst = xrealloc(dst, n + 1, sizeof *dst);
dst = xreallocarray(dst, n + 1, sizeof *dst);
dst[n].size = 0;
return (dst);
}
@ -452,12 +452,12 @@ utf8_tocstr(struct utf8_data *src)
n = 0;
for(; src->size != 0; src++) {
dst = xrealloc(dst, n + src->size, 1);
dst = xreallocarray(dst, n + src->size, 1);
memcpy(dst + n, src->data, src->size);
n += src->size;
}
dst = xrealloc(dst, n + 1, 1);
dst = xreallocarray(dst, n + 1, 1);
dst[n] = '\0';
return (dst);
}

View File

@ -317,7 +317,7 @@ window_choose_prompt_input(enum window_choose_input_type input_type,
data->input_prompt = prompt;
input_len = strlen(data->input_str) + 2;
data->input_str = xrealloc(data->input_str, 1, input_len);
data->input_str = xrealloc(data->input_str, input_len);
data->input_str[input_len - 2] = key;
data->input_str[input_len - 1] = '\0';

View File

@ -794,7 +794,7 @@ window_copy_key_input(struct window_pane *wp, int key)
}
inputlen = strlen(data->inputstr);
data->inputstr = xrealloc(data->inputstr, 1, inputlen + n + 1);
data->inputstr = xrealloc(data->inputstr, inputlen + n + 1);
memcpy(data->inputstr + inputlen, pb->data, n);
data->inputstr[inputlen + n] = '\0';
break;
@ -840,7 +840,7 @@ window_copy_key_input(struct window_pane *wp, int key)
break;
inputlen = strlen(data->inputstr) + 2;
data->inputstr = xrealloc(data->inputstr, 1, inputlen);
data->inputstr = xrealloc(data->inputstr, inputlen);
data->inputstr[inputlen - 2] = key;
data->inputstr[inputlen - 1] = '\0';
break;
@ -1533,7 +1533,7 @@ window_copy_append_selection(struct window_pane *wp, const char *bufname)
} else
pb = paste_get_name(bufname);
if (pb != NULL) {
buf = xrealloc(buf, 1, len + pb->size);
buf = xrealloc(buf, len + pb->size);
memmove(buf + pb->size, buf, len);
memcpy(buf, pb->data, pb->size);
len += pb->size;
@ -1589,7 +1589,7 @@ window_copy_copy_line(struct window_pane *wp,
}
}
*buf = xrealloc(*buf, 1, (*off) + ud.size);
*buf = xrealloc(*buf, (*off) + ud.size);
memcpy(*buf + *off, ud.data, ud.size);
*off += ud.size;
}
@ -1597,7 +1597,7 @@ window_copy_copy_line(struct window_pane *wp,
/* Only add a newline if the line wasn't wrapped. */
if (!wrapped || ex != xx) {
*buf = xrealloc(*buf, 1, (*off) + 1);
*buf = xrealloc(*buf, (*off) + 1);
(*buf)[(*off)++] = '\n';
}
}

View File

@ -68,7 +68,20 @@ xmalloc(size_t size)
}
void *
xrealloc(void *oldptr, size_t nmemb, size_t size)
xrealloc(void *oldptr, size_t newsize)
{
void *newptr;
if (newsize == 0)
fatalx("zero size");
if ((newptr = realloc(oldptr, newsize)) == NULL)
fatal("xrealloc failed");
return (newptr);
}
void *
xreallocarray(void *oldptr, size_t nmemb, size_t size)
{
size_t newsize = nmemb * size;
void *newptr;
@ -78,7 +91,7 @@ xrealloc(void *oldptr, size_t nmemb, size_t size)
if (SIZE_MAX / nmemb < size)
fatalx("nmemb * size > SIZE_MAX");
if ((newptr = realloc(oldptr, newsize)) == NULL)
fatal("xrealloc failed");
fatal("xreallocarray failed");
return (newptr);
}