threads / patch / 30404

patchgit cherry-pick: Add NULL check to sequencer parsing of HEAD

Subject: [PATCH] git cherry-pick: Add NULL check to sequencer parsing of HEAD

## tl;dr

9 messages between May 3, 2012 and May 3, 2012. Diffs are folded; open one to read it.

replies: 8people: 4as markdown or json

Neil Horman· May 3, 2012, 11:20 UTC · lore

Michael Mueller noted that a feature I recently added failed to check the return of lookup_commit to ensure that it was not NULL. I don't think a NULL can actually happen in the this particular use case, but regardless it seems a good idea to check.

Signed-off-by: Neil Horman <nhorman@tuxdriver.com>
---
 sequencer.c |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
Show changes to sequencer.c +10 −1
diff --git a/sequencer.c b/sequencer.c
index f83cdfd..ad4d781 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -261,7 +261,16 @@ static int is_index_unchanged(void)
 		return error(_("Could not resolve HEAD commit\n"));
 
 	head_commit = lookup_commit(head_sha1);
-	if (!head_commit || parse_commit(head_commit))
+
+	/*
+	 * If head_commit is NULL, just return, as check_commit,
+	 * called from lookup_commit, would have indicated that
+	 * head_commit is not a commit object already.
+	 */
+	if (!head_commit)
+		return;
+
+	if (parse_commit(head_commit))
 		return error(_("could not parse commit %s\n"),
 			     sha1_to_hex(head_commit->object.sha1));
 
-- 
1.7.7.6
René Scharfe· May 3, 2012, 11:45 UTC · re: Neil Horman · lore

Re: [PATCH] git cherry-pick: Add NULL check to sequencer parsing of HEAD

Am 03.05.2012 13:20, schrieb Neil Horman:
Show 27 quoted lines
> Michael Mueller noted that a feature I recently added failed to check the return
> of lookup_commit to ensure that it was not NULL.  I don't think a NULL can
> actually happen in the this particular use case, but regardless it seems a good
> idea to check.
>
> Signed-off-by: Neil Horman <nhorman@tuxdriver.com>
> ---
>   sequencer.c |   11 ++++++++++-
>   1 files changed, 10 insertions(+), 1 deletions(-)
>
> diff --git a/sequencer.c b/sequencer.c
> index f83cdfd..ad4d781 100644
> --- a/sequencer.c
> +++ b/sequencer.c
> @@ -261,7 +261,16 @@ static int is_index_unchanged(void)
>   		return error(_("Could not resolve HEAD commit\n"));
>
>   	head_commit = lookup_commit(head_sha1);
> -	if (!head_commit || parse_commit(head_commit))
> +
> +	/*
> +	 * If head_commit is NULL, just return, as check_commit,
> +	 * called from lookup_commit, would have indicated that
> +	 * head_commit is not a commit object already.
> +	 */
> +	if (!head_commit)
> +		return;
A return value is missing.  Perhaps -1?
> +
> +	if (parse_commit(head_commit))
>   		return error(_("could not parse commit %s\n"),
>   			     sha1_to_hex(head_commit->object.sha1));
Note: parse_commit() can handle NULL, and it already reports error 
details itself.
René
Neil Horman· May 3, 2012, 12:08 UTC · re: René Scharfe · lore

Re: [PATCH] git cherry-pick: Add NULL check to sequencer parsing of HEAD

On Thu, May 03, 2012 at 01:45:54PM +0200, René Scharfe wrote:
Show 31 quoted lines
> Am 03.05.2012 13:20, schrieb Neil Horman:
> >Michael Mueller noted that a feature I recently added failed to check the return
> >of lookup_commit to ensure that it was not NULL.  I don't think a NULL can
> >actually happen in the this particular use case, but regardless it seems a good
> >idea to check.
> >
> >Signed-off-by: Neil Horman <nhorman@tuxdriver.com>
> >---
> >  sequencer.c |   11 ++++++++++-
> >  1 files changed, 10 insertions(+), 1 deletions(-)
> >
> >diff --git a/sequencer.c b/sequencer.c
> >index f83cdfd..ad4d781 100644
> >--- a/sequencer.c
> >+++ b/sequencer.c
> >@@ -261,7 +261,16 @@ static int is_index_unchanged(void)
> >  		return error(_("Could not resolve HEAD commit\n"));
> >
> >  	head_commit = lookup_commit(head_sha1);
> >-	if (!head_commit || parse_commit(head_commit))
> >+
> >+	/*
> >+	 * If head_commit is NULL, just return, as check_commit,
> >+	 * called from lookup_commit, would have indicated that
> >+	 * head_commit is not a commit object already.
> >+	 */
> >+	if (!head_commit)
> >+		return;
> 
> A return value is missing.  Perhaps -1?
> 
Yeah, sorry, not sure how I missed the compiler warning.
Show 7 quoted lines
> >+
> >+	if (parse_commit(head_commit))
> >  		return error(_("could not parse commit %s\n"),
> >  			     sha1_to_hex(head_commit->object.sha1));
> 
> Note: parse_commit() can handle NULL, and it already reports error
> details itself.

