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

Re: [PATCH v3 29/32] submodule--helper: check repo{_submodule,}_init() return values

From
Glen Choo <chooglen@google.com>
Date
Aug 23, 2022, 23:38 UTC
Message-ID
<kl6lh7224jk5.fsf@chooglen-macbookpro.roam.corp.google.com>
In-Reply-To
<patch-v3-29.32-d4b55f07a30-20220821T130231Z-avarab@gmail.com>
Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:
Show 6 quoted lines
> Fix code added in ce125d431aa (submodule: extract path to submodule
> gitdir func, 2021-09-15) and a77c3fcb5ec (submodule--helper: get
> remote names from any repository, 2022-03-04) which failed to check
> the return values of repo_init() and repo_submodule_init(). If we
> failed to initialize the repository or submodule we could segfault
> when trying to access the invalid repository structs.

Yes, this sounds correct. repo_init() and repo_submodule_init() are pure initialization and have no intended side effects, so there's no reason to not check the return value.

Show 12 quoted lines
>
> Let's also check that these were the only such logic errors in the
> codebase by making use of the "warn_unused_result" attribute. This is
> valid as of GCC 3.4.0 (and clang will catch it via its faking of
> __GNUC__ ).
>
> As the comment being added to git-compat-util.h we're piggy-backing on
> the LAST_ARG_MUST_BE_NULL version check out of lazyness. See
> 9fe3edc47f1 (Add the LAST_ARG_MUST_BE_NULL macro, 2013-07-18) for its
> addition. The marginal benefit of covering gcc 3.4.0..4.0.0 is
> near-zero (or zero) at this point. It mostly matters that we catch
> this somewhere.

I'm not familiar enough with attributes and the requistie compiler versions, so I won't comment on this bit.

