Skip to content

Commit 12b9078

Browse files
pks-tgitster
authored andcommitted
reftable: handle trivial allocation failures
Handle trivial allocation failures in the reftable library and its unit tests. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 51afc70 commit 12b9078

File tree

7 files changed

+67
-14
lines changed

7 files changed

+67
-14
lines changed

reftable/merged.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ int reftable_merged_table_new(struct reftable_merged_table **dest,
203203
}
204204

205205
REFTABLE_CALLOC_ARRAY(m, 1);
206+
if (!m)
207+
return REFTABLE_OUT_OF_MEMORY_ERROR;
208+
206209
m->readers = readers;
207210
m->readers_len = n;
208211
m->min = first_min;

reftable/reader.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,10 @@ int reftable_reader_new(struct reftable_reader **out,
598598
int err;
599599

600600
REFTABLE_CALLOC_ARRAY(r, 1);
601+
if (!r) {
602+
err = REFTABLE_OUT_OF_MEMORY_ERROR;
603+
goto done;
604+
}
601605

602606
/*
603607
* We need one extra byte to read the type of first block. We also
@@ -627,7 +631,11 @@ int reftable_reader_new(struct reftable_reader **out,
627631

628632
r->size = file_size - footer_size(r->version);
629633
r->source = *source;
630-
r->name = xstrdup(name);
634+
r->name = reftable_strdup(name);
635+
if (!r->name) {
636+
err = REFTABLE_OUT_OF_MEMORY_ERROR;
637+
goto done;
638+
}
631639
r->hash_id = 0;
632640
r->refcount = 1;
633641

reftable/stack.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ static int fd_read_lines(int fd, char ***namesp)
116116
}
117117

118118
REFTABLE_ALLOC_ARRAY(buf, size + 1);
119+
if (!buf) {
120+
err = REFTABLE_OUT_OF_MEMORY_ERROR;
121+
goto done;
122+
}
123+
119124
if (read_in_full(fd, buf, size) != size) {
120125
err = REFTABLE_IO_ERROR;
121126
goto done;
@@ -140,6 +145,8 @@ int read_lines(const char *filename, char ***namesp)
140145
if (fd < 0) {
141146
if (errno == ENOENT) {
142147
REFTABLE_CALLOC_ARRAY(*namesp, 1);
148+
if (!*namesp)
149+
return REFTABLE_OUT_OF_MEMORY_ERROR;
143150
return 0;
144151
}
145152

@@ -420,6 +427,10 @@ static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,
420427
}
421428

422429
REFTABLE_CALLOC_ARRAY(names, 1);
430+
if (!names) {
431+
err = REFTABLE_OUT_OF_MEMORY_ERROR;
432+
goto out;
433+
}
423434
} else {
424435
err = fd_read_lines(fd, &names);
425436
if (err < 0)
@@ -779,7 +790,11 @@ int reftable_stack_new_addition(struct reftable_addition **dest,
779790
{
780791
int err = 0;
781792
struct reftable_addition empty = REFTABLE_ADDITION_INIT;
793+
782794
REFTABLE_CALLOC_ARRAY(*dest, 1);
795+
if (!*dest)
796+
return REFTABLE_OUT_OF_MEMORY_ERROR;
797+
783798
**dest = empty;
784799
err = reftable_stack_init_addition(*dest, st);
785800
if (err) {
@@ -886,7 +901,12 @@ int reftable_addition_add(struct reftable_addition *add,
886901

887902
REFTABLE_ALLOC_GROW(add->new_tables, add->new_tables_len + 1,
888903
add->new_tables_cap);
904+
if (!add->new_tables) {
905+
err = REFTABLE_OUT_OF_MEMORY_ERROR;
906+
goto done;
907+
}
889908
add->new_tables[add->new_tables_len++] = strbuf_detach(&next_name, NULL);
909+
890910
done:
891911
delete_tempfile(&tab_file);
892912
strbuf_release(&temp_tab_file_name);

reftable/writer.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ static int padded_write(struct reftable_writer *w, uint8_t *data, size_t len,
4949
{
5050
int n = 0;
5151
if (w->pending_padding > 0) {
52-
uint8_t *zeroed = reftable_calloc(w->pending_padding, sizeof(*zeroed));
53-
int n = w->write(w->write_arg, zeroed, w->pending_padding);
52+
uint8_t *zeroed;
53+
int n;
54+
55+
zeroed = reftable_calloc(w->pending_padding, sizeof(*zeroed));
56+
if (!zeroed)
57+
return -1;
58+
59+
n = w->write(w->write_arg, zeroed, w->pending_padding);
5460
if (n < 0)
5561
return n;
5662

@@ -767,6 +773,9 @@ static int writer_flush_nonempty_block(struct reftable_writer *w)
767773
* case we will end up with a multi-level index.
768774
*/
769775
REFTABLE_ALLOC_GROW(w->index, w->index_len + 1, w->index_cap);
776+
if (!w->index)
777+
return REFTABLE_OUT_OF_MEMORY_ERROR;
778+
770779
index_record.offset = w->next;
771780
strbuf_reset(&index_record.last_key);
772781
strbuf_addbuf(&index_record.last_key, &w->block_writer->last_key);

t/unit-tests/t-reftable-block.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ static void t_ref_block_read_write(void)
3232
struct strbuf want = STRBUF_INIT, buf = STRBUF_INIT;
3333

3434
REFTABLE_CALLOC_ARRAY(block.data, block_size);
35+
check(block.data != NULL);
3536
block.len = block_size;
3637
block_source_from_strbuf(&block.source ,&buf);
3738
ret = block_writer_init(&bw, BLOCK_TYPE_REF, block.data, block_size,
@@ -125,6 +126,7 @@ static void t_log_block_read_write(void)
125126
struct strbuf want = STRBUF_INIT, buf = STRBUF_INIT;
126127

127128
REFTABLE_CALLOC_ARRAY(block.data, block_size);
129+
check(block.data != NULL);
128130
block.len = block_size;
129131
block_source_from_strbuf(&block.source ,&buf);
130132
ret = block_writer_init(&bw, BLOCK_TYPE_LOG, block.data, block_size,
@@ -214,6 +216,7 @@ static void t_obj_block_read_write(void)
214216
struct strbuf want = STRBUF_INIT, buf = STRBUF_INIT;
215217

216218
REFTABLE_CALLOC_ARRAY(block.data, block_size);
219+
check(block.data != NULL);
217220
block.len = block_size;
218221
block_source_from_strbuf(&block.source, &buf);
219222
ret = block_writer_init(&bw, BLOCK_TYPE_OBJ, block.data, block_size,
@@ -297,6 +300,7 @@ static void t_index_block_read_write(void)
297300
struct strbuf want = STRBUF_INIT, buf = STRBUF_INIT;
298301

299302
REFTABLE_CALLOC_ARRAY(block.data, block_size);
303+
check(block.data != NULL);
300304
block.len = block_size;
301305
block_source_from_strbuf(&block.source, &buf);
302306
ret = block_writer_init(&bw, BLOCK_TYPE_INDEX, block.data, block_size,

t/unit-tests/t-reftable-merged.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ merged_table_from_records(struct reftable_ref_record **refs,
2929
int err;
3030

3131
REFTABLE_CALLOC_ARRAY(*readers, n);
32+
check(*readers != NULL);
3233
REFTABLE_CALLOC_ARRAY(*source, n);
34+
check(*source != NULL);
3335

3436
for (size_t i = 0; i < n; i++) {
3537
t_reftable_write_to_buf(&buf[i], refs[i], sizes[i], NULL, 0, &opts);
@@ -285,7 +287,9 @@ merged_table_from_log_records(struct reftable_log_record **logs,
285287
int err;
286288

287289
REFTABLE_CALLOC_ARRAY(*readers, n);
290+
check(*readers != NULL);
288291
REFTABLE_CALLOC_ARRAY(*source, n);
292+
check(*source != NULL);
289293

290294
for (size_t i = 0; i < n; i++) {
291295
t_reftable_write_to_buf(&buf[i], NULL, 0, logs[i], sizes[i], &opts);

t/unit-tests/t-reftable-readwrite.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ static void write_table(char ***names, struct strbuf *buf, int N,
5252
int i;
5353

5454
REFTABLE_CALLOC_ARRAY(*names, N + 1);
55+
check(*names != NULL);
5556
REFTABLE_CALLOC_ARRAY(refs, N);
57+
check(refs != NULL);
5658
REFTABLE_CALLOC_ARRAY(logs, N);
59+
check(logs != NULL);
5760

5861
for (i = 0; i < N; i++) {
5962
refs[i].refname = (*names)[i] = xstrfmt("refs/heads/branch%02d", i);
@@ -150,23 +153,25 @@ static void t_log_overflow(void)
150153

151154
static void t_log_write_read(void)
152155
{
153-
int N = 2;
154-
char **names = reftable_calloc(N + 1, sizeof(*names));
155-
int err;
156156
struct reftable_write_options opts = {
157157
.block_size = 256,
158158
};
159159
struct reftable_ref_record ref = { 0 };
160-
int i = 0;
161160
struct reftable_log_record log = { 0 };
162-
int n;
163161
struct reftable_iterator it = { 0 };
164162
struct reftable_reader *reader;
165163
struct reftable_block_source source = { 0 };
166164
struct strbuf buf = STRBUF_INIT;
167165
struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts);
168166
const struct reftable_stats *stats = NULL;
167+
int N = 2, err, i, n;
168+
char **names;
169+
170+
names = reftable_calloc(N + 1, sizeof(*names));
171+
check(names != NULL);
172+
169173
reftable_writer_set_limits(w, 0, N);
174+
170175
for (i = 0; i < N; i++) {
171176
char name[256];
172177
struct reftable_ref_record ref = { 0 };
@@ -178,6 +183,7 @@ static void t_log_write_read(void)
178183
err = reftable_writer_add_ref(w, &ref);
179184
check(!err);
180185
}
186+
181187
for (i = 0; i < N; i++) {
182188
struct reftable_log_record log = { 0 };
183189

@@ -476,24 +482,23 @@ static void t_table_read_write_seek_index(void)
476482

477483
static void t_table_refs_for(int indexed)
478484
{
479-
int N = 50;
480-
char **want_names = reftable_calloc(N + 1, sizeof(*want_names));
485+
char **want_names;
481486
int want_names_len = 0;
482487
uint8_t want_hash[GIT_SHA1_RAWSZ];
483488

484489
struct reftable_write_options opts = {
485490
.block_size = 256,
486491
};
487492
struct reftable_ref_record ref = { 0 };
488-
int i = 0;
489-
int n;
490-
int err;
491493
struct reftable_reader *reader;
492494
struct reftable_block_source source = { 0 };
493495
struct strbuf buf = STRBUF_INIT;
494496
struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts);
495497
struct reftable_iterator it = { 0 };
496-
int j;
498+
int N = 50, n, j, err, i;
499+
500+
want_names = reftable_calloc(N + 1, sizeof(*want_names));
501+
check(want_names != NULL);
497502

498503
t_reftable_set_hash(want_hash, 4, GIT_SHA1_FORMAT_ID);
499504

0 commit comments

Comments
 (0)