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

Re: [PATCH v2 6/6] update-ref: add support for 'symref-update' command

From
Karthik Nayak <karthik.188@gmail.com>
Date
May 29, 2024, 08:29 UTC
Message-ID
<CAOLa=ZSMox99eZ=QmkucN+M+SxzFkQ3ivEzOfa-SZ=kKtEtcMw@mail.gmail.com>
In-Reply-To
<xmqqplt9h4yq.fsf@gitster.g>
Junio C Hamano <gitster@pobox.com> writes:
Show 35 quoted lines
> Karthik Nayak <karthik.188@gmail.com> writes:
>
>> +static void parse_cmd_symref_update(struct ref_transaction *transaction,
>> +				    const char *next, const char *end)
>> +{
>> +	char *refname, *new_target, *old_arg;
>> +	char *old_target = NULL;
>> + ...
>> +	old_arg = parse_next_arg(&next);
>
>> +	if (old_arg) {
>> +		old_target = parse_next_arg(&next);
>
> Now we have an allocated memory we are responsible for freeing in
> old_target, obtained from parse_next_arg() ...
>
>> +		if (!old_target)
>> +			die("symref-update %s: expected old value", refname);
>
> ... and here we know it is not NULL.  We use it to grab the object
> name ...
>
>> +		if (!strcmp(old_arg, "oid")) {
>> +			if (repo_get_oid(the_repository, old_target, &old_oid))
>> +				die("symref-update %s: invalid oid: %s", refname, old_target);
>> +
>> +			old_target = NULL;
>
> ... and then we overwritten the variable, losing the last reference
> to the piece of memory without freeing.
>
> Perhaps squashing this in is sufficient to plug this leak, but there
> probably are other new leaks around this code.  I ran out of time so
> I'll let you take care of the rest ;-)
>

Yeah this indeed was missed, I think having the `TEST_PASSES_SANITIZE_LEAK` flag set, helped catch this leak and another around strbuf.

Show 18 quoted lines
> Thanks.
>
>  builtin/update-ref.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git c/builtin/update-ref.c w/builtin/update-ref.c
> index 76d20ca0f1..7d2a419230 100644
> --- c/builtin/update-ref.c
> +++ w/builtin/update-ref.c
> @@ -297,7 +297,7 @@ static void parse_cmd_symref_update(struct ref_transaction *transaction,
>
>  		if (!strcmp(old_arg, "oid") &&
>  		    !repo_get_oid(the_repository, old_target, &old_oid)) {
> -			old_target = NULL;
> +			FREE_AND_NULL(old_target);
>  			have_old = 1;
>  		} else if (strcmp(old_arg, "ref"))
>  			die("symref-update %s: invalid arg '%s' for old value", refname, old_arg);

This definitely works, but keeping it consistent with the rest of the code in this file, I think we can do:

diff --git a/builtin/update-ref.c b/builtin/update-ref.c
index bda37c161d..82f461d6f8 100644
--- a/builtin/update-ref.c
+++ b/builtin/update-ref.c
@@ -298,7 +300,6 @@ static void parse_cmd_symref_update(struct
ref_transaction *transaction,
 			if (repo_get_oid(the_repository, old_target, &old_oid))
 				die("symref-update %s: invalid oid: %s", refname, old_target);

-			old_target = NULL;
 			have_old_oid = 1;
 		} else if (!strcmp(old_arg, "ref")) {
 			if (check_refname_format(old_target, REFNAME_ALLOW_ONELEVEL))
@@ -313,7 +314,8 @@ static void parse_cmd_symref_update(struct
ref_transaction *transaction,

 	if (ref_transaction_update(transaction, refname, NULL,
 				   have_old_oid ? &old_oid : NULL,
-				   new_target, old_target,
+				   new_target,
+				   have_old_oid ? NULL : old_target,
 				   update_flags | create_reflog_flag,
 				   msg, &err))
 		die("%s", err.buf);

This works, since we anyways do a `free(old_target)` at the end of this
function.
Previous: Junio C HamanoNext: Junio C Hamano
Message 27 of 50 in “update-ref: add symref support for --stdin”
  1. 0/6 update-ref: add symref support for --stdinKarthik Nayak, May 14, 2024
  2. 1/6 refs: create and use `ref_update_ref_must_exist()`Karthik Nayak, May 14, 2024
  3. Patrick SteinhardtMay 16, 2024
  4. Karthik NayakMay 17, 2024
  5. 2/6 update-ref: add support for 'symref-verify' commandKarthik Nayak, May 14, 2024
  6. Patrick SteinhardtMay 16, 2024
  7. Karthik NayakMay 17, 2024
  8. Patrick SteinhardtMay 21, 2024
  9. 3/6 update-ref: add support for 'symref-delete' commandKarthik Nayak, May 14, 2024
  10. Patrick SteinhardtMay 16, 2024
  11. 4/6 update-ref: add support for 'symref-create' commandKarthik Nayak, May 14, 2024
  12. Patrick SteinhardtMay 16, 2024
  13. Karthik NayakMay 19, 2024
  14. 5/6 reftable: pick either 'oid' or 'target' for new updatesKarthik Nayak, May 14, 2024
  15. 6/6 update-ref: add support for 'symref-update' commandKarthik Nayak, May 14, 2024
  16. Patrick SteinhardtMay 16, 2024
  17. Karthik NayakMay 21, 2024
  18. Karthik NayakMay 22, 2024
  19. 0/6 update-ref: add symref support for --stdinKarthik Nayak, May 22, 2024
  20. 1/6 refs: create and use `ref_update_expects_existing_old_ref()`Karthik Nayak, May 22, 2024
  21. 2/6 update-ref: add support for 'symref-verify' commandKarthik Nayak, May 22, 2024
  22. 3/6 update-ref: add support for 'symref-delete' commandKarthik Nayak, May 22, 2024
  23. 4/6 update-ref: add support for 'symref-create' commandKarthik Nayak, May 22, 2024
  24. 5/6 reftable: pick either 'oid' or 'target' for new updatesKarthik Nayak, May 22, 2024
  25. 6/6 update-ref: add support for 'symref-update' commandKarthik Nayak, May 22, 2024
  26. Junio C HamanoMay 25, 2024
  27. Karthik NayakMay 29, 2024
  28. Junio C HamanoMay 23, 2024
  29. Karthik NayakMay 23, 2024
  30. Junio C HamanoMay 23, 2024
  31. Karthik NayakMay 23, 2024
  32. Eric SunshineMay 23, 2024
  33. Junio C HamanoMay 23, 2024
  34. Eric SunshineMay 23, 2024
  35. Junio C HamanoMay 23, 2024
  36. Junio C HamanoMay 23, 2024
  37. Junio C HamanoMay 23, 2024
  38. 0/6 update-ref: add symref support for --stdinKarthik Nayak, May 30, 2024
  39. Patrick SteinhardtJun 5, 2024
  40. 1/6 refs: create and use `ref_update_expects_existing_old_ref()`Karthik Nayak, May 30, 2024
  41. 2/6 update-ref: add support for 'symref-verify' commandKarthik Nayak, May 30, 2024
  42. 3/6 update-ref: add support for 'symref-delete' commandKarthik Nayak, May 30, 2024
  43. Patrick SteinhardtJun 5, 2024
  44. Karthik NayakJun 5, 2024
  45. Junio C HamanoJun 5, 2024
  46. 4/6 update-ref: add support for 'symref-create' commandKarthik Nayak, May 30, 2024
  47. Patrick SteinhardtJun 5, 2024
  48. 5/6 reftable: pick either 'oid' or 'target' for new updatesKarthik Nayak, May 30, 2024
  49. 6/6 update-ref: add support for 'symref-update' commandKarthik Nayak, May 30, 2024
  50. Patrick SteinhardtJun 5, 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.