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

Re: [PATCH 1/3] parse_insn_line(): improve error message when parsing failed

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 17, 2020, 18:00 UTC
Message-ID
<xmqqmuamdkuv.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<2ae2e435b0ef6888e72defc7abee1909b29aa914.1579209506.git.gitgitgadget@gmail.com>

"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com> writes:

Show 23 quoted lines
> From: Johannes Schindelin <johannes.schindelin@gmx.de>
>
> In the case that a `get_oid()` call failed, we showed some rather bogus
> part of the line instead of the precise string we sent to said function.
> That makes it rather hard for users to understand what is going wrong,
> so let's fix that.
>
> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> ---
>  sequencer.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/sequencer.c b/sequencer.c
> index b9dbf1adb0..7c30dad59c 100644
> --- a/sequencer.c
> +++ b/sequencer.c
> @@ -2118,6 +2118,8 @@ static int parse_insn_line(struct repository *r, struct todo_item *item,
>  	saved = *end_of_object_name;
>  	*end_of_object_name = '\0';
>  	status = get_oid(bol, &commit_oid);
> +	if (status < 0)
> +		error(_("could not parse '%s'"), bol); /* return later */
>  	*end_of_object_name = saved;

OK, so after making sure the line begins a command that takes an object name, we first NUL terminated the token after the command but it turns out the string is not a valid oid. We show the part we thought was the object name before reverting the NUL termination.

Makes sense.  And then later...
Show 8 quoted lines
>  	bol = end_of_object_name + strspn(end_of_object_name, " \t");
> @@ -2125,11 +2127,10 @@ static int parse_insn_line(struct repository *r, struct todo_item *item,
>  	item->arg_len = (int)(eol - bol);
>  
>  	if (status < 0)
> -		return error(_("could not parse '%.*s'"),
> -			     (int)(end_of_object_name - bol), bol);
> +		return status;

...this is the *only* code that uses the status that was assigned to the return value of get_oid().

Because the implementation of this function (mis)uses "bol", which stands for "beginning of line", as a moving pointer of "beginning of the token we are looking at", the value of "bol" at this point of the control in the original code is not where the "bol" pointer used to be when end-of-object-name was computed (if it were, the '%.*s' argument would have been correct) and shows a token after where the object name would have been, which may help the user identify the line but does not directly show what token we had trouble with parsing.

And the updated code will avoid the issue by using the "bol" pointer when it is still pointing at the right place.

>  	item->commit = lookup_commit_reference(r, &commit_oid);
> -	return !item->commit;
> +	return item->commit ? 0 : -1;

This changes the polarity of the error exit from positive 1 to negative 1. The only caller of this function takes anything non-zero as a failure so this would not cause behaviour change, but returning negative is more in line with the practice so it is an improvement (it is unrelated to the theme of this patch and is not explained, though).

OK.
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin via GitGitGadget
Message 3 of 28 in “Re-fix rebase -i with SHA-1 collisions”
  1. 0/3 Re-fix rebase -i with SHA-1 collisionsJohannes Schindelin via GitGitGadget, Jan 16, 2020
  2. 1/3 parse_insn_line(): improve error message when parsing failedJohannes Schindelin via GitGitGadget, Jan 16, 2020
  3. Junio C HamanoJan 17, 2020
  4. 2/3 rebase -i: re-fix short SHA-1 collisionJohannes Schindelin via GitGitGadget, Jan 16, 2020
  5. Junio C HamanoJan 17, 2020
  6. Johannes SchindelinJan 17, 2020
  7. 3/3 rebase -i: also avoid SHA-1 collisions with missingCommitsCheckJohannes Schindelin via GitGitGadget, Jan 16, 2020
  8. brian m. carlsonJan 16, 2020
  9. Johannes SchindelinJan 17, 2020
  10. brian m. carlsonJan 17, 2020
  11. Junio C HamanoJan 21, 2020
  12. 0/3 Re-fix rebase -i with SHA-1 collisionsJohannes Schindelin via GitGitGadget, Jan 17, 2020
  13. 1/3 parse_insn_line(): improve error message when parsing failedJohannes Schindelin via GitGitGadget, Jan 17, 2020
  14. Junio C HamanoJan 21, 2020
  15. Johannes SchindelinJan 23, 2020
  16. 3/3 rebase -i: also avoid SHA-1 collisions with missingCommitsCheckJohannes Schindelin via GitGitGadget, Jan 17, 2020
  17. 2/3 rebase -i: re-fix short SHA-1 collisionJohannes Schindelin via GitGitGadget, Jan 17, 2020
  18. Eric SunshineJan 20, 2020
  19. Johannes SchindelinJan 20, 2020
  20. Eric SunshineJan 20, 2020
  21. Johannes SchindelinJan 21, 2020
  22. Junio C HamanoJan 21, 2020
  23. Johannes SchindelinJan 22, 2020
  24. Junio C HamanoJan 22, 2020
  25. 0/3 Re-fix rebase -i with SHA-1 collisionsJohannes Schindelin via GitGitGadget, Jan 23, 2020
  26. 1/3 parse_insn_line(): improve error message when parsing failedJohannes Schindelin via GitGitGadget, Jan 23, 2020
  27. 2/3 rebase -i: re-fix short SHA-1 collisionJohannes Schindelin via GitGitGadget, Jan 23, 2020
  28. 3/3 rebase -i: also avoid SHA-1 collisions with missingCommitsCheckJohannes Schindelin via GitGitGadget, Jan 23, 2020

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.