Show 81 quoted lines
>
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>  builtin/submodule--helper.c | 9 +++++++--
>  git-compat-util.h           | 3 +++
>  repository.h                | 3 +++
>  3 files changed, 13 insertions(+), 2 deletions(-)
>
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index 5f109d422ea..88fc01320f3 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> @@ -63,7 +63,10 @@ static char *get_default_remote_submodule(const char *module_path)
>  {
>  	struct repository subrepo;
>  
> -	repo_submodule_init(&subrepo, the_repository, module_path, null_oid());
> +	if (repo_submodule_init(&subrepo, the_repository, module_path,
> +				null_oid()) < 0)
> +		die(_("could not get a repository handle for submodule '%s'"),
> +		    module_path);
>  	return repo_get_default_remote(&subrepo);
>  }
>  
> @@ -1483,7 +1486,9 @@ static int add_possible_reference_from_superproject(
>  		struct strbuf err = STRBUF_INIT;
>  		strbuf_add(&sb, odb->path, len);
>  
> -		repo_init(&alternate, sb.buf, NULL);
> +		if (repo_init(&alternate, sb.buf, NULL) < 0)
> +			die(_("could not get a repository handle for gitdir '%s'"),
> +			    sb.buf);
>  
>  		/*
>  		 * We need to end the new path with '/' to mark it as a dir,
> diff --git a/git-compat-util.h b/git-compat-util.h
> index 36a25ae252f..01d88650ba3 100644
> --- a/git-compat-util.h
> +++ b/git-compat-util.h
> @@ -568,8 +568,11 @@ static inline int git_has_dir_sep(const char *path)
>  /* The sentinel attribute is valid from gcc version 4.0 */
>  #if defined(__GNUC__) && (__GNUC__ >= 4)
>  #define LAST_ARG_MUST_BE_NULL __attribute__((sentinel))
> +/* warn_unused_result exists as of gcc 3.4.0, but be lazy and check 4.0 */
> +#define RESULT_MUST_BE_USED __attribute__ ((warn_unused_result))
>  #else
>  #define LAST_ARG_MUST_BE_NULL
> +#define RESULT_MUST_BE_USED
>  #endif
>  
>  #define MAYBE_UNUSED __attribute__((__unused__))
> diff --git a/repository.h b/repository.h
> index 797f471cce9..24316ac944e 100644
> --- a/repository.h
> +++ b/repository.h
> @@ -1,6 +1,7 @@
>  #ifndef REPOSITORY_H
>  #define REPOSITORY_H
>  
> +#include "git-compat-util.h"
>  #include "path.h"
>  
>  struct config_set;
> @@ -186,6 +187,7 @@ void repo_set_gitdir(struct repository *repo, const char *root,
>  void repo_set_worktree(struct repository *repo, const char *path);
>  void repo_set_hash_algo(struct repository *repo, int algo);
>  void initialize_the_repository(void);
> +RESULT_MUST_BE_USED
>  int repo_init(struct repository *r, const char *gitdir, const char *worktree);
>  
>  /*
> @@ -197,6 +199,7 @@ int repo_init(struct repository *r, const char *gitdir, const char *worktree);
>   * Return 0 upon success and a non-zero value upon failure.
>   */
>  struct object_id;
> +RESULT_MUST_BE_USED
>  int repo_submodule_init(struct repository *subrepo,
>  			struct repository *superproject,
>  			const char *path,
> -- 
> 2.37.2.1279.g64dec4e13cf
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 103 of 142 in “submodule--helper: add tests, rm dead code, refactor & leak prep”
  1. 00/20 submodule--helper: add tests, rm dead code, refactor & leak prepÆvar Arnfjörð Bjarmason, Jul 28, 2022
  2. 01/20 submodule tests: test usage behaviorÆvar Arnfjörð Bjarmason, Jul 28, 2022
  3. Glen ChooJul 29, 2022
  4. 02/20 submodule tests: test for "add <repository> <abs-path>"Ævar Arnfjörð Bjarmason, Jul 28, 2022
  5. 03/20 submodule--helper: remove unused "name" helperÆvar Arnfjörð Bjarmason, Jul 28, 2022
  6. 04/20 submodule--helper: remove unused "list" helperÆvar Arnfjörð Bjarmason, Jul 28, 2022
  7. Glen ChooJul 29, 2022
  8. 05/20 test-tool submodule-config: remove unused "--url" handlingÆvar Arnfjörð Bjarmason, Jul 28, 2022
  9. 06/20 submodule--helper: move "is-active" to a test-toolÆvar Arnfjörð Bjarmason, Jul 28, 2022
  10. Glen ChooJul 29, 2022
  11. 08/20 submodule--helper: move "resolve-relative-url-test" to a test-toolÆvar Arnfjörð Bjarmason, Jul 28, 2022
  12. Glen ChooJul 29, 2022
  13. 07/20 submodule--helper: move "check-name" to a test-toolÆvar Arnfjörð Bjarmason, Jul 28, 2022
  14. Glen ChooJul 29, 2022
  15. 09/20 submodule--helper style: don't separate declared variables with \n\nÆvar Arnfjörð Bjarmason, Jul 28, 2022
  16. 10/20 submodule--helper style: add \n\n after variable declarationsÆvar Arnfjörð Bjarmason, Jul 28, 2022
  17. 11/20 submodule--helper: replace memset() with { 0 }-initializationÆvar Arnfjörð Bjarmason, Jul 28, 2022
  18. 12/20 submodule--helper: convert a strbuf_detach() to xstrfmt()Ævar Arnfjörð Bjarmason, Jul 28, 2022
  19. 13/20 submodule--helper: stop conflating "sb" in clone_submodule()Ævar Arnfjörð Bjarmason, Jul 28, 2022
  20. Glen ChooJul 29, 2022
  21. 14/20 submodule--helper: pass a "const struct module_clone_data" to clone_submodule()Ævar Arnfjörð Bjarmason, Jul 28, 2022
  22. Glen ChooJul 29, 2022
  23. Ævar Arnfjörð BjarmasonAug 1, 2022
  24. 15/20 submodule--helper: add "const" to copy of "update_data"Ævar Arnfjörð Bjarmason, Jul 28, 2022
  25. 16/20 submodule--helper: refactor "errmsg_str" to be a "struct strbuf"Ævar Arnfjörð Bjarmason, Jul 28, 2022
  26. 17/20 submodule--helper: rename "int res" to "int ret"Ævar Arnfjörð Bjarmason, Jul 28, 2022
  27. 18/20 submodule--helper: add skeleton "goto cleanup" to update_submodule()Ævar Arnfjörð Bjarmason, Jul 28, 2022
  28. 19/20 submodule--helper: don't exit() on failure, returnÆvar Arnfjörð Bjarmason, Jul 28, 2022
  29. Glen ChooJul 29, 2022
  30. 20/20 submodule--helper: fix bad config API usageÆvar Arnfjörð Bjarmason, Jul 28, 2022
  31. Glen ChooJul 29, 2022
  32. 00/28 submodule--helper: add tests, rm dead code, refactor & leak prepÆvar Arnfjörð Bjarmason, Aug 2, 2022
  33. 02/28 submodule tests: test for "add <repository> <abs-path>"Ævar Arnfjörð Bjarmason, Aug 2, 2022
  34. 01/28 submodule tests: test usage behaviorÆvar Arnfjörð Bjarmason, Aug 2, 2022
  35. Glen ChooAug 2, 2022
  36. 03/28 submodule--helper: remove unused "name" helperÆvar Arnfjörð Bjarmason, Aug 2, 2022
  37. 05/28 test-tool submodule-config: remove unused "--url" handlingÆvar Arnfjörð Bjarmason, Aug 2, 2022
  38. 04/28 submodule--helper: remove unused "list" helperÆvar Arnfjörð Bjarmason, Aug 2, 2022
  39. 06/28 submodule--helper: move "is-active" to a test-toolÆvar Arnfjörð Bjarmason, Aug 2, 2022
  40. 07/28 submodule--helper: move "check-name" to a test-toolÆvar Arnfjörð Bjarmason, Aug 2, 2022
  41. 08/28 submodule--helper: move "resolve-relative-url-test" to a test-toolÆvar Arnfjörð Bjarmason, Aug 2, 2022
  42. Glen ChooAug 2, 2022
  43. 11/28 submodule--helper: replace memset() with { 0 }-initializationÆvar Arnfjörð Bjarmason, Aug 2, 2022
  44. 10/28 submodule--helper style: add \n\n after variable declarationsÆvar Arnfjörð Bjarmason, Aug 2, 2022
  45. 09/28 submodule--helper style: don't separate declared variables with \n\nÆvar Arnfjörð Bjarmason, Aug 2, 2022
  46. 13/28 submodule--helper: move "sb" in clone_submodule() to its own scopeÆvar Arnfjörð Bjarmason, Aug 2, 2022
  47. 12/28 submodule--helper: use xstrfmt() in clone_submodule()Ævar Arnfjörð Bjarmason, Aug 2, 2022
  48. 15/28 submodule--helper: add "const" to copy of "update_data"Ævar Arnfjörð Bjarmason, Aug 2, 2022
  49. 14/28 submodule--helper: pass a "const struct module_clone_data" to clone_submodule()Ævar Arnfjörð Bjarmason, Aug 2, 2022
  50. 17/28 submodule--helper: don't redundantly check "else if (res)"Ævar Arnfjörð Bjarmason, Aug 2, 2022
  51. 16/28 submodule--helper: refactor "errmsg_str" to be a "struct strbuf"Ævar Arnfjörð Bjarmason, Aug 2, 2022
  52. 18/28 submodule--helper: rename "int res" to "int ret"Ævar Arnfjörð Bjarmason, Aug 2, 2022
  53. 19/28 submodule--helper: return "ret", not "1" from update_submodule()Ævar Arnfjörð Bjarmason, Aug 2, 2022
  54. 20/28 submodule--helper: add missing braces to "else" armÆvar Arnfjörð Bjarmason, Aug 2, 2022
  55. 21/28 submodule--helper: don't call submodule_strategy_to_string() in BUG()Ævar Arnfjörð Bjarmason, Aug 2, 2022
  56. Glen ChooAug 2, 2022
  57. 22/28 submodule--helper: move submodule_strategy_to_string() to only userÆvar Arnfjörð Bjarmason, Aug 2, 2022
  58. Glen ChooAug 2, 2022
  59. Ævar Arnfjörð BjarmasonAug 3, 2022
  60. 24/28 submodule--helper: don't exit() on failure, returnÆvar Arnfjörð Bjarmason, Aug 2, 2022
  61. 25/28 submodule--helper: libify determine_submodule_update_strategy()Ævar Arnfjörð Bjarmason, Aug 2, 2022
  62. 23/28 submodule--helper: use "code" in run_update_command()Ævar Arnfjörð Bjarmason, Aug 2, 2022
  63. 27/28 submodule--helper: libify "must_die_on_failure" code paths (for die)Ævar Arnfjörð Bjarmason, Aug 2, 2022
  64. Glen ChooAug 3, 2022
  65. 26/28 submodule--helper: libify "must_die_on_failure" code pathsÆvar Arnfjörð Bjarmason, Aug 2, 2022
  66. Glen ChooAug 3, 2022
  67. 28/28 submodule--helper: fix bad config API usageÆvar Arnfjörð Bjarmason, Aug 2, 2022
  68. 00/32 submodule--helper: add tests, rm dead code, refactor & leak prepÆvar Arnfjörð Bjarmason, Aug 21, 2022
  69. 01/32 submodule tests: test usage behaviorÆvar Arnfjörð Bjarmason, Aug 21, 2022
  70. Glen ChooAug 23, 2022
  71. 02/32 submodule tests: test for "add <repository> <abs-path>"Ævar Arnfjörð Bjarmason, Aug 21, 2022
  72. 03/32 submodule--helper: remove unused "name" helperÆvar Arnfjörð Bjarmason, Aug 21, 2022
  73. 04/32 submodule--helper: remove unused "list" helperÆvar Arnfjörð Bjarmason, Aug 21, 2022
  74. 05/32 test-tool submodule-config: remove unused "--url" handlingÆvar Arnfjörð Bjarmason, Aug 21, 2022
  75. 06/32 submodule--helper: move "is-active" to a test-toolÆvar Arnfjörð Bjarmason, Aug 21, 2022
  76. 07/32 submodule--helper: move "check-name" to a test-toolÆvar Arnfjörð Bjarmason, Aug 21, 2022
  77. 08/32 submodule--helper: move "resolve-relative-url-test" to a test-toolÆvar Arnfjörð Bjarmason, Aug 21, 2022
  78. 09/32 submodule--helper style: don't separate declared variables with \n\nÆvar Arnfjörð Bjarmason, Aug 21, 2022
  79. 10/32 submodule--helper style: add \n\n after variable declarationsÆvar Arnfjörð Bjarmason, Aug 21, 2022
  80. 11/32 submodule--helper: replace memset() with { 0 }-initializationÆvar Arnfjörð Bjarmason, Aug 21, 2022
  81. 13/32 submodule--helper: move "sb" in clone_submodule() to its own scopeÆvar Arnfjörð Bjarmason, Aug 21, 2022
  82. 12/32 submodule--helper: use xstrfmt() in clone_submodule()Ævar Arnfjörð Bjarmason, Aug 21, 2022
  83. 14/32 submodule--helper: add "const" to passed "module_clone_data"Ævar Arnfjörð Bjarmason, Aug 21, 2022
  84. 15/32 submodule--helper: add "const" to copy of "update_data"Ævar Arnfjörð Bjarmason, Aug 21, 2022
  85. 16/32 submodule--helper: refactor "errmsg_str" to be a "struct strbuf"Ævar Arnfjörð Bjarmason, Aug 21, 2022
  86. 17/32 submodule--helper: don't redundantly check "else if (res)"Ævar Arnfjörð Bjarmason, Aug 21, 2022
  87. Eric SunshineAug 21, 2022
  88. 19/32 submodule--helper: return "ret", not "1" from update_submodule()Ævar Arnfjörð Bjarmason, Aug 21, 2022
  89. 18/32 submodule--helper: rename "int res" to "int ret"Ævar Arnfjörð Bjarmason, Aug 21, 2022
  90. 20/32 submodule--helper: add missing braces to "else" armÆvar Arnfjörð Bjarmason, Aug 21, 2022
  91. 21/32 submodule--helper: don't call submodule_strategy_to_string() in BUG()Ævar Arnfjörð Bjarmason, Aug 21, 2022
  92. 22/32 submodule API: don't handle SM_..{UNSPECIFIED,COMMAND} in to_string()Ævar Arnfjörð Bjarmason, Aug 21, 2022
  93. 23/32 submodule--helper: use "code" in run_update_command()Ævar Arnfjörð Bjarmason, Aug 21, 2022
  94. 24/32 submodule--helper: don't exit() on failure, returnÆvar Arnfjörð Bjarmason, Aug 21, 2022
  95. 25/32 submodule--helper: libify determine_submodule_update_strategy()Ævar Arnfjörð Bjarmason, Aug 21, 2022
  96. 27/32 submodule--helper update: don't override 'checkout' exit codeÆvar Arnfjörð Bjarmason, Aug 21, 2022
  97. 26/32 submodule--helper: libify "must_die_on_failure" code pathsÆvar Arnfjörð Bjarmason, Aug 21, 2022
  98. 28/32 submodule--helper: libify "must_die_on_failure" code paths (for die)Ævar Arnfjörð Bjarmason, Aug 21, 2022
  99. Glen ChooAug 23, 2022
  100. 31/32 submodule--helper: libify even more "die" paths for module_update()Ævar Arnfjörð Bjarmason, Aug 21, 2022
  101. Glen ChooAug 24, 2022
  102. 29/32 submodule--helper: check repo{_submodule,}_init() return valuesÆvar Arnfjörð Bjarmason, Aug 21, 2022
  103. Glen ChooAug 23, 2022
  104. 32/32 submodule--helper: fix bad config API usageÆvar Arnfjörð Bjarmason, Aug 21, 2022
  105. 30/32 submodule--helper: libify more "die" paths for module_update()Ævar Arnfjörð Bjarmason, Aug 21, 2022
  106. Glen ChooAug 24, 2022
  107. 00/33 submodule--helper: add tests, rm dead code, refactor & leak prepÆvar Arnfjörð Bjarmason, Aug 31, 2022
  108. 01/33 submodule tests: test usage behaviorÆvar Arnfjörð Bjarmason, Aug 31, 2022
  109. 02/33 submodule tests: test for "add <repository> <abs-path>"Ævar Arnfjörð Bjarmason, Aug 31, 2022
  110. 03/33 submodule--helper: remove unused "name" helperÆvar Arnfjörð Bjarmason, Aug 31, 2022
  111. 04/33 submodule--helper: remove unused "list" helperÆvar Arnfjörð Bjarmason, Aug 31, 2022
  112. 05/33 test-tool submodule-config: remove unused "--url" handlingÆvar Arnfjörð Bjarmason, Aug 31, 2022
  113. 07/33 submodule--helper: move "check-name" to a test-toolÆvar Arnfjörð Bjarmason, Aug 31, 2022
  114. 08/33 submodule--helper: move "resolve-relative-url-test" to a test-toolÆvar Arnfjörð Bjarmason, Aug 31, 2022
  115. 09/33 submodule--helper style: don't separate declared variables with \n\nÆvar Arnfjörð Bjarmason, Aug 31, 2022
  116. 12/33 submodule--helper: use xstrfmt() in clone_submodule()Ævar Arnfjörð Bjarmason, Aug 31, 2022
  117. 11/33 submodule--helper: replace memset() with { 0 }-initializationÆvar Arnfjörð Bjarmason, Aug 31, 2022
  118. 06/33 submodule--helper: move "is-active" to a test-toolÆvar Arnfjörð Bjarmason, Aug 31, 2022
  119. 10/33 submodule--helper style: add \n\n after variable declarationsÆvar Arnfjörð Bjarmason, Aug 31, 2022
  120. 16/33 submodule--helper: add "const" to passed "struct update_data"Ævar Arnfjörð Bjarmason, Aug 31, 2022
  121. 14/33 submodule--helper: add "const" to passed "module_clone_data"Ævar Arnfjörð Bjarmason, Aug 31, 2022
  122. 13/33 submodule--helper: move "sb" in clone_submodule() to its own scopeÆvar Arnfjörð Bjarmason, Aug 31, 2022
  123. 19/33 submodule--helper: rename "int res" to "int ret"Ævar Arnfjörð Bjarmason, Aug 31, 2022
  124. 17/33 submodule--helper: refactor "errmsg_str" to be a "struct strbuf"Ævar Arnfjörð Bjarmason, Aug 31, 2022
  125. 18/33 submodule--helper: don't redundantly check "else if (res)"Ævar Arnfjörð Bjarmason, Aug 31, 2022
  126. 15/33 submodule--helper: add "const" to copy of "update_data"Ævar Arnfjörð Bjarmason, Aug 31, 2022
  127. 21/33 submodule--helper: add missing braces to "else" armÆvar Arnfjörð Bjarmason, Aug 31, 2022
  128. 22/33 submodule--helper: don't call submodule_strategy_to_string() in BUG()Ævar Arnfjörð Bjarmason, Aug 31, 2022
  129. 20/33 submodule--helper: return "ret", not "1" from update_submodule()Ævar Arnfjörð Bjarmason, Aug 31, 2022
  130. 23/33 submodule API: don't handle SM_..{UNSPECIFIED,COMMAND} in to_string()Ævar Arnfjörð Bjarmason, Aug 31, 2022
  131. 24/33 submodule--helper: use "code" in run_update_command()Ævar Arnfjörð Bjarmason, Aug 31, 2022
  132. 26/33 submodule--helper: libify determine_submodule_update_strategy()Ævar Arnfjörð Bjarmason, Aug 31, 2022
  133. 27/33 submodule--helper: libify "must_die_on_failure" code pathsÆvar Arnfjörð Bjarmason, Aug 31, 2022
  134. 25/33 submodule--helper: don't exit() on failure, returnÆvar Arnfjörð Bjarmason, Aug 31, 2022
  135. 28/33 submodule--helper update: don't override 'checkout' exit codeÆvar Arnfjörð Bjarmason, Aug 31, 2022
  136. 29/33 submodule--helper: libify "must_die_on_failure" code paths (for die)Ævar Arnfjörð Bjarmason, Aug 31, 2022
  137. 30/33 submodule--helper: check repo{_submodule,}_init() return valuesÆvar Arnfjörð Bjarmason, Aug 31, 2022
  138. 32/33 submodule--helper: libify even more "die" paths for module_update()Ævar Arnfjörð Bjarmason, Aug 31, 2022
  139. 31/33 submodule--helper: libify more "die" paths for module_update()Ævar Arnfjörð Bjarmason, Aug 31, 2022
  140. Glen ChooSep 1, 2022
  141. 33/33 submodule--helper: fix bad config API usageÆvar Arnfjörð Bjarmason, Aug 31, 2022
  142. Glen ChooSep 1, 2022

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.