Use xrealloc instead of realloc

Change places that use realloc, without a proper error path, to instead use
xrealloc. Drop an erroneous error path in the daemon code that used errno
in the die message in favour of the simpler xrealloc.

Signed-off-by: Jonas Fonseca <fonseca@diku.dk>
Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
Jonas Fonseca 2006-08-26 16:16:18 +02:00 committed by Junio C Hamano
parent 095c424d08
commit 83572c1a91
9 changed files with 21 additions and 26 deletions

View File

@ -27,8 +27,8 @@ static void append_to_list(struct list *list, char *value, void *payload)
{ {
if (list->nr == list->alloc) { if (list->nr == list->alloc) {
list->alloc += 32; list->alloc += 32;
list->list = realloc(list->list, sizeof(char *) * list->alloc); list->list = xrealloc(list->list, sizeof(char *) * list->alloc);
list->payload = realloc(list->payload, list->payload = xrealloc(list->payload,
sizeof(char *) * list->alloc); sizeof(char *) * list->alloc);
} }
list->payload[list->nr] = payload; list->payload[list->nr] = payload;

View File

@ -101,7 +101,7 @@ static int git_format_config(const char *var, const char *value)
if (!strcmp(var, "format.headers")) { if (!strcmp(var, "format.headers")) {
int len = strlen(value); int len = strlen(value);
extra_headers_size += len + 1; extra_headers_size += len + 1;
extra_headers = realloc(extra_headers, extra_headers_size); extra_headers = xrealloc(extra_headers, extra_headers_size);
extra_headers[extra_headers_size - len - 1] = 0; extra_headers[extra_headers_size - len - 1] = 0;
strcat(extra_headers, value); strcat(extra_headers, value);
return 0; return 0;
@ -381,7 +381,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
continue; continue;
nr++; nr++;
list = realloc(list, nr * sizeof(list[0])); list = xrealloc(list, nr * sizeof(list[0]));
list[nr - 1] = commit; list[nr - 1] = commit;
} }
total = nr; total = nr;

View File

@ -168,13 +168,13 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
int j, dst_len; int j, dst_len;
if (last - first > 0) { if (last - first > 0) {
source = realloc(source, source = xrealloc(source,
(count + last - first) (count + last - first)
* sizeof(char *)); * sizeof(char *));
destination = realloc(destination, destination = xrealloc(destination,
(count + last - first) (count + last - first)
* sizeof(char *)); * sizeof(char *));
modes = realloc(modes, modes = xrealloc(modes,
(count + last - first) (count + last - first)
* sizeof(enum update_mode)); * sizeof(enum update_mode));
} }

View File

@ -526,7 +526,6 @@ static int socksetup(int port, int **socklist_p)
for (ai = ai0; ai; ai = ai->ai_next) { for (ai = ai0; ai; ai = ai->ai_next) {
int sockfd; int sockfd;
int *newlist;
sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (sockfd < 0) if (sockfd < 0)
@ -560,11 +559,7 @@ static int socksetup(int port, int **socklist_p)
continue; /* not fatal */ continue; /* not fatal */
} }
newlist = realloc(socklist, sizeof(int) * (socknum + 1)); socklist = xrealloc(socklist, sizeof(int) * (socknum + 1));
if (!newlist)
die("memory allocation failed: %s", strerror(errno));
socklist = newlist;
socklist[socknum++] = sockfd; socklist[socknum++] = sockfd;
if (maxfd < sockfd) if (maxfd < sockfd)

View File

@ -392,7 +392,7 @@ create_delta(const struct delta_index *index,
outsize = max_size + MAX_OP_SIZE + 1; outsize = max_size + MAX_OP_SIZE + 1;
if (max_size && outpos > max_size) if (max_size && outpos > max_size)
break; break;
out = realloc(out, outsize); out = xrealloc(out, outsize);
if (!out) { if (!out) {
free(tmp); free(tmp);
return NULL; return NULL;

2
dir.c
View File

@ -101,7 +101,7 @@ void add_exclude(const char *string, const char *base,
x->baselen = baselen; x->baselen = baselen;
if (which->nr == which->alloc) { if (which->nr == which->alloc) {
which->alloc = alloc_nr(which->alloc); which->alloc = alloc_nr(which->alloc);
which->excludes = realloc(which->excludes, which->excludes = xrealloc(which->excludes,
which->alloc * sizeof(x)); which->alloc * sizeof(x));
} }
which->excludes[which->nr++] = x; which->excludes[which->nr++] = x;

4
git.c
View File

@ -120,7 +120,7 @@ static int split_cmdline(char *cmdline, const char ***argv)
; /* skip */ ; /* skip */
if (count >= size) { if (count >= size) {
size += 16; size += 16;
*argv = realloc(*argv, sizeof(char*) * size); *argv = xrealloc(*argv, sizeof(char*) * size);
} }
(*argv)[count++] = cmdline + dst; (*argv)[count++] = cmdline + dst;
} else if(!quoted && (c == '\'' || c == '"')) { } else if(!quoted && (c == '\'' || c == '"')) {
@ -191,7 +191,7 @@ static int handle_alias(int *argcp, const char ***argv)
fflush(stderr); fflush(stderr);
} }
new_argv = realloc(new_argv, sizeof(char*) * new_argv = xrealloc(new_argv, sizeof(char*) *
(count + *argcp + 1)); (count + *argcp + 1));
/* insert after command name */ /* insert after command name */
memcpy(new_argv + count, *argv + 1, sizeof(char*) * *argcp); memcpy(new_argv + count, *argv + 1, sizeof(char*) * *argcp);

View File

@ -1740,7 +1740,7 @@ int read_pipe(int fd, char** return_buf, unsigned long* return_size)
off += iret; off += iret;
if (off == size) { if (off == size) {
size *= 2; size *= 2;
buf = realloc(buf, size); buf = xrealloc(buf, size);
} }
} }
} while (iret > 0); } while (iret > 0);

View File

@ -69,7 +69,7 @@ int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
for (i = 0; i < nbuf; i++) { for (i = 0; i < nbuf; i++) {
if (mb[i].ptr[mb[i].size-1] != '\n') { if (mb[i].ptr[mb[i].size-1] != '\n') {
/* Incomplete line */ /* Incomplete line */
priv->remainder = realloc(priv->remainder, priv->remainder = xrealloc(priv->remainder,
priv->remainder_size + priv->remainder_size +
mb[i].size); mb[i].size);
memcpy(priv->remainder + priv->remainder_size, memcpy(priv->remainder + priv->remainder_size,
@ -83,7 +83,7 @@ int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
consume_one(priv, mb[i].ptr, mb[i].size); consume_one(priv, mb[i].ptr, mb[i].size);
continue; continue;
} }
priv->remainder = realloc(priv->remainder, priv->remainder = xrealloc(priv->remainder,
priv->remainder_size + priv->remainder_size +
mb[i].size); mb[i].size);
memcpy(priv->remainder + priv->remainder_size, memcpy(priv->remainder + priv->remainder_size,