No, it doesn't. parse_commit checks NULL already, true, but it just returns -1. No error message is provided to the user Neil

> 
> René
> 
René Scharfe· May 3, 2012, 12:26 UTC · re: Neil Horman · lore

Re: [PATCH] git cherry-pick: Add NULL check to sequencer parsing of HEAD

Am 03.05.2012 14:08, schrieb Neil Horman:
Show 8 quoted lines
>>> +	if (parse_commit(head_commit))
>>>   		return error(_("could not parse commit %s\n"),
>>>   			     sha1_to_hex(head_commit->object.sha1));
>>
>> Note: parse_commit() can handle NULL, and it already reports error
>> details itself.
> No, it doesn't.  parse_commit checks NULL already, true, but it just returns -1.
> No error message is provided to the user
Sorry, was too terse again: It handles NULL, without printing anything. 
  And it reports details for (other) errors.  So you could return -1 if 
parse_commit() returns non-zero and be done with it.  Just saying.
René
Neil Horman· May 3, 2012, 12:10 UTC · re: Neil Horman · lore

[PATCH v2] git cherry-pick: Add NULL check to sequencer parsing of HEAD

Michael Mueller noted that a feature I recently added failed to check the return of lookup_commit to ensure that it was not NULL. I don't think a NULL can actually happen in the this particular use case, but regardless it seems a good idea to check.

Signed-off-by: Neil Horman <nhorman@tuxdriver.com>
---
 sequencer.c |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
Show changes to sequencer.c +10 −1
diff --git a/sequencer.c b/sequencer.c
index f83cdfd..ded0b76 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -261,7 +261,16 @@ static int is_index_unchanged(void)
 		return error(_("Could not resolve HEAD commit\n"));
 
 	head_commit = lookup_commit(head_sha1);
-	if (!head_commit || parse_commit(head_commit))
+
+	/*
+	 * If head_commit is NULL, just return, as check_commit,
+	 * called from lookup_commit, would have indicated that
+	 * head_commit is not a commit object already.
+	 */
+	if (!head_commit)
+		return -1;
+
+	if (parse_commit(head_commit))
 		return error(_("could not parse commit %s\n"),
 			     sha1_to_hex(head_commit->object.sha1));
 
-- 
1.7.7.6
Neil Horman· May 3, 2012, 14:09 UTC · re: Neil Horman · lore

[PATCH v3] git cherry-pick: Add NULL check to sequencer parsing of HEAD

Michael Mueller noted that a feature I recently added failed to check the return of lookup_commit to ensure that it was not NULL. I don't think a NULL can actually happen in the this particular use case, but regardless it seems a good idea to check.

Signed-off-by: Neil Horman <nhorman@tuxdriver.com>
---
 sequencer.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
Show changes to sequencer.c +3 −3
diff --git a/sequencer.c b/sequencer.c
index f83cdfd..f7eac1d 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -261,9 +261,9 @@ static int is_index_unchanged(void)
 		return error(_("Could not resolve HEAD commit\n"));
 
 	head_commit = lookup_commit(head_sha1);
-	if (!head_commit || parse_commit(head_commit))
-		return error(_("could not parse commit %s\n"),
-			     sha1_to_hex(head_commit->object.sha1));
+
+	if (parse_commit(head_commit))
+		return -1;
 
 	if (!active_cache_tree)
 		active_cache_tree = cache_tree();
-- 
1.7.7.6
Matthieu Moy· May 3, 2012, 16:48 UTC · re: Neil Horman · lore

