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

Re: [PATCH] fixup! bisect--helper: `bisect_next_check` & bisect_voc shell function in C

From
René Scharfe <l.s.r@web.de>
Date
Feb 13, 2017, 18:27 UTC
Message-ID
<74dfcffe-274c-7045-420a-95612394d66b@web.de>
In-Reply-To
<alpine.DEB.2.20.1702131722350.3496@virtualbox>
Am 13.02.2017 um 17:23 schrieb Johannes Schindelin:
Show 41 quoted lines
> Hi René,
> 
> On Fri, 10 Feb 2017, René Scharfe wrote:
> 
>> Am 10.02.2017 um 15:20 schrieb Johannes Schindelin:
>>> It is curious that only MacOSX builds trigger an error about this, both
>>> GCC and Clang, but not Linux GCC nor Clang (see
>>> https://travis-ci.org/git/git/jobs/200182819#L1152 for details):
>>>
>>> builtin/bisect--helper.c:299:6: error: variable 'good_syn' is used
>>>   uninitialized whenever 'if' condition is true
>>>   [-Werror,-Wsometimes-uninitialized]
>>>         if (missing_good && !missing_bad && current_term &&
>>>             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> builtin/bisect--helper.c:350:7: note: uninitialized use occurs here
>>>         if (!good_syn)
>>>              ^~~~~~~~
>>
>> The only way that good_syn could be used in the if block is by going to the
>> label finish, which does the following before returning:
>>
>> 	if (!bad_ref)
>> 		free(bad_ref);
>> 	if (!good_glob)
>> 		free(good_glob);
>> 	if (!bad_syn)
>> 		free(bad_syn);
>> 	if (!good_syn)
>> 		free(good_syn);
>>
>> On Linux that code is elided completely -- freeing NULL is a no-op.  I guess
>> free(3) has different attributes on OS X and compilers don't dare to optimize
>> it away there.
>>
>> So instead of calling free(3) only in the case when we did not allocate memory
>> (which makes no sense and leaks) we should either call it in the opposite
>> case, or (preferred) unconditionally, as it can handle the NULL case itself.
>> Once that's fixed initialization will be required even on Linux.
> 
> Exactly, free(NULL) is a no-op. The problem before this fixup was that
> good_syn was not initialized to NULL.

Strictly speaking: no. The value doesn't matter -- the free(3) calls above will be done with NULL regardless, due to the conditionals. Setting bad_syn and good_syn to an invalid pointer would have calmed the compiler just as well, and would have had no ill side effect (i.e. no invalid free(3) call).

Initializing to NULL is still the correct thing to do, of course -- together with removing the conditionals (or at least the negations).

But back to the topic of why the compilers on OS X didn't optimize out the free(3) calls with their conditionals. AFAICS no attributes are set for the function in stdlib.h of in glibc[1] or Darwin[2]. And I can't see any relevant option in config.mak.uname (e.g. -no-builtin). It's not terribly important, but does anyone know what prevents the elision of "if (!p) free(p);" on OS X?

René

[1] https://sourceware.org/git/?p=glibc.git;a=blob;f=stdlib/stdlib.h;h=292c6a2f053a2a578cd09d75307c26ed191e1c00;hb=b987917e6aa7ffe2fd74f0b6a989438e6edd0727 [2] https://opensource.apple.com/source/Libc/Libc-1158.30.7/include/stdlib.h.auto.html

Previous: Johannes SchindelinNext: Junio C Hamano
Message 5 of 8 in “fixup! bisect--helper: `bisect_next_check` & bisect_voc shell function in C”
  1. fixup! bisect--helper: `bisect_next_check` & bisect_voc shell function in CJohannes Schindelin, Feb 10, 2017
  2. Pranit BauvaFeb 10, 2017
  3. René ScharfeFeb 10, 2017
  4. Johannes SchindelinFeb 13, 2017
  5. René ScharfeFeb 13, 2017
  6. Junio C HamanoFeb 13, 2017
  7. Pranit BauvaFeb 13, 2017
  8. Junio C HamanoFeb 19, 2017

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.