From a53d2e3985505de0e1dcc580cd2c624fcf0fce93 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 9 Feb 2016 09:58:56 +0100 Subject: [PATCH 1/5] pack: do not free passed in poiter on error The function `git_packfile_stream_open` tries to free the passed in stream when an error occurs. The only call site is `git_indexer_append`, though, which passes in the address of a stream struct which has not been allocated on the heap. Fix the issue by simply removing the call to free. In case of an error we did not allocate any memory yet and otherwise it should be the caller's responsibility to manage it's object's lifetime. --- src/pack.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pack.c b/src/pack.c index f6cb3a548..081e37084 100644 --- a/src/pack.c +++ b/src/pack.c @@ -790,7 +790,6 @@ int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p, obj->zstream.next_out = Z_NULL; st = inflateInit(&obj->zstream); if (st != Z_OK) { - git__free(obj); giterr_set(GITERR_ZLIB, "failed to init packfile stream"); return -1; } From 0b2437bb680897424e208464312fe2d4610cc4b6 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 9 Feb 2016 10:43:28 +0100 Subject: [PATCH 2/5] pack-objects: fix memory leak in compute_write_order --- src/pack-objects.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pack-objects.c b/src/pack-objects.c index fd181fc5e..3046d941d 100644 --- a/src/pack-objects.c +++ b/src/pack-objects.c @@ -605,6 +605,7 @@ static git_pobject **compute_write_order(git_packbuilder *pb) } if (wo_end != pb->nr_objects) { + git__free(wo); giterr_set(GITERR_INVALID, "invalid write order"); return NULL; } From 39c9dd24d3556bc4eec584ac2165f2a01de64da1 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 9 Feb 2016 10:53:30 +0100 Subject: [PATCH 3/5] pack-objects: fix memory leak in packbuilder_config --- src/pack-objects.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/pack-objects.c b/src/pack-objects.c index 3046d941d..0afa28e62 100644 --- a/src/pack-objects.c +++ b/src/pack-objects.c @@ -91,7 +91,7 @@ static unsigned name_hash(const char *name) static int packbuilder_config(git_packbuilder *pb) { git_config *config; - int ret; + int ret = 0; int64_t val; if ((ret = git_repository_config_snapshot(&config, pb->repo)) < 0) @@ -100,8 +100,10 @@ static int packbuilder_config(git_packbuilder *pb) #define config_get(KEY,DST,DFLT) do { \ ret = git_config_get_int64(&val, config, KEY); \ if (!ret) (DST) = val; \ - else if (ret == GIT_ENOTFOUND) (DST) = (DFLT); \ - else if (ret < 0) return -1; } while (0) + else if (ret == GIT_ENOTFOUND) { \ + (DST) = (DFLT); \ + ret = 0; \ + } else if (ret < 0) goto out; } while (0) config_get("pack.deltaCacheSize", pb->max_delta_cache_size, GIT_PACK_DELTA_CACHE_SIZE); @@ -113,9 +115,10 @@ static int packbuilder_config(git_packbuilder *pb) #undef config_get +out: git_config_free(config); - return 0; + return ret; } int git_packbuilder_new(git_packbuilder **out, git_repository *repo) From e26254575306abbc39acd96c6dc54a07aa3b2aa4 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 9 Feb 2016 11:07:50 +0100 Subject: [PATCH 4/5] checkout: fix resource leak --- src/checkout.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/checkout.c b/src/checkout.c index a92ad0825..fd8e2c443 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -1487,8 +1487,10 @@ static int blob_content_to_file( if (!data->opts.disable_filters && (error = git_filter_list__load_ext( &fl, data->repo, blob, hint_path, - GIT_FILTER_TO_WORKTREE, &filter_opts))) + GIT_FILTER_TO_WORKTREE, &filter_opts))) { + p_close(fd); return error; + } /* setup the writer */ memset(&writer, 0, sizeof(struct checkout_stream)); From 24b8ed2b3ae4519e9c56afcfdf1d0f00a2b95842 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 9 Feb 2016 11:11:38 +0100 Subject: [PATCH 5/5] attr_file: fix resource leak --- src/attr_file.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/attr_file.c b/src/attr_file.c index 500c99bd9..11d149358 100644 --- a/src/attr_file.c +++ b/src/attr_file.c @@ -123,7 +123,7 @@ int git_attr_file__load( break; } case GIT_ATTR_FILE__FROM_FILE: { - int fd; + int fd = -1; /* For open or read errors, pretend that we got ENOTFOUND. */ /* TODO: issue warning when warning API is available */ @@ -133,7 +133,8 @@ int git_attr_file__load( (fd = git_futils_open_ro(entry->fullpath)) < 0 || (error = git_futils_readbuffer_fd(&content, fd, (size_t)st.st_size)) < 0) nonexistent = true; - else + + if (fd >= 0) p_close(fd); break;