{"thread":{"id":"5395","subject":"[PATCH] Use xrealloc instead of realloc","startedAt":"2006-08-26T14:16:18Z","lastAt":"2006-08-26T14:16:18Z","messageCount":1,"participants":["Jonas Fonseca"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"25937","messageId":"20060826141618.GB11601@diku.dk","threadId":"5395","inReplyTo":null,"subject":"[PATCH] Use xrealloc instead of realloc","fromName":"Jonas Fonseca","fromEmail":"fonseca@diku.dk","sentAt":"2006-08-26T14:16:18Z","receivedAt":"2006-08-26T14:16:18Z","isPatch":true,"sender":{"key":"fonseca@diku.dk","avatar":"https://gravatar.com/avatar/f82f3ad698717c51873b020c750a92438c820a24056dc39fe4d07baa10a92264?d=mp&s=160"},"body":"Change places that use realloc, without a proper error path, to instead use\nxrealloc. Drop an erroneous error path in the daemon code that used errno\nin the die message in favour of the simpler xrealloc.\n\nSigned-off-by: Jonas Fonseca <fonseca@diku.dk>\n---\n\nIf this sort of cleanup is desired, I can make one for malloc -> xmalloc\ntoo.\n\n builtin-fmt-merge-msg.c |    4 ++--\n builtin-log.c           |    4 ++--\n builtin-mv.c            |    6 +++---\n daemon.c                |    7 +------\n diff-delta.c            |    2 +-\n dir.c                   |    4 ++--\n git.c                   |    6 +++---\n sha1_file.c             |    2 +-\n xdiff-interface.c       |   12 ++++++------\n 9 files changed, 21 insertions(+), 26 deletions(-)\n\ndiff --git a/builtin-fmt-merge-msg.c b/builtin-fmt-merge-msg.c\nindex 28b5dfd..a5ed8db 100644\n--- a/builtin-fmt-merge-msg.c\n+++ b/builtin-fmt-merge-msg.c\n@@ -27,8 +27,8 @@ static void append_to_list(struct list *\n {\n \tif (list->nr == list->alloc) {\n \t\tlist->alloc += 32;\n-\t\tlist->list = realloc(list->list, sizeof(char *) * list->alloc);\n-\t\tlist->payload = realloc(list->payload,\n+\t\tlist->list = xrealloc(list->list, sizeof(char *) * list->alloc);\n+\t\tlist->payload = xrealloc(list->payload,\n \t\t\t\tsizeof(char *) * list->alloc);\n \t}\n \tlist->payload[list->nr] = payload;\ndiff --git a/builtin-log.c b/builtin-log.c\nindex 691cf3a..fbc58bb 100644\n--- a/builtin-log.c\n+++ b/builtin-log.c\n@@ -101,7 +101,7 @@ static int git_format_config(const char \n \tif (!strcmp(var, \"format.headers\")) {\n \t\tint len = strlen(value);\n \t\textra_headers_size += len + 1;\n-\t\textra_headers = realloc(extra_headers, extra_headers_size);\n+\t\textra_headers = xrealloc(extra_headers, extra_headers_size);\n \t\textra_headers[extra_headers_size - len - 1] = 0;\n \t\tstrcat(extra_headers, value);\n \t\treturn 0;\n@@ -381,7 +381,7 @@ int cmd_format_patch(int argc, const cha\n \t\t\tcontinue;\n \n \t\tnr++;\n-\t\tlist = realloc(list, nr * sizeof(list[0]));\n+\t\tlist = xrealloc(list, nr * sizeof(list[0]));\n \t\tlist[nr - 1] = commit;\n \t}\n \ttotal = nr;\ndiff --git a/builtin-mv.c b/builtin-mv.c\nindex fd1e520..4d21d88 100644\n--- a/builtin-mv.c\n+++ b/builtin-mv.c\n@@ -168,13 +168,13 @@ int cmd_mv(int argc, const char **argv, \n \t\t\t\tint j, dst_len;\n \n \t\t\t\tif (last - first > 0) {\n-\t\t\t\t\tsource = realloc(source,\n+\t\t\t\t\tsource = xrealloc(source,\n \t\t\t\t\t\t\t(count + last - first)\n \t\t\t\t\t\t\t* sizeof(char *));\n-\t\t\t\t\tdestination = realloc(destination,\n+\t\t\t\t\tdestination = xrealloc(destination,\n \t\t\t\t\t\t\t(count + last - first)\n \t\t\t\t\t\t\t* sizeof(char *));\n-\t\t\t\t\tmodes = realloc(modes,\n+\t\t\t\t\tmodes = xrealloc(modes,\n \t\t\t\t\t\t\t(count + last - first)\n \t\t\t\t\t\t\t* sizeof(enum update_mode));\n \t\t\t\t}\ndiff --git a/daemon.c b/daemon.c\nindex dd3915a..66ec830 100644\n--- a/daemon.c\n+++ b/daemon.c\n@@ -529,7 +529,6 @@ static int socksetup(int port, int **soc\n \n \tfor (ai = ai0; ai; ai = ai->ai_next) {\n \t\tint sockfd;\n-\t\tint *newlist;\n \n \t\tsockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);\n \t\tif (sockfd < 0)\n@@ -563,11 +562,7 @@ #endif\n \t\t\tcontinue;\t/* not fatal */\n \t\t}\n \n-\t\tnewlist = realloc(socklist, sizeof(int) * (socknum + 1));\n-\t\tif (!newlist)\n-\t\t\tdie(\"memory allocation failed: %s\", strerror(errno));\n-\n-\t\tsocklist = newlist;\n+\t\tsocklist = xrealloc(socklist, sizeof(int) * (socknum + 1));\n \t\tsocklist[socknum++] = sockfd;\n \n \t\tif (maxfd < sockfd)\ndiff --git a/diff-delta.c b/diff-delta.c\nindex 51e2e56..fa16d06 100644\n--- a/diff-delta.c\n+++ b/diff-delta.c\n@@ -392,7 +392,7 @@ create_delta(const struct delta_index *i\n \t\t\t\toutsize = max_size + MAX_OP_SIZE + 1;\n \t\t\tif (max_size && outpos > max_size)\n \t\t\t\tbreak;\n-\t\t\tout = realloc(out, outsize);\n+\t\t\tout = xrealloc(out, outsize);\n \t\t\tif (!out) {\n \t\t\t\tfree(tmp);\n \t\t\t\treturn NULL;\ndiff --git a/dir.c b/dir.c\nindex a686de6..d53d48f 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -101,8 +101,8 @@ void add_exclude(const char *string, con\n \tx->baselen = baselen;\n \tif (which->nr == which->alloc) {\n \t\twhich->alloc = alloc_nr(which->alloc);\n-\t\twhich->excludes = realloc(which->excludes,\n-\t\t\t\t\t  which->alloc * sizeof(x));\n+\t\twhich->excludes = xrealloc(which->excludes,\n+\t\t\t\t\t   which->alloc * sizeof(x));\n \t}\n \twhich->excludes[which->nr++] = x;\n }\ndiff --git a/git.c b/git.c\nindex a01d195..3adf262 100644\n--- a/git.c\n+++ b/git.c\n@@ -120,7 +120,7 @@ static int split_cmdline(char *cmdline, \n \t\t\t\t; /* skip */\n \t\t\tif (count >= size) {\n \t\t\t\tsize += 16;\n-\t\t\t\t*argv = realloc(*argv, sizeof(char*) * size);\n+\t\t\t\t*argv = xrealloc(*argv, sizeof(char*) * size);\n \t\t\t}\n \t\t\t(*argv)[count++] = cmdline + dst;\n \t\t} else if(!quoted && (c == '\\'' || c == '\"')) {\n@@ -191,8 +191,8 @@ static int handle_alias(int *argcp, cons\n \t\t\tfflush(stderr);\n \t\t}\n \n-\t\tnew_argv = realloc(new_argv, sizeof(char*) *\n-\t\t\t\t   (count + *argcp + 1));\n+\t\tnew_argv = xrealloc(new_argv, sizeof(char*) *\n+\t\t\t\t    (count + *argcp + 1));\n \t\t/* insert after command name */\n \t\tmemcpy(new_argv + count, *argv + 1, sizeof(char*) * *argcp);\n \t\tnew_argv[count+*argcp] = NULL;\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 789deb7..879390a 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -1804,7 +1804,7 @@ int read_pipe(int fd, char** return_buf,\n \t\t\toff += iret;\n \t\t\tif (off == size) {\n \t\t\t\tsize *= 2;\n-\t\t\t\tbuf = realloc(buf, size);\n+\t\t\t\tbuf = xrealloc(buf, size);\n \t\t\t}\n \t\t}\n \t} while (iret > 0);\ndiff --git a/xdiff-interface.c b/xdiff-interface.c\nindex 6a82da7..08602f5 100644\n--- a/xdiff-interface.c\n+++ b/xdiff-interface.c\n@@ -69,9 +69,9 @@ int xdiff_outf(void *priv_, mmbuffer_t *\n \tfor (i = 0; i < nbuf; i++) {\n \t\tif (mb[i].ptr[mb[i].size-1] != '\\n') {\n \t\t\t/* Incomplete line */\n-\t\t\tpriv->remainder = realloc(priv->remainder,\n-\t\t\t\t\t\t  priv->remainder_size +\n-\t\t\t\t\t\t  mb[i].size);\n+\t\t\tpriv->remainder = xrealloc(priv->remainder,\n+\t\t\t\t\t\t   priv->remainder_size +\n+\t\t\t\t\t\t   mb[i].size);\n \t\t\tmemcpy(priv->remainder + priv->remainder_size,\n \t\t\t       mb[i].ptr, mb[i].size);\n \t\t\tpriv->remainder_size += mb[i].size;\n@@ -83,9 +83,9 @@ int xdiff_outf(void *priv_, mmbuffer_t *\n \t\t\tconsume_one(priv, mb[i].ptr, mb[i].size);\n \t\t\tcontinue;\n \t\t}\n-\t\tpriv->remainder = realloc(priv->remainder,\n-\t\t\t\t\t  priv->remainder_size +\n-\t\t\t\t\t  mb[i].size);\n+\t\tpriv->remainder = xrealloc(priv->remainder,\n+\t\t\t\t\t   priv->remainder_size +\n+\t\t\t\t\t   mb[i].size);\n \t\tmemcpy(priv->remainder + priv->remainder_size,\n \t\t       mb[i].ptr, mb[i].size);\n \t\tconsume_one(priv, priv->remainder,\n-- \n1.4.2.GIT\n\n-- \nJonas Fonseca\n"}]}