Skip to content

Commit 5dbe266

Browse files
pks-tgitster
authored andcommitted
reftable/stack: handle allocation failures in reftable_new_stack()
Handle allocation failures in `reftable_new_stack()`. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dce75e1 commit 5dbe266

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

reftable/stack.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,16 @@ static int reftable_fd_flush(void *arg)
5656
int reftable_new_stack(struct reftable_stack **dest, const char *dir,
5757
const struct reftable_write_options *_opts)
5858
{
59-
struct reftable_stack *p = reftable_calloc(1, sizeof(*p));
6059
struct strbuf list_file_name = STRBUF_INIT;
61-
struct reftable_write_options opts = {0};
62-
int err = 0;
60+
struct reftable_write_options opts = { 0 };
61+
struct reftable_stack *p;
62+
int err;
63+
64+
p = reftable_calloc(1, sizeof(*p));
65+
if (!p) {
66+
err = REFTABLE_OUT_OF_MEMORY_ERROR;
67+
goto out;
68+
}
6369

6470
if (_opts)
6571
opts = *_opts;
@@ -74,15 +80,23 @@ int reftable_new_stack(struct reftable_stack **dest, const char *dir,
7480

7581
p->list_file = strbuf_detach(&list_file_name, NULL);
7682
p->list_fd = -1;
77-
p->reftable_dir = xstrdup(dir);
7883
p->opts = opts;
84+
p->reftable_dir = reftable_strdup(dir);
85+
if (!p->reftable_dir) {
86+
err = REFTABLE_OUT_OF_MEMORY_ERROR;
87+
goto out;
88+
}
7989

8090
err = reftable_stack_reload_maybe_reuse(p, 1);
81-
if (err < 0) {
91+
if (err < 0)
92+
goto out;
93+
94+
*dest = p;
95+
err = 0;
96+
97+
out:
98+
if (err < 0)
8299
reftable_stack_destroy(p);
83-
} else {
84-
*dest = p;
85-
}
86100
return err;
87101
}
88102

@@ -171,6 +185,10 @@ void reftable_stack_destroy(struct reftable_stack *st)
171185
{
172186
char **names = NULL;
173187
int err = 0;
188+
189+
if (!st)
190+
return;
191+
174192
if (st->merged) {
175193
reftable_merged_table_free(st->merged);
176194
st->merged = NULL;

0 commit comments

Comments
 (0)