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

Re: [PATCH 1/2] notes: clean up confusing NULL checks in init_notes()

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 24, 2023, 18:05 UTC
Message-ID
<xmqqttx5duki.fsf@gitster.g>
In-Reply-To
<20230422135543.GA3942829@coredump.intra.peff.net>
Jeff King <peff@peff.net> writes:
Show 13 quoted lines
> Coverity complains that we check whether "notes_ref" is NULL, but it was
> already implied to be non-NULL earlier in the function. And this is
> true; since b9342b3fd63 (refs: add array of ref namespaces, 2022-08-05),
> we call xstrdup(notes_ref) unconditionally, which would segfault if it
> was NULL.
>
> But that commit is actually doing the right thing. Even if NULL is
> passed into the function, we'll use default_notes_ref() as a fallback,
> which will never return NULL (it tries a few options, but its last
> resort is a string literal). Ironically, the "!notes_ref" check was
> added by the same commit that added the fallback: 709f79b0894 (Notes
> API: init_notes(): Initialize the notes tree from the given notes ref,
> 2010-02-13). So this check never did anything.

I am impressed(?) that Coverity can complain at the "_or_null" part in xstrdup_or_null().

It all makes sense.  Thanks.
Show 25 quoted lines
> Signed-off-by: Jeff King <peff@peff.net>
> ---
>  notes.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/notes.c b/notes.c
> index 45fb7f22d1..cadb435056 100644
> --- a/notes.c
> +++ b/notes.c
> @@ -1019,13 +1019,13 @@ void init_notes(struct notes_tree *t, const char *notes_ref,
>  	t->root = (struct int_node *) xcalloc(1, sizeof(struct int_node));
>  	t->first_non_note = NULL;
>  	t->prev_non_note = NULL;
> -	t->ref = xstrdup_or_null(notes_ref);
> +	t->ref = xstrdup(notes_ref);
>  	t->update_ref = (flags & NOTES_INIT_WRITABLE) ? t->ref : NULL;
>  	t->combine_notes = combine_notes;
>  	t->initialized = 1;
>  	t->dirty = 0;
>  
> -	if (flags & NOTES_INIT_EMPTY || !notes_ref ||
> +	if (flags & NOTES_INIT_EMPTY ||
>  	    repo_get_oid_treeish(the_repository, notes_ref, &object_oid))
>  		return;
>  	if (flags & NOTES_INIT_WRITABLE && read_ref(notes_ref, &object_oid))
Previous: Jeff KingNext: Jeff King
Message 3 of 5 in “coverity null-check fixes”
  1. 0/2 coverity null-check fixesJeff King, Apr 22, 2023
  2. 1/2 notes: clean up confusing NULL checks in init_notes()Jeff King, Apr 22, 2023
  3. Junio C HamanoApr 24, 2023
  4. Jeff KingApr 24, 2023
  5. 2/2 fetch_bundle_uri(): drop pointless NULL checkJeff King, Apr 22, 2023

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.