From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
"Derrick Stolee" <derrickstolee@github.com>,
"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: [RFC PATCH 0/2] Alternate ab/valgrind-fixes fix-up
Date: Thu, 19 May 2022 22:09:15 +0200 [thread overview]
Message-ID: <RFC-cover-0.2-00000000000-20220519T195055Z-avarab@gmail.com> (raw)
In-Reply-To: <377be0e9-8a0f-4a86-0a66-3b08c0284dae@github.com>
On Mon, May 16 2022, Derrick Stolee wrote:
> On 5/12/2022 7:39 PM, Junio C Hamano wrote:
> [...]
> This switch statement was recently added to make it clear that
> unpack_loose_header() returns an enum value, not an int. This adds
> complications for future developers if that enum gains new values, since
> that developer would need to add a case statement to this switch for
> little real value.
>
> Instead, we can revert back to an 'if' statement, but make the enum
> explicit by using "!= ULHR_OK" instead of assuming it has the numerical
> value zero.
>
> Co-authored-by: Junio C Hamano <gitster@pobox.com>
> Signed-off-by: Derrick Stolee <derrickstolee@github.com>
> ---
>
> object-file.c | 8 ++------
> 1 file changed, 2 insertions(+), 6 deletions(-)
>
> diff --git a/object-file.c b/object-file.c
> index b5d1d12b68a..52e4ae1b5f0 100644
> --- a/object-file.c
> +++ b/object-file.c
> @@ -2623,12 +2623,8 @@ int read_loose_object(const char *path,
> goto out;
> }
>
> - switch (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr),
> - NULL)) {
> - case ULHR_OK:
> - break;
> - case ULHR_BAD:
> - case ULHR_TOO_LONG:
> + if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr),
> + NULL) != ULHR_OK) {
> error(_("unable to unpack header of %s"), path);
> goto out;
> }
This whole topic-at-large is a stylistic fix-up for a case where I
obviously got it wrong, so take this with a double grain of salt.
Re the "What's Cooking" mention of
ds/object-file-unpack-loose-header-fix: I don't mind it being merged
down at all. The below is all small potatoes.
I don't think the rationale ("adds complications for future
developers") makes sense in this case.
Let's leave aside the question of whether we exhaustively check enum
arms as in the pre-image, or check "not ok" as in the post-image.
Surely we can agree that whatever pattern is preferred we're better
off consistently picking one or the other?
I think this proposed change would make more sense and be in line with
its commit message if it also proposed this:
diff --git a/streaming.c b/streaming.c
index fe54665d86e..bb4ed198463 100644
--- a/streaming.c
+++ b/streaming.c
@@ -230,15 +230,10 @@ static int open_istream_loose(struct git_istream *st, struct repository *r,
st->u.loose.mapped = map_loose_object(r, oid, &st->u.loose.mapsize);
if (!st->u.loose.mapped)
return -1;
- switch (unpack_loose_header(&st->z, st->u.loose.mapped,
- st->u.loose.mapsize, st->u.loose.hdr,
- sizeof(st->u.loose.hdr), NULL)) {
- case ULHR_OK:
- break;
- case ULHR_BAD:
- case ULHR_TOO_LONG:
+ if (unpack_loose_header(&st->z, st->u.loose.mapped,
+ st->u.loose.mapsize, st->u.loose.hdr,
+ sizeof(st->u.loose.hdr), NULL) != ULHR_OK)
goto error;
- }
if (parse_loose_header(st->u.loose.hdr, &oi) < 0 || *type < 0)
goto error;
I.e. now we've converted the 2/3 callers of the API that only cared
about "not OK", there's a third one that cares about all the enum arms
currently, so that one remains a "switch".
The reason I think the rationale doesn't make sense is because of this
inconsistency. I.e. if we suppose a developer adds another enum value,
they'll then discover those three callers.
Surely whatever our preference for how to handle those 2/3 callers
it's less complicated if they don't use different patterns for no
obvious reason.
But anyway. Looking a bit deeper at this code again I think these two
patches are where we'd eventually want to head with this API. I.e. I
think the whole business of making this a tri-state return was
premature on my part.
After this RFC unpack_loose_header() is again a function that returns
a negative value on error, and the enum is gone. As noted in 2/2
there's a slight trade-off there, which I think is for the better,
both in terms of API simplicity, and in the new "error" output we'll
omit in these obscure cases. I.e.:
- error: header for $bogus_long_sha1 too long, exceeds 32 bytes
+ error: header too long, exceeds 32 bytes
+ error: unable to unpack $bogus_long_sha1 header
This whole "switch" complexity was because the old error message
wanted to note the OID in the "header too long" message.
Again, I'm perfectly fine with ds/object-file-unpack-loose-header-fix
advancing to "next", I can rebase this on top, or drop it depending on
the consensus about whether it's worthwile. I did want to un-block
that topic one way or the other, so to the extent that it was waiting
on my feedback...
Ævar Arnfjörð Bjarmason (2):
object-file API: fix obscure unpack_loose_header() return
object-file API: have unpack_loose_header() return "int" again
cache.h | 25 +++++-------------------
object-file.c | 46 +++++++++++++++++----------------------------
streaming.c | 11 +++--------
t/t1006-cat-file.sh | 6 ++++--
4 files changed, 29 insertions(+), 59 deletions(-)
--
2.36.1.957.g2c13267e09b
next prev parent reply other threads:[~2022-05-19 20:09 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-21 20:14 [PATCH 0/4] Fix issues and a regression noted by valgrind Ævar Arnfjörð Bjarmason
2022-04-21 20:14 ` [PATCH 1/4] tests: make RUNTIME_PREFIX compatible with --valgrind Ævar Arnfjörð Bjarmason
2022-04-21 22:22 ` Junio C Hamano
2022-04-21 20:14 ` [PATCH 2/4] log test: skip a failing mkstemp() test under valgrind Ævar Arnfjörð Bjarmason
2022-04-21 20:14 ` [PATCH 3/4] commit-graph.c: don't assume that stat() succeeds Ævar Arnfjörð Bjarmason
2022-04-21 22:29 ` Junio C Hamano
2022-04-21 20:14 ` [PATCH 4/4] object-file: fix a unpack_loose_header() regression in 3b6a8db3b03 Ævar Arnfjörð Bjarmason
2022-04-21 22:39 ` Junio C Hamano
2022-04-22 8:21 ` Ævar Arnfjörð Bjarmason
2022-05-12 22:32 ` [PATCH v2 0/4] test fixes around valgrind Junio C Hamano
2022-05-12 22:32 ` [PATCH v2 1/4] tests: using custom GIT_EXEC_PATH breaks --valgrind tests Junio C Hamano
2022-05-12 22:32 ` [PATCH v2 2/4] log test: skip a failing mkstemp() test under valgrind Junio C Hamano
2022-05-12 22:32 ` [PATCH v2 3/4] commit-graph.c: don't assume that stat() succeeds Junio C Hamano
2022-05-12 22:32 ` [PATCH v2 4/4] object-file: fix a unpack_loose_header() regression in 3b6a8db3b03 Junio C Hamano
2022-05-12 23:39 ` Junio C Hamano
2022-05-16 14:59 ` Derrick Stolee
2022-05-19 20:09 ` Ævar Arnfjörð Bjarmason [this message]
2022-05-19 20:09 ` [RFC PATCH 1/2] object-file API: fix obscure unpack_loose_header() return Ævar Arnfjörð Bjarmason
2022-05-19 20:09 ` [RFC PATCH 2/2] object-file API: have unpack_loose_header() return "int" again Ævar Arnfjörð Bjarmason
2022-05-20 4:27 ` Junio C Hamano
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=RFC-cover-0.2-00000000000-20220519T195055Z-avarab@gmail.com \
--to=avarab@gmail.com \
--cc=derrickstolee@github.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.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).