From 1e8697b5c4ed7c2762a0858251efa8feb8126038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Thu, 1 Sep 2022 01:18:12 +0200 Subject: submodule--helper: check repo{_submodule,}_init() return values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. 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. Signed-off-by: Ævar Arnfjörð Bjarmason Reviewed-by: Glen Choo Signed-off-by: Junio C Hamano --- repository.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'repository.h') diff --git a/repository.h b/repository.h index 6cc661e5a4..17c45ae096 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; @@ -185,6 +186,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); /* @@ -196,6 +198,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, -- cgit v1.2.3