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

Re: git segfaults on older Solaris releases

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 7, 2016, 19:37 UTC
Message-ID
<xmqq8u0pyzu6.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20160407190709.GC4478@sigill.intra.peff.net>
Jeff King <peff@peff.net> writes:
Show 21 quoted lines
>> So perhaps this is all we need to fix your box.
>> 
>>  setup.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>> 
>> diff --git a/setup.c b/setup.c
>> index 3439ec6..b6c8aab 100644
>> --- a/setup.c
>> +++ b/setup.c
>> @@ -103,7 +103,7 @@ char *prefix_path_gently(const char *prefix, int len,
>>  			return NULL;
>>  		}
>>  	} else {
>> -		sanitized = xstrfmt("%.*s%s", len, prefix, path);
>> +		sanitized = xstrfmt("%.*s%s", len, prefix ? prefix : "", path);
>>  		if (remaining_prefix)
>>  			*remaining_prefix = len;
>>  		if (normalize_path_copy_len(sanitized, sanitized, remaining_prefix)) {
>
> The original pre-75faa45ae0230b321bf72027b2274315d7e14e34 version
> checked "if (len)", but I think this should be equally right.
That's a good point.  The original had
-               sanitized = xmalloc(len + strlen(path) + 1);
-               if (len)
-                       memcpy(sanitized, prefix, len);
-               strcpy(sanitized + len, path);
+               sanitized = xstrfmt("%.*s%s", len, prefix, path);

and for a brief moment I was wondering why the strlen() did not barf, until I realized that was on path not prefix.

-- >8 --
Subject: setup.c: do not feed NULL to "%.*s" even with the precision 0

A recent update 75faa45a (replace trivial malloc + sprintf / strcpy calls with xstrfmt, 2015-09-24) rewrote

	prepare an empty buffer
	if (len)
        	append the first len bytes of "prefix" to the buffer
	append "path" to the buffer
that computed "path", optionally prefixed by "prefix", into
	xstrfmt("%.*s%s", len, prefix, path);

However, passing a NULL pointer to the printf(3) family of functions to format it with %s conversion, even with the precision 0, i.e.

	xstrfmt("%.*s", 0, NULL)
yields undefined results, at least on some platforms.  

Avoid this problem by substituting prefix with "" when len==0, as prefix can legally be NULL in that case. This would mimick the intent of the original code better.

Reported-by: "Tom G. Christensen" <tgc@jupiterrise.com>
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 setup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/setup.c b/setup.c
index 3439ec6..b4a92fe 100644
--- a/setup.c
+++ b/setup.c
@@ -103,7 +103,7 @@ char *prefix_path_gently(const char *prefix, int len,
 			return NULL;
 		}
 	} else {
-		sanitized = xstrfmt("%.*s%s", len, prefix, path);
+		sanitized = xstrfmt("%.*s%s", len, len ? prefix : "", path);
 		if (remaining_prefix)
 			*remaining_prefix = len;
 		if (normalize_path_copy_len(sanitized, sanitized, remaining_prefix)) {
Previous: Jeff KingNext: Jeff King
Message 6 of 17 in “git segfaults on older Solaris releases”
  1. Tom G. ChristensenApr 7, 2016
  2. Junio C HamanoApr 7, 2016
  3. Junio C HamanoApr 7, 2016
  4. David TurnerApr 7, 2016
  5. Jeff KingApr 7, 2016
  6. Junio C HamanoApr 7, 2016
  7. Jeff KingApr 7, 2016
  8. Tom G. ChristensenApr 7, 2016
  9. Tom G. ChristensenApr 9, 2016
  10. Jeff KingApr 9, 2016
  11. 1/3 config: lower-case first word of error stringsJeff King, Apr 9, 2016
  12. 2/3 git_config_set_multivar_in_file: all non-zero returns are errorsJeff King, Apr 9, 2016
  13. 3/3 git_config_set_multivar_in_file: handle "unset" errorsJeff King, Apr 9, 2016
  14. Tom G. ChristensenApr 9, 2016
  15. Jeff KingApr 9, 2016
  16. Patrick SteinhardtApr 12, 2016
  17. Tom G. ChristensenApr 7, 2016

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.