threads / patch / 9552

patchFix an unitialized pointer in merge-recursive.c

Subject: [PATCH] Fix an unitialized pointer in merge-recursive.c

## tl;dr

7 messages between Aug 16, 2007 and Aug 16, 2007. Diffs are folded; open one to read it.

replies: 6people: 4as markdown or json

Marco Costalba· Aug 16, 2007, 08:00 UTC · lore

Indeed &mrtree is passed to merge_trees() that not always seems to set the value, so on some paths mrtree could return uninitialized.

Spotted by a gcc 4.2.1 warning
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
---
 merge-recursive.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Show changes to merge-recursive.c +1 −1
diff --git a/merge-recursive.c b/merge-recursive.c
index 16f6a0f..934b0d6 100644
--- a/merge-recursive.c
+++ b/merge-recursive.c
@@ -1610,7 +1610,7 @@ static int merge(struct commit
 {
  	struct commit_list *iter;
  	struct commit *merged_common_ancestors;
-	struct tree *mrtree;
+	struct tree *mrtree = NULL;
 	int clean;

 	if (show(4)) {
-- 
1.5.3.rc4.67.gf9286
Junio C Hamano· Aug 16, 2007, 08:08 UTC · re: Marco Costalba · lore

Re: [PATCH] Fix an unitialized pointer in merge-recursive.c

"Marco Costalba" <mcostalba@gmail.com> writes:
Show 5 quoted lines
> Indeed &mrtree is passed to merge_trees() that not always
> seems to set the value, so on some paths mrtree could
> return uninitialized.
>
> Spotted by a gcc 4.2.1 warning
Are you sure that gcc is correctly seeing the codeflow?

In merge(), mrtree is used only under index_only, and merge_trees() always sets *result under index_only.

Marco Costalba· Aug 16, 2007, 08:11 UTC · re: Junio C Hamano · lore

Re: [PATCH] Fix an unitialized pointer in merge-recursive.c

On 8/16/07, Junio C Hamano <gitster@pobox.com> wrote:
Show 13 quoted lines
> "Marco Costalba" <mcostalba@gmail.com> writes:
>
> > Indeed &mrtree is passed to merge_trees() that not always
> > seems to set the value, so on some paths mrtree could
> > return uninitialized.
> >
> > Spotted by a gcc 4.2.1 warning
>
> Are you sure that gcc is correctly seeing the codeflow?
>
> In merge(), mrtree is used only under index_only, and
> merge_trees() always sets *result under index_only.
>
Ok ;-)
Now two options:
- discard the patch
- change the title in 'silence a gcc bougus warning'
Junio C Hamano· Aug 16, 2007, 08:23 UTC · re: Marco Costalba · lore

Re: [PATCH] Fix an unitialized pointer in merge-recursive.c

"Marco Costalba" <mcostalba@gmail.com> writes:
Show 22 quoted lines
> On 8/16/07, Junio C Hamano <gitster@pobox.com> wrote:
>> "Marco Costalba" <mcostalba@gmail.com> writes:
>>
>> > Indeed &mrtree is passed to merge_trees() that not always
>> > seems to set the value, so on some paths mrtree could
>> > return uninitialized.
>> >
>> > Spotted by a gcc 4.2.1 warning
>>
>> Are you sure that gcc is correctly seeing the codeflow?
>>
>> In merge(), mrtree is used only under index_only, and
>> merge_trees() always sets *result under index_only.
>>
>
> Ok ;-)
>
> Now two options:
>
> - discard the patch
>
> - change the title in 'silence a gcc bougus warning'

Third option. Change the assignment from "mrtree = NULL" to "mrtree = mtree". It is a standard idiom to work around stupid gcc warnings.

Junio C Hamano· Aug 16, 2007, 08:30 UTC · re: Junio C Hamano · lore

Re: [PATCH] Fix an unitialized pointer in merge-recursive.c

Junio C Hamano <gitster@pobox.com> writes:
> Third option.  Change the assignment from "mrtree = NULL" to
> "mrtree = mtree".  It is a standard idiom to work around stupid
> gcc warnings.
Oops; it might be obvious but that's "mrtree = mrtree".
David Kastrup· Aug 16, 2007, 09:07 UTC · re: Junio C Hamano · lore

Re: [PATCH] Fix an unitialized pointer in merge-recursive.c

Junio C Hamano <gitster@pobox.com> writes:
Show 27 quoted lines
> "Marco Costalba" <mcostalba@gmail.com> writes:
>
>> On 8/16/07, Junio C Hamano <gitster@pobox.com> wrote:
>>> "Marco Costalba" <mcostalba@gmail.com> writes:
>>>
>>> > Indeed &mrtree is passed to merge_trees() that not always
>>> > seems to set the value, so on some paths mrtree could
>>> > return uninitialized.
>>> >
>>> > Spotted by a gcc 4.2.1 warning
>>>
>>> Are you sure that gcc is correctly seeing the codeflow?
>>>
>>> In merge(), mrtree is used only under index_only, and
>>> merge_trees() always sets *result under index_only.
>>
>> Ok ;-)
>>
>> Now two options:
>>
>> - discard the patch
>>
>> - change the title in 'silence a gcc bougus warning'
>
> Third option.  Change the assignment from "mrtree = NULL" to
> "mrtree = mtree".  It is a standard idiom to work around stupid
> gcc warnings.

I think it is more efficient to write mrtree = NULL: For the computer, it makes a minuscule difference, and it can save programmers a bit of confusion and worrying times. Time that may be better spent improving things elsewhere.

I don't think that we have a contest running for least redundancy in code layout, have we? Why then require the programmer to do a complete call trace analysis before he can feel comfortable about the code? And what if callers change at some point of time?

-- 
David Kastrup
Florian Weimer· Aug 16, 2007, 13:16 UTC · re: Junio C Hamano · lore

Re: [PATCH] Fix an unitialized pointer in merge-recursive.c

* Junio C. Hamano:
> Third option.  Change the assignment from "mrtree = NULL" to
> "mrtree = mtree".  It is a standard idiom to work around stupid
> gcc warnings.

Is it? It's also undefined according to the C standard. In other words, the compiler may assume that this particular declaration is never reached.

← back to recent threads