Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

cleanup: use internal memory allocation wrapper functions everywhere

The "x"-prefixed versions of strdup, malloc, etc. will check whether the
allocation was successful and terminate the process otherwise.

A few uses of malloc were left alone since they already implemented a
graceful path of failure or were in a quasi external library like xdiff.

Additionally, the call to malloc in compat/win32/syslog.c was not modified
since the syslog() implemented there is a die handler and a call to the
x-wrappers within a die handler could result in recursion should memory
allocation fail.  This will have to be addressed separately.

Signed-off-by: Brandon Casey <drafnel@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information...
commit 040a655116c9755bbf30acd22c34eecb2f502c6d 1 parent 97410b2
Brandon Casey authored October 06, 2011 gitster committed October 06, 2011
2  attr.c
@@ -533,7 +533,7 @@ static void bootstrap_attr_stack(void)
533 533
 
534 534
 		if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
535 535
 			elem = read_attr(GITATTRIBUTES_FILE, 1);
536  
-			elem->origin = strdup("");
  536
+			elem->origin = xstrdup("");
537 537
 			elem->prev = attr_stack;
538 538
 			attr_stack = elem;
539 539
 			debug_push(elem);
2  builtin/mv.c
@@ -29,7 +29,7 @@ static const char **copy_pathspec(const char *prefix, const char **pathspec,
29 29
 			to_copy--;
30 30
 		if (to_copy != length || base_name) {
31 31
 			char *it = xmemdupz(result[i], to_copy);
32  
-			result[i] = base_name ? strdup(basename(it)) : it;
  32
+			result[i] = base_name ? xstrdup(basename(it)) : it;
33 33
 		}
34 34
 	}
35 35
 	return get_pathspec(prefix, result);
2  compat/mingw.c
@@ -1183,7 +1183,7 @@ static int WSAAPI getaddrinfo_stub(const char *node, const char *service,
1183 1183
 	}
1184 1184
 	ai->ai_addrlen = sizeof(struct sockaddr_in);
1185 1185
 	if (hints && (hints->ai_flags & AI_CANONNAME))
1186  
-		ai->ai_canonname = h ? strdup(h->h_name) : NULL;
  1186
+		ai->ai_canonname = h ? xstrdup(h->h_name) : NULL;
1187 1187
 	else
1188 1188
 		ai->ai_canonname = NULL;
1189 1189
 
2  compat/qsort.c
@@ -55,7 +55,7 @@ void git_qsort(void *b, size_t n, size_t s,
55 55
 		msort_with_tmp(b, n, s, cmp, buf);
56 56
 	} else {
57 57
 		/* It's somewhat large, so malloc it.  */
58  
-		char *tmp = malloc(size);
  58
+		char *tmp = xmalloc(size);
59 59
 		msort_with_tmp(b, n, s, cmp, tmp);
60 60
 		free(tmp);
61 61
 	}
2  remote.c
@@ -840,7 +840,7 @@ char *apply_refspecs(struct refspec *refspecs, int nr_refspec,
840 840
 						    refspec->dst, &ret))
841 841
 				return ret;
842 842
 		} else if (!strcmp(refspec->src, name))
843  
-			return strdup(refspec->dst);
  843
+			return xstrdup(refspec->dst);
844 844
 	}
845 845
 	return NULL;
846 846
 }
2  show-index.c
@@ -48,7 +48,7 @@ int main(int argc, char **argv)
48 48
 			unsigned char sha1[20];
49 49
 			uint32_t crc;
50 50
 			uint32_t off;
51  
-		} *entries = malloc(nr * sizeof(entries[0]));
  51
+		} *entries = xmalloc(nr * sizeof(entries[0]));
52 52
 		for (i = 0; i < nr; i++)
53 53
 			if (fread(entries[i].sha1, 20, 1, stdin) != 1)
54 54
 				die("unable to read sha1 %u/%u", i, nr);
4  transport-helper.c
@@ -183,7 +183,7 @@ static struct child_process *get_helper(struct transport *transport)
183 183
 			ALLOC_GROW(refspecs,
184 184
 				   refspec_nr + 1,
185 185
 				   refspec_alloc);
186  
-			refspecs[refspec_nr++] = strdup(capname + strlen("refspec "));
  186
+			refspecs[refspec_nr++] = xstrdup(capname + strlen("refspec "));
187 187
 		} else if (!strcmp(capname, "connect")) {
188 188
 			data->connect = 1;
189 189
 		} else if (!prefixcmp(capname, "export-marks ")) {
@@ -445,7 +445,7 @@ static int fetch_with_import(struct transport *transport,
445 445
 		if (data->refspecs)
446 446
 			private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name);
447 447
 		else
448  
-			private = strdup(posn->name);
  448
+			private = xstrdup(posn->name);
449 449
 		read_ref(private, posn->old_sha1);
450 450
 		free(private);
451 451
 	}

0 notes on commit 040a655

Please sign in to comment.
Something went wrong with that request. Please try again.