Skip to content

Commit 1e8697b

Browse files
avargitster
authored andcommitted
submodule--helper: check repo{_submodule,}_init() return values
Fix code added in ce125d4 (submodule: extract path to submodule gitdir func, 2021-09-15) and a77c3fc (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. 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 9fe3edc (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. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Reviewed-by: Glen Choo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ac35015 commit 1e8697b

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

builtin/submodule--helper.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ static char *get_default_remote_submodule(const char *module_path)
6363
{
6464
struct repository subrepo;
6565

66-
repo_submodule_init(&subrepo, the_repository, module_path, null_oid());
66+
if (repo_submodule_init(&subrepo, the_repository, module_path,
67+
null_oid()) < 0)
68+
die(_("could not get a repository handle for submodule '%s'"),
69+
module_path);
6770
return repo_get_default_remote(&subrepo);
6871
}
6972

@@ -1480,7 +1483,9 @@ static int add_possible_reference_from_superproject(
14801483
struct strbuf err = STRBUF_INIT;
14811484
strbuf_add(&sb, odb->path, len);
14821485

1483-
repo_init(&alternate, sb.buf, NULL);
1486+
if (repo_init(&alternate, sb.buf, NULL) < 0)
1487+
die(_("could not get a repository handle for gitdir '%s'"),
1488+
sb.buf);
14841489

14851490
/*
14861491
* We need to end the new path with '/' to mark it as a dir,

git-compat-util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,11 @@ static inline int git_has_dir_sep(const char *path)
565565
/* The sentinel attribute is valid from gcc version 4.0 */
566566
#if defined(__GNUC__) && (__GNUC__ >= 4)
567567
#define LAST_ARG_MUST_BE_NULL __attribute__((sentinel))
568+
/* warn_unused_result exists as of gcc 3.4.0, but be lazy and check 4.0 */
569+
#define RESULT_MUST_BE_USED __attribute__ ((warn_unused_result))
568570
#else
569571
#define LAST_ARG_MUST_BE_NULL
572+
#define RESULT_MUST_BE_USED
570573
#endif
571574

572575
#define MAYBE_UNUSED __attribute__((__unused__))

repository.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef REPOSITORY_H
22
#define REPOSITORY_H
33

4+
#include "git-compat-util.h"
45
#include "path.h"
56

67
struct config_set;
@@ -185,6 +186,7 @@ void repo_set_gitdir(struct repository *repo, const char *root,
185186
void repo_set_worktree(struct repository *repo, const char *path);
186187
void repo_set_hash_algo(struct repository *repo, int algo);
187188
void initialize_the_repository(void);
189+
RESULT_MUST_BE_USED
188190
int repo_init(struct repository *r, const char *gitdir, const char *worktree);
189191

190192
/*
@@ -196,6 +198,7 @@ int repo_init(struct repository *r, const char *gitdir, const char *worktree);
196198
* Return 0 upon success and a non-zero value upon failure.
197199
*/
198200
struct object_id;
201+
RESULT_MUST_BE_USED
199202
int repo_submodule_init(struct repository *subrepo,
200203
struct repository *superproject,
201204
const char *path,

0 commit comments

Comments
 (0)