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

Re: [PATCH 3/7] builtin/gc: fix leaking config values

From
Derrick Stolee <stolee@gmail.com>
Date
Aug 15, 2024, 13:50 UTC
Message-ID
<23188674-a465-4af8-ae5a-2404ab6d4c1e@gmail.com>
In-Reply-To
<310e361371efc156c3aaac94439bdbeaa965155f.1723533091.git.ps@pks.im>
On 8/13/24 3:17 AM, Patrick Steinhardt wrote:
Show 11 quoted lines
> We're leaking config values in git-gc(1) when those values are tracked
> as strings. Introduce a new `gc_config_release()` function that releases
> this memory to plug those leaks and release old values before populating
> the config fields via `git_config_string()` et al.
> 
> Note that there is one small gotcha here with the "--prune" option. Next
> to passing a string, this option also accepts the "--no-prune" option
> that overrides the default or configured value. We thus need to discern
> between the option not having been passed by the user and the negative
> variant of it. This is done by using a simple sentinel value that lets
> us discern these cases.

I'm glad to see that you are correcting this in the very next patch where I pointed out my concern about it. Excellent.

Show 6 quoted lines
> -	const char *gc_log_expire;
> -	const char *prune_expire;
> -	const char *prune_worktrees_expire;
> +	char *gc_log_expire;
> +	char *prune_expire;
> +	char *prune_worktrees_expire;
Show 6 quoted lines
> -	.gc_log_expire = "1.day.ago", \
> -	.prune_expire = "2.weeks.ago", \
> -	.prune_worktrees_expire = "3.months.ago", \
> +	.gc_log_expire = xstrdup("1.day.ago"), \
> +	.prune_expire = xstrdup("2.weeks.ago"), \
> +	.prune_worktrees_expire = xstrdup("3.months.ago"), \
Using xstrdup() is now possible that these aren't globals. Good.
Show 19 quoted lines
>   static void gc_config(struct gc_config *cfg)
>   {
>   	const char *value;
> +	char *owned = NULL;
>   
>   	if (!git_config_get_value("gc.packrefs", &value)) {
>   		if (value && !strcmp(value, "notbare"))
> @@ -185,15 +195,34 @@ static void gc_config(struct gc_config *cfg)
>   	git_config_get_bool("gc.autodetach", &cfg->detach_auto);
>   	git_config_get_bool("gc.cruftpacks", &cfg->cruft_packs);
>   	git_config_get_ulong("gc.maxcruftsize", &cfg->max_cruft_size);
> -	git_config_get_expiry("gc.pruneexpire", (char **) &cfg->prune_expire);
> -	git_config_get_expiry("gc.worktreepruneexpire", (char **) &cfg->prune_worktrees_expire);
> -	git_config_get_expiry("gc.logexpiry", (char **) &cfg->gc_log_expire);
> +
> +	if (!git_config_get_expiry("gc.pruneexpire", &owned)) {
> +		free(cfg->prune_expire);
> +		cfg->prune_expire = owned;
> +	}

Ah. Good logic of "if I get a (possibly NULL) result, then free the old value before setting the new one".

Show 9 quoted lines
> diff --git a/t/t5304-prune.sh b/t/t5304-prune.sh
> index 1f1f664871..e641df0116 100755
> --- a/t/t5304-prune.sh
> +++ b/t/t5304-prune.sh
> @@ -7,6 +7,7 @@ test_description='prune'
>   GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
>   export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
>   
> +TEST_PASSES_SANITIZE_LEAK=true
Fantastic.

Thanks, -Stolee

Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 10 of 79 in “builtin/maintenance: fix auto-detach with non-standard tasks”
  1. 0/7 builtin/maintenance: fix auto-detach with non-standard tasksPatrick Steinhardt, Aug 13, 2024
  2. 1/7 config: fix constness of out parameter for `git_config_get_expiry()`Patrick Steinhardt, Aug 13, 2024
  3. 2/7 builtin/gc: refactor to read config into structurePatrick Steinhardt, Aug 13, 2024
  4. James LiuAug 15, 2024
  5. Patrick SteinhardtAug 15, 2024
  6. Derrick StoleeAug 15, 2024
  7. 3/7 builtin/gc: fix leaking config valuesPatrick Steinhardt, Aug 13, 2024
  8. James LiuAug 15, 2024
  9. Patrick SteinhardtAug 15, 2024
  10. Derrick StoleeAug 15, 2024
  11. 4/7 builtin/gc: stop processing log file on signalPatrick Steinhardt, Aug 13, 2024
  12. James LiuAug 15, 2024
  13. 5/7 builtin/gc: add a `--detach` flagPatrick Steinhardt, Aug 13, 2024
  14. 6/7 builtin/maintenance: add a `--detach` flagPatrick Steinhardt, Aug 13, 2024
  15. 7/7 builtin/maintenance: fix auto-detach with non-standard tasksPatrick Steinhardt, Aug 13, 2024
  16. Phillip WoodAug 13, 2024
  17. Patrick SteinhardtAug 13, 2024
  18. Phillip WoodAug 13, 2024
  19. Patrick SteinhardtAug 14, 2024
  20. Phillip WoodAug 14, 2024
  21. Patrick SteinhardtAug 15, 2024
  22. James LiuAug 15, 2024
  23. Patrick SteinhardtAug 15, 2024
  24. Derrick StoleeAug 15, 2024
  25. James LiuAug 15, 2024
  26. 0/7 builtin/maintenance: fix auto-detach with non-standard tasksPatrick Steinhardt, Aug 15, 2024
  27. 2/7 builtin/gc: refactor to read config into structurePatrick Steinhardt, Aug 15, 2024
  28. 1/7 config: fix constness of out parameter for `git_config_get_expiry()`Patrick Steinhardt, Aug 15, 2024
  29. 6/7 builtin/maintenance: add a `--detach` flagPatrick Steinhardt, Aug 15, 2024
  30. 5/7 builtin/gc: add a `--detach` flagPatrick Steinhardt, Aug 15, 2024
  31. Junio C HamanoAug 15, 2024
  32. Junio C HamanoAug 15, 2024
  33. Patrick SteinhardtAug 16, 2024
  34. 4/7 builtin/gc: stop processing log file on signalPatrick Steinhardt, Aug 15, 2024
  35. 7/7 run-command: fix detaching when running auto maintenancePatrick Steinhardt, Aug 15, 2024
  36. Junio C HamanoAug 15, 2024
  37. Patrick SteinhardtAug 16, 2024
  38. 3/7 builtin/gc: fix leaking config valuesPatrick Steinhardt, Aug 15, 2024
  39. Derrick StoleeAug 15, 2024
  40. Junio C HamanoAug 15, 2024
  41. Patrick SteinhardtAug 16, 2024
  42. 0/7 builtin/maintenance: fix auto-detach with non-standard tasksPatrick Steinhardt, Aug 16, 2024
  43. 1/7 config: fix constness of out parameter for `git_config_get_expiry()`Patrick Steinhardt, Aug 16, 2024
  44. 2/7 builtin/gc: refactor to read config into structurePatrick Steinhardt, Aug 16, 2024
  45. 3/7 builtin/gc: fix leaking config valuesPatrick Steinhardt, Aug 16, 2024
  46. 4/7 builtin/gc: stop processing log file on signalPatrick Steinhardt, Aug 16, 2024
  47. 5/7 builtin/gc: add a `--detach` flagPatrick Steinhardt, Aug 16, 2024
  48. 6/7 builtin/maintenance: add a `--detach` flagPatrick Steinhardt, Aug 16, 2024
  49. Jeff KingAug 17, 2024
  50. Jeff KingAug 17, 2024
  51. Patrick SteinhardtAug 19, 2024
  52. 7/7 run-command: fix detaching when running auto maintenancePatrick Steinhardt, Aug 16, 2024
  53. Jeff KingAug 17, 2024
  54. Patrick SteinhardtAug 19, 2024
  55. 0/3 Fixups for git-maintenance(1) testsPatrick Steinhardt, Aug 19, 2024
  56. 1/3 t7900: fix flaky test due to leaking background jobPatrick Steinhardt, Aug 19, 2024
  57. Jeff KingAug 19, 2024
  58. Patrick SteinhardtAug 19, 2024
  59. Jeff KingAug 19, 2024
  60. Patrick SteinhardtAug 19, 2024
  61. 2/3 t7900: exercise detaching via trace2 regionsPatrick Steinhardt, Aug 19, 2024
  62. Jeff KingAug 19, 2024
  63. Patrick SteinhardtAug 19, 2024
  64. Junio C HamanoAug 21, 2024
  65. Patrick SteinhardtAug 22, 2024
  66. Junio C HamanoAug 22, 2024
  67. 3/3 builtin/maintenance: fix loose objects task emitting pack hashPatrick Steinhardt, Aug 19, 2024
  68. Jeff KingAug 19, 2024
  69. Patrick SteinhardtAug 19, 2024
  70. Jeff KingAug 19, 2024
  71. Patrick SteinhardtAug 19, 2024
  72. Jeff KingAug 19, 2024
  73. Patrick SteinhardtAug 20, 2024
  74. Junio C HamanoAug 20, 2024
  75. Junio C HamanoAug 19, 2024
  76. Jeff KingAug 19, 2024
  77. Patrick SteinhardtAug 19, 2024
  78. Patrick SteinhardtAug 19, 2024
  79. Patrick SteinhardtAug 19, 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.