Re: [PATCH v3] git cherry-pick: Add NULL check to sequencer parsing of HEAD

Neil Horman <nhorman@tuxdriver.com> writes:
Show 6 quoted lines
> -	if (!head_commit || parse_commit(head_commit))
> -		return error(_("could not parse commit %s\n"),
> -			     sha1_to_hex(head_commit->object.sha1));
> +
> +	if (parse_commit(head_commit))
> +		return -1;

Why did you replace the error("...") with only a -1? error() also returns -1, but displays a message before, which I think was fine. If you want to remove the message, then explain why in the commit message.

If you do not test for head_commit to be null, you can't use it in the error message. But from the context, it seems you can use head_sha1. If not, a message like "Could not parse HEAD commit" seems better than nothing.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Junio C Hamano· May 3, 2012, 17:13 UTC · re: Matthieu Moy · lore

Re: [PATCH v3] git cherry-pick: Add NULL check to sequencer parsing of HEAD

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
Show 17 quoted lines
> Neil Horman <nhorman@tuxdriver.com> writes:
>
>> -	if (!head_commit || parse_commit(head_commit))
>> -		return error(_("could not parse commit %s\n"),
>> -			     sha1_to_hex(head_commit->object.sha1));
>> +
>> +	if (parse_commit(head_commit))
>> +		return -1;
>
> Why did you replace the error("...") with only a -1? error() also
> returns -1, but displays a message before, which I think was fine. If
> you want to remove the message, then explain why in the commit message.
>
> If you do not test for head_commit to be null, you can't use it in the
> error message. But from the context, it seems you can use head_sha1. If
> not, a message like "Could not parse HEAD commit" seems better than
> nothing.
Yeah, I think v2 in this series is the appropriate fix.
Neil Horman· May 3, 2012, 17:34 UTC · lore

Re: [PATCH v3] git cherry-pick: Add NULL check to sequencer parsing of HEAD

On Thu, May 03, 2012 at 09:56:07AM -0700, Junio C Hamano wrote:
Show 43 quoted lines
> Neil Horman <nhorman@tuxdriver.com> writes:
> 
> > Michael Mueller noted that a feature I recently added failed to check the return
> > of lookup_commit to ensure that it was not NULL.  I don't think a NULL can
> > actually happen in the this particular use case, but regardless it seems a good
> > idea to check.
> >
> > Signed-off-by: Neil Horman <nhorman@tuxdriver.com>
> 
> Make a mental note here to remember what we just read above: Earlier code
> was missing a check for NULL and the patch should be about adding a new
> check.
> 
> >  sequencer.c |    6 +++---
> >  1 files changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/sequencer.c b/sequencer.c
> > index f83cdfd..f7eac1d 100644
> > --- a/sequencer.c
> > +++ b/sequencer.c
> > @@ -261,9 +261,9 @@ static int is_index_unchanged(void)
> >  		return error(_("Could not resolve HEAD commit\n"));
> >  
> >  	head_commit = lookup_commit(head_sha1);
> > -	if (!head_commit || parse_commit(head_commit))
> > -		return error(_("could not parse commit %s\n"),
> > -			     sha1_to_hex(head_commit->object.sha1));
> > +
> > +	if (parse_commit(head_commit))
> > +		return -1;
> 
> Whoa?  This patch is not about adding any new check.  It removes
> conditions from if clause and removes an error message.
> 
> What does that mean?  6 months down the road, when you read this commit,
> you will be very confused.  The resulting code may be correct, but the
> explanation is way off.  Perhaps explain it like the attached?
> 
> Having said that, if you had HEAD that is corrupt (perhaps filesystem
> corruption), you *WILL* get NULL in head_commit, and with the updated code
> you won't issue any error message from parse_commit(), so I do not think
> the patched result is entirely correct.
> 

See check_commit, as called from lookup_commit, it issues a user visible error message as part of its parsing.

Show 8 quoted lines
> -- >8 --
> Subject: [PATCH] git cherry-pick: remove bogus error message generation
> 
> The code to issue an error message tried to access the pointer head_commit
> that is potentially NULL.  Just calling parse_commit() will give us the
> necessary "is the commit object valid?" check and issue an error message,
> so we do not need an error message here.
> 

This seems reasonable to me Neil

← back to recent threads