fix a leak in submodule error path
cover fix a leak in submodule error path
This fixes a small leak noticed by Coverity. I think it has been there for a while, but nearby code movement caused it to be marked as "new".
[1/2]: repository: make repo_clear() idempotent [2/2]: submodule--helper: free URL when repository setup fails
builtin/submodule--helper.c | 10 +++++++--- repository.c | 3 ++- t/t7426-submodule-get-default-remote.sh | 17 +++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-)
-Peff
PATCH 1/2 repository: make repo_clear() idempotent
Calling repo_clear() twice in a row will segfault because the second call will invoke parse_object_pool_clear() on a NULL pointer. This is not usually a big deal, but we can make some error cleanup a little simpler if callers do not need to worry about invoking it twice.
We can fix it by catching the NULL case. The rest of repo_clear() appears to be idempotent.
---
This helps in the next patch, but I think it's also just the path of least surprise.
Arguably this should not be a pointer at all, but the pool code is weirdly asymmetric. It offers only "new" which allocates a struct, but only "clear" to clean it up (but not deallocate). Might be worth fixing, but out of scope for this series.
repository.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
repository.c +2 -1
@@ -385,7 +385,8 @@ void repo_clear(struct repository *repo)odb_free (repo->objects); repo->objects =NULL ; -parsed_object_pool_clear (repo->parsed_objects); +if (repo->parsed_objects) +parsed_object_pool_clear (repo->parsed_objects);FREE_AND_NULL (repo->parsed_objects);repo_settings_clear (repo);
Signature · 1 line
2.55.0.1067.gf7fc94a55c
Re: [PATCH 1/2] repository: make repo_clear() idempotent
Quoted 4 lines · Jeff King
Arguably this should not be a pointer at all, but the pool code is weirdly asymmetric. It offers only "new" which allocates a struct, but only "clear" to clean it up (but not deallocate). Might be worth fixing, but out of scope for this series.
I took a quick stab at this, and it gets ugly. There is no mutual recursion between the parsed_object_pool and repository struct definitions, but we do end up in a header include loop:
- repository.h would need object.h (to include the pool struct)
- object.h includes hash.h for object_id, etc
- hash.h (sometimes) includes repository.h so it can define the_hash_algo when USE_THE_REPOSITORY_VARIABLE is defined
We could break the cycle if we had a separate the-repository.h which looked like this:
struct repository;
extern struct repository *the_repository;
and then included that from hash.h. But then callers which want to use the_hash_algo would need to include repository.h themselves. It is just a macro looking at the_repository->hash_algo, so they need the actual repository definition. About 9 files need to start including repository.h themselves to make it work. Though a few of them _ought_ to be including it anyway; they are not using the_hash_algo at all, but just lucky that hash.h happens to bring repository.h when USE_THE_REPOSITORY_VARIABLE is set.
An alternative would be to define the_hash_algo as its own pointer, like:
hash.h +1 -2
@@ -269,8 +269,7 @@ enum get_oid_result { };#ifdef USE_THE_REPOSITORY_VARIABLE -# include "repository.h" -# define the_hash_algo the_repository->hash_algo +extern struct git_hash_algo *the_hash_algo;#endif /* A suitably aligned type for stack allocations of hash contexts. */
repository.c +1 -0
@@ -30,6 +30,7 @@ extern struct repository *the_repository;/* The main repository */ static struct repository the_repo;struct repository *the_repository = &the_repo; +struct the_hash_algo = &the_repo->hash_algo;/* * An escape hatch: if we hit a bug in the production code that fails
That makes the_hash_algo just work without most code caring about repositories at all. But of course it reveals yet more spots which are relying on hash.h mentioning the_repository. :-/
I'm not sure how much it's worth untangling all of this, but probably not enough just to remove pointer indirection from repo->parsed_objects.
-Peff
Re: [PATCH 1/2] repository: make repo_clear() idempotent
Quoted 2 lines · Jeff King
I'm not sure how much it's worth untangling all of this, but probably not enough just to remove pointer indirection from
repo->parsed_objects.
BTW, another curiosity: parsed_objects contains a pointer back to the repo that contains it! What could it possibly depend on in the repo itself?
As far as I can tell, the answer is nothing. We only ever access p->repo in order to get to p->repo->parsed_objects, which will always be the same as our original "p". There are some internal functions within object.c which could be simplified by passing around the parsed_object_pool directly. But we also call lookup_commit() and a few other public functions, all of which take a repository struct. Even though they only use it to look at the parsed_objects field!
Structurally speaking these should be operating on a parsed_object_pool, since that's all they need. But from the caller's point of view that is just an implementation detail, and it is easier to pass in the whole repository.
So we'd probably need to provide functions that operate directly on the pool like:
struct commit *lookup_commit_via_pool(struct parsed_object_pool *p,
const struct object_id *oid);
and then maintain wrappers like:
struct commit *lookup_commit(struct repository *r,
const struct object_id *oid)
{
return lookup_commit_via_pool(r->parsed_objects, oid);}
to avoid rewriting every caller with r->parsed_objects themselves.
The patch below illustrates the minimal change to drop the repo pointer from parsed_object_pool. I think it more accurately represents the actual dependencies of the data structures, but it's a fair bit of churn for a minor amount of clarity. Probably not worth it.
--- alloc.c | 9 ++++-- alloc.h | 1 + commit.c | 20 +++++++++---- commit.h | 3 ++ object.c | 55 ++++++++++++++++++++--------------- object.h | 3 +- 6 files changed, 60 insertions(+), 31 deletions(-)
alloc.c +7 -2
@@ -121,9 +121,14 @@ void init_commit_node(struct commit *c) c->index =alloc_commit_index (); } -void *alloc_commit_node (struct repository *r) +void *alloc_commit_node_via_pool (struct parsed_object_pool *p) { -struct commit *c =alloc_node (r->parsed_objects->commit_state,sizeof (struct commit )); +struct commit *c =alloc_node (p->commit_state,sizeof (struct commit ));init_commit_node (c);return c; } + +void *alloc_commit_node (struct repository *r) +{ +return alloc_commit_node_via_pool (r->parsed_objects); +}
alloc.h +1 -0
@@ -11,6 +11,7 @@ void *alloc_blob_node(struct repository *r);void *alloc_tree_node (struct repository *r);void init_commit_node (struct commit *c);void *alloc_commit_node (struct repository *r); +void *alloc_commit_node_via_pool (struct parsed_object_pool *p);void *alloc_tag_node (struct repository *r);void *alloc_object_node (struct repository *r);
commit.c +15 -5
@@ -98,14 +98,19 @@ struct commit *lookup_commit_object(struct repository *r, } -struct commit *lookup_commit (struct repository *r,const struct object_id *oid) +struct commit *lookup_commit_via_pool (struct parsed_object_pool *p,const struct object_id *oid) { -struct object *obj =lookup_object (r, oid); +struct object *obj =lookup_object_via_pool (p, oid);if (!obj) -return create_object (r, oid,alloc_commit_node (r)); +return create_object_via_pool (p, oid,alloc_commit_node_via_pool (p));return object_as_type (obj,OBJ_COMMIT ,0 ); } +struct commit *lookup_commit (struct repository *r,const struct object_id *oid) +{ +return lookup_commit_via_pool (r->parsed_objects, oid); +} +struct commit *lookup_commit_reference_by_name (const char *name) {return lookup_commit_reference_by_name_gently (name,0 );
@@ -206,9 +211,9 @@ int commit_graft_pos(struct repository *r, const struct object_id *oid) commit_graft_oid_access); } -void unparse_commit (struct repository *r,const struct object_id *oid) +void unparse_commit_via_pool (struct parsed_object_pool *p,const struct object_id *oid) { -struct commit *c =lookup_commit (r, oid); +struct commit *c =lookup_commit_via_pool (p, oid);if (!c->object.parsed)return ;
@@ -217,6 +222,11 @@ void unparse_commit(struct repository *r, const struct object_id *oid) c->object.parsed =0 ; } +void unparse_commit (struct repository *repo,const struct object_id *oid) +{ +unparse_commit_via_pool (repo->parsed_objects, oid); +} +int register_commit_graft (struct repository *r,struct commit_graft *graft,int ignore_dups) {
commit.h +3 -0
@@ -76,6 +76,8 @@ struct commit *lookup_commit_object(struct repository *r, const struct object_id* "oid" is not in the object cache. */ struct commit *lookup_commit (struct repository *r,const struct object_id *oid); +struct commit *lookup_commit_via_pool (struct parsed_object_pool *pool, +const struct object_id *oid);struct commit *lookup_commit_reference (struct repository *r,const struct object_id *oid); struct commit *lookup_commit_reference_gently(struct repository *r,
@@ -104,6 +106,7 @@ static inline int repo_parse_commit(struct repository *r, struct commit *item) }void unparse_commit (struct repository *r,const struct object_id *oid); +void unparse_commit_via_pool (struct parsed_object_pool *p,const struct object_id *oid);static inline int repo_parse_commit_no_graph (struct repository *r,struct commit *commit)
object.c +32 -23
128 lines not on this page · raw message
object.h +2 -1
@@ -7,7 +7,6 @@ struct buffer_slab;struct repository ;struct parsed_object_pool { -struct repository *repo;struct object **obj_hash;int nr_objs, obj_hash_size;
@@ -191,8 +190,10 @@ struct object *get_indexed_object(const struct repository *repo,* by calling parse_object() on them. */ struct object *lookup_object (struct repository *r,const struct object_id *oid); +struct object *lookup_object_via_pool (struct parsed_object_pool *p,const struct object_id *oid);void *create_object (struct repository *r,const struct object_id *oid,void *obj); +void *create_object_via_pool (struct parsed_object_pool *p,const struct object_id *oid,void *obj);void *object_as_type (struct object *obj,enum object_type type,int quiet);
Re: [PATCH 1/2] repository: make repo_clear() idempotent
Quoted 17 lines · Jeff King
Quoted 2 lines · Jeff King
I'm not sure how much it's worth untangling all of this, but probably not enough just to remove pointer indirection from
repo->parsed_objects.BTW, another curiosity:
parsed_objectscontains a pointer back to the repo that contains it! What could it possibly depend on in the repo itself?As far as I can tell, the answer is nothing. We only ever access
p->repoin order to get top->repo->parsed_objects, which will always be the same as our original "p". There are some internal functions withinobject.cwhich could be simplified by passing around theparsed_object_pooldirectly. But we also calllookup_commit()and a few other public functions, all of which take a repository struct. Even though they only use it to look at theparsed_objectsfield!
Quoted 4 lines
Structurally speaking these should be operating on a
parsed_object_pool, since that's all they need. But from the caller's point of view that is just an implementation detail, and it is easier to pass in the whole repository.
I was at one point wondering whether the parsed object pool should really be an implementation detail of the object database -- parsing objects should not have to depend on the repository, but it really should only interact with the object database. I hacked together a series, but it grew _huge_ because I was of course also trying to bubble up the changes into all subsystems that do parse objects rigth now. So I discarded that idea eventually.
But I think making the parsed object pool become more self-contained is a step into the right direction.
Quoted 8 lines
So we'd probably need to provide functions that operate directly on the pool like:
struct commit *lookup_commit_via_pool(struct
parsed_object_pool*p,const struct object_id *oid);and then maintain wrappers like:
Quoted 7 lines
struct commit *lookup_commit(struct repository *r,
const structobject_id*oid){ return lookup_commit_via_pool(r->parsed_objects, oid);}
to avoid rewriting every caller with
r->parsed_objectsthemselves.
I dunno. If we want to make this switch I'd say that we should go all or nothing. Otherwise, if we retain both interfaces, I don't really feel like it gains us anything at all.
Quoted 4 lines
The patch below illustrates the minimal change to drop the repo pointer from
parsed_object_pool. I think it more accurately represents the actual dependencies of the data structures, but it's a fair bit of churn for a minor amount of clarity. Probably not worth it.
I think there is value in it. While deglobalizing our state we tend to just pass the repository explicitly into the subsystems, which is a good step. But I think it really should only be the first step, where the next step would be to reduce the state we pass around. So ideally, subsystems should really only receive as input what they actually need. This would eventually ensure that our subsystems are more self-contained and that they can be used more flexibly.
Thanks!
Patrick
Re: [PATCH 1/2] repository: make repo_clear() idempotent
Quoted 3 lines · Patrick Steinhardt
I was at one point wondering whether the parsed object pool should really be an implementation detail of the object database -- parsing objects should not have to depend on the repository, ...
We need to be a bit careful here, as the above directly contradicts our earlier design choice to have things like hash algorithm as properties of a repository instance.
Quoted 2 lines
This would eventually ensure that our subsystems are more self-contained and that they can be used more flexibly.
I do agree with this line of thinking, though.
Thanks.
Re: [PATCH 1/2] repository: make repo_clear() idempotent
Quoted 9 lines · Junio C Hamano
Quoted 3 lines · Patrick Steinhardt
I was at one point wondering whether the parsed object pool should really be an implementation detail of the object database -- parsing objects should not have to depend on the repository, ...
We need to be a bit careful here, as the above directly contradicts our earlier design choice to have things like hash algorithm as properties of a repository instance.
Yeah, true. The question though is whether it really makes sense to always propagate down the full repository, or whether we should instead propagate only what matters. We do of course require the hash algorithm (and potentially the compatibility hash algorithm) in these subsystems, but propagating these two pieces of information might be the saner approach compared to always making the full repository available.
In any case, I eventually discarded the above idea anyway.
Patrick
PATCH 2/2 submodule--helper: free URL when repository setup fails
In reply to Jeff KingIf repo setup fails, we'll return an error without freeing the allocated url string, leaking the memory. The test suite does trigger this error, but never with the leak. We only allocate a url if submodule_from_path() returned something, but our tests use other situations, like totally nonexistent submodules.
We can cover this case by asking about a submodule that exists but which has not been initialized. The new test fails with SANITIZE=leak.
The smallest fix would just be a call to free(url), but I think it's a little nicer to set up a dedicated out-path for cleanup here. The previous commit made it safe to call repo_clear() even if repo_submodule_init() fails.
--- builtin/submodule--helper.c | 10 +++++++--- t/t7426-submodule-get-default-remote.sh | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-)
builtin/submodule--helper.c +7 -3
@@ -80,6 +80,7 @@ static int get_default_remote_submodule(const char *module_path, char **default_struct repository subrepo;const char *remote_name =NULL ;char *url =NULL ; +int ret =0 ; sub =submodule_from_path (the_repository,null_oid (the_hash_algo), module_path);if (sub && sub->url) {
@@ -96,9 +97,11 @@ static int get_default_remote_submodule(const char *module_path, char **default_ }if (repo_submodule_init (&subrepo, the_repository, module_path, -null_oid (the_hash_algo)) <0 ) -return die_message (_ ("could not get a repository handle for submodule '%s'" ), +null_oid (the_hash_algo)) <0 ) { + ret =die_message (_ ("could not get a repository handle for submodule '%s'" ), module_path); + goto out; + }/* Look up by URL first */ if (url)
@@ -108,10 +111,11 @@ static int get_default_remote_submodule(const char *module_path, char **default_ *default_remote =xstrdup (remote_name); +out:repo_clear (&subrepo);free (url); -return 0 ; +return ret; } static int module_get_default_remote(int argc, const char **argv, const char *prefix,
t/t7426-submodule-get-default-remote.sh +17 -0
@@ -60,6 +60,23 @@ test_expect_success 'get-default-remote fails with non-submodule path' ' ) ' +test_expect_success 'get-default-remote fails with uninitialized submodule' ' + test_when_finished " + git -C super config -f .gitmodules --remove-section submodule.uninitialized && + git -C super update-index --force-remove uninitialized + " && + ( + cd super && + git config -f .gitmodules submodule.uninitialized.path uninitialized && + git config -f .gitmodules submodule.uninitialized.url ../sub && + head=$(git -C ../sub rev-parse HEAD) && + git update-index --add --cacheinfo 160000,$head,uninitialized && + test_must_fail git submodule--helper get-default-remote \ + uninitialized 2>err && + test_grep "could not get a repository handle" err + ) +' + test_expect_success 'get-default-remote fails without path argument' ' ( cd super &&
Signature · 1 line
2.55.0.1067.gf7fc94a55c
Re: [PATCH 2/2] submodule--helper: free URL when repository setup fails
Quoted 6 lines · Jeff King
If repo setup fails, we'll return an error without freeing the allocated url string, leaking the memory. The test suite does trigger this error, but never with the leak. We only allocate a url if
submodule_from_path()returned something, but our tests use other situations, like totally nonexistent submodules.
Quoted 7 lines
We can cover this case by asking about a submodule that exists but which has not been initialized. The new test fails with SANITIZE=leak.
The smallest fix would just be a call to free(url), but I think it's a little nicer to set up a dedicated out-path for cleanup here. The previous commit made it safe to call
repo_clear()even ifrepo_submodule_init()fails.
Agreed.
Quoted 11 lines
Signed-off-by Jeff King <[email protected]>--- builtin/submodule--helper.c | 10 +++++++--- t/t7426-submodule-get-default-remote.sh | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-)builtin/submodule--helper.c +0
-0
builtin/submodule--helper.c static int get_default_remote_submodule(const char *module_path, char **default_ · line 86
Quoted 7 lines
builtin/submodule--helper.c +1
-0@@ -80,6 +80,7 @@ static int get_default_remote_submodule(const char *module_path, char **default_struct repository subrepo;const char *remote_name =NULL ;char *url =NULL ; +int ret =0 ; sub =submodule_from_path (the_repository,null_oid (the_hash_algo), module_path);if (sub && sub->url) {
Nit, feel free to ignore: do we want to keep the value uninitialized and...
Quoted 15 lines
builtin/submodule--helper.c +4
-2@@ -96,9 +97,11 @@ static int get_default_remote_submodule(const char *module_path, char **default_ }if (repo_submodule_init (&subrepo, the_repository, module_path, -null_oid (the_hash_algo)) <0 ) -return die_message (_ ("could not get a repository handle for submodule '%s'" ), +null_oid (the_hash_algo)) <0 ) { + ret =die_message (_ ("could not get a repository handle for submodule '%s'" ), module_path); + goto out; + }/* Look up by URL first */ if (url)
builtin/submodule--helper.c static int get_default_remote_submodule(const char *module_path, char **default_ · line 113
Quoted 3 lines
builtin/submodule--helper.c +0
-0@@ -108,10 +111,11 @@ static int get_default_remote_submodule(const char *module_path, char **default_ *default_remote =xstrdup (remote_name);
... set it to 0 here? Many compilers would warn in case the value was uninitialized, which ensures that the return value is being explicitly set before every goto out.
Quoted 14 lines
builtin/submodule--helper.c +2
-1+out:repo_clear (&subrepo);free (url); -return 0 ; +return ret; } static int module_get_default_remote(int argc, const char **argv, const char *prefix,t/t7426-submodule-get-default-remote.sh +0
-0
t/t7426-submodule-get-default-remote.sh test_expect_success 'get-default-remote fails with non-submodule path' ' · line 67
Quoted 8 lines
t/t7426-submodule-get-default-remote.sh +5
-0@@ -60,6 +60,23 @@ test_expect_success 'get-default-remote fails with non-submodule path' ' ) ' +test_expect_success 'get-default-remote fails with uninitialized submodule' ' + test_when_finished " + git -C super config -f .gitmodules --remove-section submodule.uninitialized && + git -C super update-index --force-remove uninitialized + " &&
I was about to say we could use test_config instead, but you're of course not modifying the normal ".git/config" file but ".gitmodules".
t/t7426-submodule-get-default-remote.sh test_expect_success 'get-default-remote fails with non-submodule path' ' · line 78
Quoted 11 lines
t/t7426-submodule-get-default-remote.sh +11
-0+ ( + cd super && + git config -f .gitmodules submodule.uninitialized.path uninitialized && + git config -f .gitmodules submodule.uninitialized.url ../sub && + head=$(git -C ../sub rev-parse HEAD) && + git update-index --add --cacheinfo 160000,$head,uninitialized && + test_must_fail git submodule--helper get-default-remote \ + uninitialized 2>err && + test_grep "could not get a repository handle" err + ) +'
Thanks!
Patrick