Re: [PATCH 02/14] get_parent(): defensive programming
- From
Junio C Hamano <gitster@pobox.com>
- Date
- May 15, 2025, 17:21 UTC
- Message-ID
- <xmqq4ixl23jr.fsf@gitster.g>
- In-Reply-To
- <35c4870e2c101c9ef72d1657c8f6dd077cecc5fa.1747313140.git.gitgitgadget@gmail.com>
"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 21 quoted lines
> From: Johannes Schindelin <johannes.schindelin@gmx.de> > > CodeQL points out that `lookup_commit_reference()` can return NULL > values. > > Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> > --- > object-name.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/object-name.c b/object-name.c > index 76749fbfe652..ca54dad2f4c8 100644 > --- a/object-name.c > +++ b/object-name.c > @@ -1106,7 +1106,7 @@ static enum get_oid_result get_parent(struct repository *r, > if (ret) > return ret; > commit = lookup_commit_reference(r, &oid); > - if (repo_parse_commit(r, commit)) > + if (!commit || repo_parse_commit(r, commit)) > return MISSING_OBJECT;
Most of the time, the check for "ret" we see in the pre-context, which is a result of get_oid_1(), would prevent an oid that is not a valid name for a committish to even reach this code, I would think, but with possible repository corruption, we may fail to "lookup" the commit, so this is a good correction, I would think.
Thanks.
> if (!idx) {
> oidcpy(result, &commit->object.oid);