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

Re: [PATCH v2 1/3] refs: keep track of unresolved reference value in iterator

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 8, 2024, 23:02 UTC
Message-ID
<xmqq34rv791a.fsf@gitster.g>
In-Reply-To
<6adc9dd26da4459d246591ce148c960b33bde336.1712597893.git.gitgitgadget@gmail.com>
Show 16 quoted lines
> diff --git a/refs.h b/refs.h
> index 298caf6c618..2e740c692ac 100644
> --- a/refs.h
> +++ b/refs.h
> @@ -71,9 +71,10 @@ struct pack_refs_opts {
>  	struct ref_exclusions *exclusions;
>  	struct string_list *includes;
>  };
> -
>  const char *refs_resolve_ref_unsafe(struct ref_store *refs,
> +
>  				    const char *refname,
> +				    char **referent,
>  				    int resolve_flags,
>  				    struct object_id *oid,
>  				    int *flags);

If referent is meant to be an out-parameter, it should sit next to oid that is also an out-parameter. And as a late-comer sibling, it should sit after its elder brother.

Also, I do not see the reason for the shuffling of blank lines. Shouldn't it be the other way around, even? After an unrelated definition of "struct pack_refs_opts", there is (and should be) a blank line, then the first line of the declaration of function.

Perhaps some fat-fingering.
Show 14 quoted lines
> @@ -1928,6 +1928,7 @@ int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
>  
>  const char *refs_resolve_ref_unsafe(struct ref_store *refs,
>  				    const char *refname,
> +				    char **referent,
>  				    int resolve_flags,
>  				    struct object_id *oid,
>  				    int *flags)
> @@ -1989,6 +1990,8 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
>  		}
>  
>  		*flags |= read_flags;
> +		if (referent && read_flags & REF_ISSYMREF && sb_refname.len > 0)
> +			*referent = sb_refname.buf;

Is this safe? After this assignment, which "return" in this loop are you expecting to return from this function? If you fail to return from the function during this iteration, you'll clobber the same strbuf with the next refs_read_raw_ref(), but I do not see how you are ensuring that you'll return from the function without such corruption happening.

This assignment happens only when read_flags has REF_ISSYMREF set, so the next "if it is not, then return refname" would not even trigger. If RESOLVE_REF_NO_RECURSE bit is on in resolve_flags, then we'd return without further dereferencing, but if that is the only safe exit from the function, shouldn't you be guarding the function with something like

	if (referent && !(resolve_flags & RESOLVE_REF_NO_RECURSE))
		BUG("recursive dereference can will clobber *referent");
to protect its callers from their own mistakes?

Another return before we start the next iteration of the loop and clobber sb_refname with another call to refs_read_raw_ref() is the error return codepath at the end of the loop, but that is a totally uninteresting case.

Or am I totally confused?
Previous: John Cai via GitGitGadgetNext: John Cai
Message 17 of 28 in “show-ref: add --unresolved option”
  1. show-ref: add --unresolved optionJohn Cai via GitGitGadget, Mar 4, 2024
  2. Junio C HamanoMar 4, 2024
  3. John CaiMar 5, 2024
  4. Junio C HamanoMar 5, 2024
  5. Phillip WoodMar 5, 2024
  6. Kristoffer HaugsbakkMar 5, 2024
  7. Jeff KingMar 6, 2024
  8. Junio C HamanoMar 6, 2024
  9. Jeff KingMar 6, 2024
  10. Patrick SteinhardtMar 6, 2024
  11. Jeff KingMar 6, 2024
  12. Junio C HamanoMar 6, 2024
  13. Jean-Noël AvilaMar 6, 2024
  14. 0/3 show-ref: add --symbolic-name optionJohn Cai via GitGitGadget, Apr 8, 2024
  15. 2/3 refs: add referent to each_repo_ref_fnJohn Cai via GitGitGadget, Apr 8, 2024
  16. 1/3 refs: keep track of unresolved reference value in iteratorJohn Cai via GitGitGadget, Apr 8, 2024
  17. Junio C HamanoApr 8, 2024
  18. John CaiApr 9, 2024
  19. Patrick SteinhardtApr 10, 2024
  20. Junio C HamanoApr 10, 2024
  21. Patrick SteinhardtApr 11, 2024
  22. 3/3 show-ref: add --symbolic-name optionJohn Cai via GitGitGadget, Apr 8, 2024
  23. Phillip WoodApr 9, 2024
  24. John CaiApr 11, 2024
  25. phillip.wood123@gmail.comApr 12, 2024
  26. Patrick SteinhardtApr 10, 2024
  27. Junio C HamanoApr 10, 2024
  28. John CaiApr 12, 2024

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.