git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 4/4] git-compat-util.h: dietlibc-friendly x{malloc,realloc,calloc}

From
EWEric Wong <normalperson@yhbt.net>
Date
Dec 24, 2005, 21:15 UTC
Message-ID
<20051224211546.GG3963@mail.yhbt.net>
In-Reply-To
<7v3bkis631.fsf@assigned-by-dhcp.cox.net>
Junio C Hamano <junkio@cox.net> wrote:
Show 21 quoted lines
> Eric Wong <normalperson@yhbt.net> writes:
> 
> > dietlibc versions of these allocators returns NULL if a size of zero is
> > specified.  Obviously, this is a problem with the x* wrappers since
> > we check for them returning NULL.
> >
> > Down the line, it'd be better to hunt down and eliminate all calls to
> > these functions when they are called with a zero argument.  I've already
> > added some checks for these cases that were exposed by tests.
> 
> I agree that it is a bug to rely on *alloc(0) not returning
> NULL, but this patch is too risky.  It would be a good thing to
> have debugging variant of x* wrappers that barf on a 0-byte
> allocation request to find the offending callers, instead of
> returning NULL, maybe like the attached patch.
> 
> Since eradicating *alloc(0) calls is the right way to go, but it
> takes time.  Touching x* wrappers for general public should not
> be done before it is done.  It breaks things for everybody,
> while the current code is broken only for diet people and
> developers that use the debugging variant of x* wrappers.

I'll be using the following patch to collect results for the next few days without interrupting my work on other projects. I'll post patches to remove *alloc(0) calls when I find them.

diff --git a/git-compat-util.h b/git-compat-util.h
index 0c98c99..43be289 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -60,9 +60,41 @@ extern int gitsetenv(const char *, const
 extern char *gitstrcasestr(const char *haystack, const char *needle);
 #endif
 
+static void fork_for_core (const char * caller)
+{
+	pid_t child = fork();
+	if (child == 0) {
+		int i;
+		char out[4096];
+		char * progname = "unknown";
+		FILE * dbg = fopen("/var/tmp/git_alloc_dbg.log","a");
+
+		for(i = 0; environ[i]; i++) {
+			if (strstr(environ[i],"_=") == environ[i])
+				progname = environ[i];
+		}
+		
+		snprintf(out, 4096, "%s:%s: pid %d dumping core for pid %d\n",
+				progname, caller, getpid(), getppid());
+
+		fputs(out, stderr);
+		fputs(out, dbg);
+		fflush(NULL);
+		
+		abort();
+	}
+}
+
 static inline void *xmalloc(size_t size)
 {
-	void *ret = malloc(size);
+	void *ret;
+	
+	if (!size) {
+		fork_for_core(__func__);
+		return NULL;
+	}
+	
+	ret = malloc(size);
 	if (!ret)
 		die("Out of memory, malloc failed");
 	return ret;
@@ -70,7 +102,15 @@ static inline void *xmalloc(size_t size)
 
 static inline void *xrealloc(void *ptr, size_t size)
 {
-	void *ret = realloc(ptr, size);
+	void *ret;
+	
+	if (!size) {
+		fork_for_core(__func__);
+		free(ptr);
+		return NULL;
+	}
+	
+	ret = realloc(ptr, size);
 	if (!ret)
 		die("Out of memory, realloc failed");
 	return ret;
@@ -78,7 +118,14 @@ static inline void *xrealloc(void *ptr, 
 
 static inline void *xcalloc(size_t nmemb, size_t size)
 {
-	void *ret = calloc(nmemb, size);
+	void *ret;
+	
+	if (!nmemb || !size) {
+		fork_for_core(__func__);
+		return NULL;
+	}
+
+	ret = calloc(nmemb, size);
 	if (!ret)
 		die("Out of memory, calloc failed");
 	return ret;
-- 
Eric Wong
Previous: Junio C HamanoNext: Johannes Schindelin
Message 17 of 24 in “dietlibc compatibility”
  1. 0/4 dietlibc compatibilityEric Wong, Dec 24, 2005
  2. 1/4 git.c: extra #include for dietlibc (and possibly other C libraries)Eric Wong, Dec 24, 2005
  3. Junio C HamanoDec 26, 2005
  4. 2/4 short circuit out of a few places where we would allocate zero bytesEric Wong, Dec 24, 2005
  5. Johannes SchindelinDec 24, 2005
  6. H. Peter AnvinDec 28, 2005
  7. Linus TorvaldsDec 28, 2005
  8. Junio C HamanoDec 28, 2005
  9. H. Peter AnvinDec 28, 2005
  10. 3/4 add xmktime() function that always accounts for the TZ envEric Wong, Dec 24, 2005
  11. Johannes SchindelinDec 24, 2005
  12. Junio C HamanoDec 24, 2005
  13. Eric WongDec 24, 2005
  14. Junio C HamanoDec 24, 2005
  15. 4/4 git-compat-util.h: dietlibc-friendly x{malloc,realloc,calloc}Eric Wong, Dec 24, 2005
  16. Junio C HamanoDec 24, 2005
  17. Eric WongDec 24, 2005
  18. Avoid allocating 0 bytes, was Re: [PATCH 4/4] git-compat-util.h: dietlibc-friendly x{malloc,realloc,calloc}Johannes Schindelin, Dec 26, 2005
  19. Junio C HamanoDec 26, 2005
  20. Johannes SchindelinDec 26, 2005
  21. avoid asking ?alloc() for zero bytes.Junio C Hamano, Dec 26, 2005
  22. Johannes SchindelinDec 26, 2005
  23. Johannes SchindelinDec 28, 2005
  24. Eric WongDec 30, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.