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

Re: [PATCH 01/11] rev-parse: fix a leak with --abbrev-ref

From
Jeff King <peff@peff.net>
Date
Jun 12, 2023, 03:12 UTC
Message-ID
<20230612031239.GA306352@coredump.intra.peff.net>
In-Reply-To
<dc821efb-bf2a-10bb-4547-98d4ba11dbb3@gmail.com>
On Sun, Jun 11, 2023 at 08:49:17PM +0200, Rubén Justo wrote:
Show 7 quoted lines
> To handle "--abbrev-ref" we use shorten_unambiguous_ref().  This
> function takes a refname and returns a shortened refname, which is a
> newly allocated string that needs to be freed.
> 
> Unfortunately, the refname variable is reused to receive the shortened
> one.  Therefore, we lose the original refname, which needs to be freed
> as well, producing a leak.
Makes sense, but...
Show 34 quoted lines
> diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
> index 852e49e340..9fd7095431 100644
> --- a/builtin/rev-parse.c
> +++ b/builtin/rev-parse.c
> @@ -141,7 +141,7 @@ static void show_rev(int type, const struct object_id *oid, const char *name)
>  	if ((symbolic || abbrev_ref) && name) {
>  		if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
>  			struct object_id discard;
> -			char *full;
> +			char *full, *to_free = NULL;
>  
>  			switch (repo_dwim_ref(the_repository, name,
>  					      strlen(name), &discard, &full,
> @@ -156,9 +156,11 @@ static void show_rev(int type, const struct object_id *oid, const char *name)
>  				 */
>  				break;
>  			case 1: /* happy */
> -				if (abbrev_ref)
> +				if (abbrev_ref) {
> +					to_free = full;
>  					full = shorten_unambiguous_ref(full,
>  						abbrev_ref_strict);
> +				}
>  				show_with_type(type, full);
>  				break;
>  			default: /* ambiguous */
> @@ -166,6 +168,7 @@ static void show_rev(int type, const struct object_id *oid, const char *name)
>  				break;
>  			}
>  			free(full);
> +			free(to_free);
>  		} else {
>  			show_with_type(type, name);
>  		}

The lifetime of "to_free" is much longer than it needs to be here. We'd usually use a "to_free" pattern when the memory is aliased to another (usually const) pointer, and the allocation needs to live as long as that other pointer. But here, nobody cares about the old value as soon as we replace it. We could almost just free() it ahead of time, but of course we also need to pass it to the shortening function. So maybe:

diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index 852e49e340..6dc8548e1f 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -156,9 +156,12 @@ static void show_rev(int type, const struct object_id *oid, const char *name)
 				 */
 				break;
 			case 1: /* happy */
-				if (abbrev_ref)
-					full = shorten_unambiguous_ref(full,
+				if (abbrev_ref) {
+					char *old = full;
+					full = shorten_unambiguous_ref(old,
 						abbrev_ref_strict);
+					free(old);
+				}
 				show_with_type(type, full);
 				break;
 			default: /* ambiguous */

-Peff
Previous: Rubén JustoNext: Rubén Justo
Message 3 of 47 in “tests: mark as passing with SANITIZE=leak”
  1. Rubén JustoJun 11, 2023
  2. 01/11 rev-parse: fix a leak with --abbrev-refRubén Justo, Jun 11, 2023
  3. Jeff KingJun 12, 2023
  4. Rubén JustoJun 16, 2023
  5. 02/11 config: fix a leak in git_config_copy_or_rename_section_in_fileRubén Justo, Jun 11, 2023
  6. Jeff KingJun 12, 2023
  7. 03/11 remote: fix a leak in query_matches_negative_refspecRubén Justo, Jun 11, 2023
  8. Jeff KingJun 12, 2023
  9. Rubén JustoJun 16, 2023
  10. 04/11 branch: fix a leak in dwim_and_setup_trackingRubén Justo, Jun 11, 2023
  11. Jeff KingJun 12, 2023
  12. Rubén JustoJun 16, 2023
  13. 05/11 branch: fix a leak in setup_trackingRubén Justo, Jun 11, 2023
  14. Jeff KingJun 12, 2023
  15. Rubén JustoJun 16, 2023
  16. 06/11 branch: fix a leak in cmd_branchRubén Justo, Jun 11, 2023
  17. Jeff KingJun 12, 2023
  18. Rubén JustoJun 16, 2023
  19. 07/11 branch: fix a leak in inherit_trackingRubén Justo, Jun 11, 2023
  20. Jeff KingJun 12, 2023
  21. 08/11 branch: fix a leak in check_tracking_branchRubén Justo, Jun 11, 2023
  22. Jeff KingJun 12, 2023
  23. 09/11 branch: fix a leak in setup_trackingRubén Justo, Jun 11, 2023
  24. Jeff KingJun 12, 2023
  25. 10/11 config: fix a leak in git_config_copy_or_rename_section_in_fileRubén Justo, Jun 11, 2023
  26. Jeff KingJun 12, 2023
  27. Rubén JustoJun 16, 2023
  28. 11/11 tests: mark as passing with SANITIZE=leakRubén Justo, Jun 11, 2023
  29. Rubén JustoJun 11, 2023
  30. Jeff KingJun 12, 2023
  31. Rubén JustoJun 16, 2023
  32. Junio C HamanoJun 13, 2023
  33. 0/5 tests: mark as passing with SANITIZE=leakRubén Justo, Jun 16, 2023
  34. 1/5 rev-parse: fix a leak with --abbrev-refRubén Justo, Jun 16, 2023
  35. 2/5 branch: fix a leak in setup_trackingRubén Justo, Jun 16, 2023
  36. 3/5 branch: fix a leak in cmd_branchRubén Justo, Jun 16, 2023
  37. 4/5 config: fix a leak in git_config_copy_or_rename_section_in_fileRubén Justo, Jun 16, 2023
  38. 5/5 tests: mark as passing with SANITIZE=leakRubén Justo, Jun 16, 2023
  39. Junio C HamanoJun 16, 2023
  40. Jeff KingJun 17, 2023
  41. Rubén JustoJun 17, 2023
  42. 0/5 tests: mark as passing with SANITIZE=leakRubén Justo, Jun 17, 2023
  43. 1/5 rev-parse: fix a leak with --abbrev-refRubén Justo, Jun 17, 2023
  44. 2/5 branch: fix a leak in setup_trackingRubén Justo, Jun 17, 2023
  45. 3/5 branch: fix a leak in cmd_branchRubén Justo, Jun 17, 2023
  46. 4/5 config: fix a leak in git_config_copy_or_rename_section_in_fileRubén Justo, Jun 17, 2023
  47. 5/5 tests: mark as passing with SANITIZE=leakRubén Justo, Jun 17, 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.