git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Jeff King <peff@peff.net>
Cc: Romain Chossart <romainchossart@gmail.com>, git@vger.kernel.org
Subject: Re: [PATCH] diff: handle negative status in diff_result_code()
Date: Mon, 21 Aug 2023 08:56:26 -0700	[thread overview]
Message-ID: <xmqqlee4s82d.fsf@gitster.g> (raw)
In-Reply-To: <20230821003532.GA1113755@coredump.intra.peff.net> (Jeff King's message of "Sun, 20 Aug 2023 20:35:32 -0400")

Jeff King <peff@peff.net> writes:

> Thanks for your report. And I'm impressed you managed to find such an
> ancient bug. :)

Indeed.  Thanks, both.

> Most programs which run a diff (porcelain git-diff, diff-tree, etc) run
> their result through diff_result_code() before presenting it to the user
> as a program return code. That result generally comes from a library
> function like builtin_diff_files(), etc, and may be "-1" if the function
> bailed with an error.
>
>
> There are two problems here:
>
>   - if --exit-code is not in use, then we pass the code along as-is.
>     Even though Unix exit codes are unsigned, this usually works OK
>     because the integer representation, and "-1" ends up as "255". But
>     it's not something we should rely on, and we've tried to avoid it
>     elsewhere. E.g. in 5391e94813 (branch: remove negative exit code,
>     2022-03-29) and 246f0edec0 (execv_dashed_external: stop exiting with
>     negative code, 2017-01-06) and probably others.
>
>   - when --exit-code is in use, we ignore the incoming "status" variable
>     entirely, and rely on the "has_changes" field. But if we saw an
>     error, this field is almost certainly invalid, and means we'd likely
>     just say "no changes", which is completely bogus. Likewise for
>     the "--check" format.

Inspecting some callers of diff_result_code() further finds
curiosities.  wt-status.c for example feeds results form
run_diff_index() and run_diff_files() to the function, neither of
which returns anything other than 0. They die or exit(128)
themselves, though, so they are OK without this fix.
builtin/describe.c is also in the same boat with its use of
run_diff_index().

But of course the presense of such codepaths does not invalidate the
fix in your patch.

> So let's intercept the negative value here and return an appropriate
> code before even considering --exit-code, etc. And while doing so, we
> can swap out the negative value for a more normal exit code.

Good.  As you said, this is not an ungent regression fix, but I'd
prefer to see us look at all the current callers, as I wonder if
there are positive non-zero numbers coming from some callers, and
possibly design how this code should react to such "result" coming
in.

Again, thanks, both.

  reply	other threads:[~2023-08-21 15:56 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-20 19:52 "git diff --no-pager --exit-code" errors out but returns zero exit code Romain Chossart
2023-08-21  0:35 ` [PATCH] diff: handle negative status in diff_result_code() Jeff King
2023-08-21 15:56   ` Junio C Hamano [this message]
2023-08-21 16:21     ` [PATCH] diff: spell DIFF_INDEX_CACHED out when calling run_diff_index() Junio C Hamano
2023-08-21 18:36       ` Jeff King
2023-08-21 22:08         ` Junio C Hamano
2023-08-21 18:09     ` [PATCH] diff: handle negative status in diff_result_code() Jeff King
2023-08-21 18:39       ` Junio C Hamano
2023-08-21 20:13         ` [PATCH v2 0/7] cleaning up diff_result_code() Jeff King
2023-08-21 20:14           ` [PATCH v2 1/7] diff: spell DIFF_INDEX_CACHED out when calling run_diff_index() Jeff King
2023-08-21 20:15           ` [PATCH v2 2/7] diff-files: avoid negative exit value Jeff King
2023-08-21 20:16           ` [PATCH v2 3/7] diff: show usage for unknown builtin_diff_files() options Jeff King
2023-08-21 20:17           ` [PATCH v2 4/7] diff: die when failing to read index in git-diff builtin Jeff King
2023-08-22 23:27             ` Junio C Hamano
2023-08-21 20:18           ` [PATCH v2 5/7] diff: drop useless return from run_diff_{files,index} functions Jeff King
2023-08-21 20:19           ` [PATCH v2 6/7] diff: drop useless return values in git-diff helpers Jeff King
2023-08-21 20:20           ` [PATCH v2 7/7] diff: drop useless "status" parameter from diff_result_code() Jeff King
2023-08-22 23:38             ` Junio C Hamano
2023-08-23 19:00               ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqlee4s82d.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=peff@peff.net \
    --cc=romainchossart@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).