Commit 2afb6fa4 by Patrick Steinhardt

rebase: plug memory leak in `rebase_alloc`

Convert `rebase_alloc` to use our usual error propagation
patterns, that is accept an out-parameter and return an error
code that is to be checked by the caller. This allows us to use
the GITERR_CHECK_ALLOC macro, which helps static analysis.
parent d0cb11e7
...@@ -257,12 +257,12 @@ done: ...@@ -257,12 +257,12 @@ done:
return error; return error;
} }
static git_rebase *rebase_alloc(const git_rebase_options *rebase_opts) static int rebase_alloc(git_rebase **out, const git_rebase_options *rebase_opts)
{ {
git_rebase *rebase = git__calloc(1, sizeof(git_rebase)); git_rebase *rebase = git__calloc(1, sizeof(git_rebase));
GITERR_CHECK_ALLOC(rebase);
if (!rebase) *out = NULL;
return NULL;
if (rebase_opts) if (rebase_opts)
memcpy(&rebase->options, rebase_opts, sizeof(git_rebase_options)); memcpy(&rebase->options, rebase_opts, sizeof(git_rebase_options));
...@@ -270,14 +270,16 @@ static git_rebase *rebase_alloc(const git_rebase_options *rebase_opts) ...@@ -270,14 +270,16 @@ static git_rebase *rebase_alloc(const git_rebase_options *rebase_opts)
git_rebase_init_options(&rebase->options, GIT_REBASE_OPTIONS_VERSION); git_rebase_init_options(&rebase->options, GIT_REBASE_OPTIONS_VERSION);
if (rebase_opts && rebase_opts->rewrite_notes_ref) { if (rebase_opts && rebase_opts->rewrite_notes_ref) {
if ((rebase->options.rewrite_notes_ref = git__strdup(rebase_opts->rewrite_notes_ref)) == NULL) rebase->options.rewrite_notes_ref = git__strdup(rebase_opts->rewrite_notes_ref);
return NULL; GITERR_CHECK_ALLOC(rebase->options.rewrite_notes_ref);
} }
if ((rebase->options.checkout_options.checkout_strategy & (GIT_CHECKOUT_SAFE | GIT_CHECKOUT_FORCE)) == 0) if ((rebase->options.checkout_options.checkout_strategy & (GIT_CHECKOUT_SAFE | GIT_CHECKOUT_FORCE)) == 0)
rebase->options.checkout_options.checkout_strategy = GIT_CHECKOUT_SAFE; rebase->options.checkout_options.checkout_strategy = GIT_CHECKOUT_SAFE;
return rebase; *out = rebase;
return 0;
} }
static int rebase_check_versions(const git_rebase_options *given_opts) static int rebase_check_versions(const git_rebase_options *given_opts)
...@@ -305,8 +307,8 @@ int git_rebase_open( ...@@ -305,8 +307,8 @@ int git_rebase_open(
if ((error = rebase_check_versions(given_opts)) < 0) if ((error = rebase_check_versions(given_opts)) < 0)
return error; return error;
rebase = rebase_alloc(given_opts); if (rebase_alloc(&rebase, given_opts) < 0)
GITERR_CHECK_ALLOC(rebase); return -1;
rebase->repo = repo; rebase->repo = repo;
...@@ -708,8 +710,8 @@ int git_rebase_init( ...@@ -708,8 +710,8 @@ int git_rebase_init(
branch = head_branch; branch = head_branch;
} }
rebase = rebase_alloc(given_opts); if (rebase_alloc(&rebase, given_opts) < 0)
GITERR_CHECK_ALLOC(rebase); return -1;
rebase->repo = repo; rebase->repo = repo;
rebase->inmemory = inmemory; rebase->inmemory = inmemory;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment