summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--array.h4
-rw-r--r--cmd-capture-pane.c2
-rw-r--r--cmd-save-buffer.c2
-rw-r--r--cmd-set-buffer.c2
-rw-r--r--cmd-string.c19
-rw-r--r--cmd.c6
-rw-r--r--format.c8
-rw-r--r--grid.c14
-rw-r--r--input.c4
-rw-r--r--paste.c2
-rw-r--r--screen.c4
-rw-r--r--status.c6
-rw-r--r--tmux.h3
-rw-r--r--utf8.c8
-rw-r--r--window-choose.c2
-rw-r--r--window-copy.c10
-rw-r--r--xmalloc.c17
17 files changed, 65 insertions, 48 deletions
diff --git a/array.h b/array.h
index c811bf6d..671bea42 100644
--- a/array.h
+++ b/array.h
@@ -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)
diff --git a/cmd-capture-pane.c b/cmd-capture-pane.c
index e3c551ab..50bddc7c 100644
--- a/cmd-capture-pane.c
+++ b/cmd-capture-pane.c
@@ -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);
diff --git a/cmd-save-buffer.c b/cmd-save-buffer.c
index ea238450..db80e48d 100644
--- a/cmd-save-buffer.c
+++ b/cmd-save-buffer.c
@@ -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);
diff --git a/cmd-set-buffer.c b/cmd-set-buffer.c
index 5a82b4d8..88bd0c10 100644
--- a/cmd-set-buffer.c
+++ b/cmd-set-buffer.c
@@ -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;
diff --git a/cmd-string.c b/cmd-string.c
index e19b8856..db1723cb 100644
--- a/cmd-string.c
+++ b/cmd-string.c
@@ -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);
diff --git a/cmd.c b/cmd.c
index 82410323..66ac58b8 100644
--- a/cmd.c
+++ b/cmd.c
@@ -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';
}
diff --git a/format.c b/format.c
index ed3c2037..b5e51494 100644
--- a/format.c
+++ b/format.c
@@ -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++] = '#';
diff --git a/grid.c b/grid.c
index b2351b28..ef7c374b 100644
--- a/grid.c
+++ b/grid.c
@@ -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;
diff --git a/input.c b/input.c
index ac1db677..fcc91180 100644
--- a/input.c
+++ b/input.c
@@ -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;
diff --git a/paste.c b/paste.c
index 603adda6..0332e60e 100644
--- a/paste.c
+++ b/paste.c
@@ -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);
diff --git a/screen.c b/screen.c
index 703f3e10..e5550222 100644
--- a/screen.c
+++ b/screen.c
@@ -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) {
diff --git a/status.c b/status.c
index 96110c77..70e0551b 100644
--- a/status.c
+++ b/status.c
@@ -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;
diff --git a/tmux.h b/tmux.h
index 9be247f8..d1197422 100644
--- a/tmux.h
+++ b/tmux.h
@@ -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 *, ...);
diff --git a/utf8.c b/utf8.c
index 78ed1675..2940ebb9 100644
--- a/utf8.c
+++ b/utf8.c
@@ -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);
}
diff --git a/window-choose.c b/window-choose.c
index 99e88036..ef3b8562 100644
--- a/window-choose.c
+++ b/window-choose.c
@@ -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';
diff --git a/window-copy.c b/window-copy.c
index 44fe6ae8..69921f62 100644
--- a/window-copy.c
+++ b/window-copy.c
@@ -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';
}
}
diff --git a/xmalloc.c b/xmalloc.c
index cb734edc..49a0eff9 100644
--- a/xmalloc.c
+++ b/xmalloc.c
@@ -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);
}