[PATCH] Use xrealloc instead of realloc

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxx>
---

If this sort of cleanup is desired, I can make one for malloc -> xmalloc
too.

 builtin-fmt-merge-msg.c |    4 ++--
 builtin-log.c           |    4 ++--
 builtin-mv.c            |    6 +++---
 daemon.c                |    7 +------
 diff-delta.c            |    2 +-
 dir.c                   |    4 ++--
 git.c                   |    6 +++---
 sha1_file.c             |    2 +-
 xdiff-interface.c       |   12 ++++++------
 9 files changed, 21 insertions(+), 26 deletions(-)

diff --git a/builtin-fmt-merge-msg.c b/builtin-fmt-merge-msg.c
index 28b5dfd..a5ed8db 100644
--- a/builtin-fmt-merge-msg.c
+++ b/builtin-fmt-merge-msg.c
@@ -27,8 +27,8 @@ static void append_to_list(struct list *
 {
 	if (list->nr == list->alloc) {
 		list->alloc += 32;
-		list->list = realloc(list->list, sizeof(char *) * list->alloc);
-		list->payload = realloc(list->payload,
+		list->list = xrealloc(list->list, sizeof(char *) * list->alloc);
+		list->payload = xrealloc(list->payload,
 				sizeof(char *) * list->alloc);
 	}
 	list->payload[list->nr] = payload;
diff --git a/builtin-log.c b/builtin-log.c
index 691cf3a..fbc58bb 100644
--- a/builtin-log.c
+++ b/builtin-log.c
@@ -101,7 +101,7 @@ static int git_format_config(const char 
 	if (!strcmp(var, "format.headers")) {
 		int len = strlen(value);
 		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;
 		strcat(extra_headers, value);
 		return 0;
@@ -381,7 +381,7 @@ int cmd_format_patch(int argc, const cha
 			continue;
 
 		nr++;
-		list = realloc(list, nr * sizeof(list[0]));
+		list = xrealloc(list, nr * sizeof(list[0]));
 		list[nr - 1] = commit;
 	}
 	total = nr;
diff --git a/builtin-mv.c b/builtin-mv.c
index fd1e520..4d21d88 100644
--- a/builtin-mv.c
+++ b/builtin-mv.c
@@ -168,13 +168,13 @@ int cmd_mv(int argc, const char **argv, 
 				int j, dst_len;
 
 				if (last - first > 0) {
-					source = realloc(source,
+					source = xrealloc(source,
 							(count + last - first)
 							* sizeof(char *));
-					destination = realloc(destination,
+					destination = xrealloc(destination,
 							(count + last - first)
 							* sizeof(char *));
-					modes = realloc(modes,
+					modes = xrealloc(modes,
 							(count + last - first)
 							* sizeof(enum update_mode));
 				}
diff --git a/daemon.c b/daemon.c
index dd3915a..66ec830 100644
--- a/daemon.c
+++ b/daemon.c
@@ -529,7 +529,6 @@ static int socksetup(int port, int **soc
 
 	for (ai = ai0; ai; ai = ai->ai_next) {
 		int sockfd;
-		int *newlist;
 
 		sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
 		if (sockfd < 0)
@@ -563,11 +562,7 @@ #endif
 			continue;	/* not fatal */
 		}
 
-		newlist = realloc(socklist, sizeof(int) * (socknum + 1));
-		if (!newlist)
-			die("memory allocation failed: %s", strerror(errno));
-
-		socklist = newlist;
+		socklist = xrealloc(socklist, sizeof(int) * (socknum + 1));
 		socklist[socknum++] = sockfd;
 
 		if (maxfd < sockfd)
diff --git a/diff-delta.c b/diff-delta.c
index 51e2e56..fa16d06 100644
--- a/diff-delta.c
+++ b/diff-delta.c
@@ -392,7 +392,7 @@ create_delta(const struct delta_index *i
 				outsize = max_size + MAX_OP_SIZE + 1;
 			if (max_size && outpos > max_size)
 				break;
-			out = realloc(out, outsize);
+			out = xrealloc(out, outsize);
 			if (!out) {
 				free(tmp);
 				return NULL;
diff --git a/dir.c b/dir.c
index a686de6..d53d48f 100644
--- a/dir.c
+++ b/dir.c
@@ -101,8 +101,8 @@ void add_exclude(const char *string, con
 	x->baselen = baselen;
 	if (which->nr == which->alloc) {
 		which->alloc = alloc_nr(which->alloc);
-		which->excludes = realloc(which->excludes,
-					  which->alloc * sizeof(x));
+		which->excludes = xrealloc(which->excludes,
+					   which->alloc * sizeof(x));
 	}
 	which->excludes[which->nr++] = x;
 }
diff --git a/git.c b/git.c
index a01d195..3adf262 100644
--- a/git.c
+++ b/git.c
@@ -120,7 +120,7 @@ static int split_cmdline(char *cmdline, 
 				; /* skip */
 			if (count >= size) {
 				size += 16;
-				*argv = realloc(*argv, sizeof(char*) * size);
+				*argv = xrealloc(*argv, sizeof(char*) * size);
 			}
 			(*argv)[count++] = cmdline + dst;
 		} else if(!quoted && (c == '\'' || c == '"')) {
@@ -191,8 +191,8 @@ static int handle_alias(int *argcp, cons
 			fflush(stderr);
 		}
 
-		new_argv = realloc(new_argv, sizeof(char*) *
-				   (count + *argcp + 1));
+		new_argv = xrealloc(new_argv, sizeof(char*) *
+				    (count + *argcp + 1));
 		/* insert after command name */
 		memcpy(new_argv + count, *argv + 1, sizeof(char*) * *argcp);
 		new_argv[count+*argcp] = NULL;
diff --git a/sha1_file.c b/sha1_file.c
index 789deb7..879390a 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1804,7 +1804,7 @@ int read_pipe(int fd, char** return_buf,
 			off += iret;
 			if (off == size) {
 				size *= 2;
-				buf = realloc(buf, size);
+				buf = xrealloc(buf, size);
 			}
 		}
 	} while (iret > 0);
diff --git a/xdiff-interface.c b/xdiff-interface.c
index 6a82da7..08602f5 100644
--- a/xdiff-interface.c
+++ b/xdiff-interface.c
@@ -69,9 +69,9 @@ int xdiff_outf(void *priv_, mmbuffer_t *
 	for (i = 0; i < nbuf; i++) {
 		if (mb[i].ptr[mb[i].size-1] != '\n') {
 			/* Incomplete line */
-			priv->remainder = realloc(priv->remainder,
-						  priv->remainder_size +
-						  mb[i].size);
+			priv->remainder = xrealloc(priv->remainder,
+						   priv->remainder_size +
+						   mb[i].size);
 			memcpy(priv->remainder + priv->remainder_size,
 			       mb[i].ptr, mb[i].size);
 			priv->remainder_size += mb[i].size;
@@ -83,9 +83,9 @@ int xdiff_outf(void *priv_, mmbuffer_t *
 			consume_one(priv, mb[i].ptr, mb[i].size);
 			continue;
 		}
-		priv->remainder = realloc(priv->remainder,
-					  priv->remainder_size +
-					  mb[i].size);
+		priv->remainder = xrealloc(priv->remainder,
+					   priv->remainder_size +
+					   mb[i].size);
 		memcpy(priv->remainder + priv->remainder_size,
 		       mb[i].ptr, mb[i].size);
 		consume_one(priv, priv->remainder,
-- 
1.4.2.GIT

-- 
Jonas Fonseca
-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]