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: git@vger.kernel.org,  Taylor Blau <me@ttaylorr.com>
Subject: Re: [PATCH 09/10] unpack_loose_rest(): simplify error handling
Date: Wed, 26 Feb 2025 05:46:04 -0800	[thread overview]
Message-ID: <xmqqldtsg6oz.fsf@gitster.g> (raw)
In-Reply-To: <20250225063351.GI1293961@coredump.intra.peff.net> (Jeff King's message of "Tue, 25 Feb 2025 01:33:51 -0500")

Jeff King <peff@peff.net> writes:

> Inflating a loose object is considered successful only if we got
> Z_STREAM_END and there were no more bytes. We check both of those
> conditions and return success, but then have to check them a second time
> to decide which error message to produce.
>
> I.e., we do something like this:
>
>   if (!error_1 && !error_2)
>           ...return success...
>
>   if (error_1)
>           ...handle error1...
>   else if (error_2)
>           ...handle error2...
>   ...common error handling...
>
> This repetition was the source of a small bug fixed in an earlier commit
> (our Z_STREAM_END check was not the same in the two conditionals).
>
> Instead we can chain them all into a single if/else cascade, which
> avoids repeating ourselves:
>
>   if (error_1)
>           ...handle error1...
>   else if (error_2)
>           ...handle error2....
>   else
>           ...return success...
>   ...common error handling...
>
> Signed-off-by: Jeff King <peff@peff.net>
> ---
>  object-file.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)

Of course the resulting code is so much cleaner and more obvious.
Thanks for cleaning it up.


> diff --git a/object-file.c b/object-file.c
> index 8cf87caef5..b7928fb74e 100644
> --- a/object-file.c
> +++ b/object-file.c
> @@ -1436,15 +1436,15 @@ static void *unpack_loose_rest(git_zstream *stream,
>  			obj_read_lock();
>  		}
>  	}
> -	if (status == Z_STREAM_END && !stream->avail_in) {
> -		return buf;
> -	}
>  
>  	if (status != Z_STREAM_END)
>  		error(_("corrupt loose object '%s'"), oid_to_hex(oid));
>  	else if (stream->avail_in)
>  		error(_("garbage at end of loose object '%s'"),
>  		      oid_to_hex(oid));
> +	else
> +		return buf;
> +
>  	free(buf);
>  	return NULL;
>  }

  reply	other threads:[~2025-02-26 13:46 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-25  6:25 [PATCH 0/10] some zlib inflating bug fixes Jeff King
2025-02-25  6:28 ` [PATCH 01/10] loose_object_info(): BUG() on inflating content with unknown type Jeff King
2025-02-25 11:42   ` Patrick Steinhardt
2025-02-26  1:47   ` Junio C Hamano
2025-02-28  0:16     ` Taylor Blau
2025-03-04  6:43       ` Jeff King
2025-03-04 15:41         ` Junio C Hamano
2025-02-28  0:14   ` Taylor Blau
2025-02-25  6:29 ` [PATCH 02/10] unpack_loose_header(): simplify next_out assignment Jeff King
2025-02-28  0:18   ` Taylor Blau
2025-02-25  6:29 ` [PATCH 03/10] unpack_loose_header(): report headers without NUL as "bad" Jeff King
2025-02-25  6:29 ` [PATCH 04/10] unpack_loose_header(): fix infinite loop on broken zlib input Jeff King
2025-02-25 11:42   ` Patrick Steinhardt
2025-02-25 19:00     ` Eric Sunshine
2025-02-26 12:56   ` Junio C Hamano
2025-02-28  0:21   ` Taylor Blau
2025-02-25  6:30 ` [PATCH 05/10] git_inflate(): skip zlib_post_call() sanity check on Z_NEED_DICT Jeff King
2025-02-26 13:26   ` Junio C Hamano
2025-02-28  0:31     ` Taylor Blau
2025-03-04  7:08       ` Jeff King
2025-02-25  6:30 ` [PATCH 06/10] unpack_loose_header(): avoid numeric comparison of zlib status Jeff King
2025-02-28  0:32   ` Taylor Blau
2025-03-04  6:55     ` Jeff King
2025-02-25  6:31 ` [PATCH 07/10] unpack_loose_rest(): " Jeff King
2025-02-25  6:33 ` [PATCH 08/10] unpack_loose_rest(): never clean up zstream Jeff King
2025-02-26 13:16   ` Junio C Hamano
2025-02-25  6:33 ` [PATCH 09/10] unpack_loose_rest(): simplify error handling Jeff King
2025-02-26 13:46   ` Junio C Hamano [this message]
2025-02-28  0:34   ` Taylor Blau
2025-02-25  6:34 ` [PATCH 10/10] unpack_loose_rest(): rewrite return handling for clarity Jeff King
2025-02-28  0:36   ` Taylor Blau
2025-03-04  7:10     ` Jeff King
2025-03-04 21:32       ` Taylor Blau
2025-02-28  0:38 ` [PATCH 0/10] some zlib inflating bug fixes Taylor Blau

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=xmqqldtsg6oz.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=me@ttaylorr.com \
    --cc=peff@peff.net \
    /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).