reftable: handle trivial allocation failures

Handle trivial allocation failures in the reftable library and its unit tests. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Oct 2, 2024 at 12:56 UTC 12b90780667ac1e1235ec4106d94c558a28880f7
7 files changed +67 -14
reftable/merged.c
+3
@@ -203,6 +203,9 @@ int reftable_merged_table_new(struct reftable_merged_table **dest,
203 }
204
205 REFTABLE_CALLOC_ARRAY(m, 1);
206 + if (!m)
207 + return REFTABLE_OUT_OF_MEMORY_ERROR;
208 +
209 m->readers = readers;
210 m->readers_len = n;
211 m->min = first_min;
reftable/reader.c
+9 -1
@@ -598,6 +598,10 @@ int reftable_reader_new(struct reftable_reader **out,
598 int err;
599
600 REFTABLE_CALLOC_ARRAY(r, 1);
601 + if (!r) {
602 + err = REFTABLE_OUT_OF_MEMORY_ERROR;
603 + goto done;
604 + }
605
606 /*
607 * 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,
631
632 r->size = file_size - footer_size(r->version);
633 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 + }
639 r->hash_id = 0;
640 r->refcount = 1;
641
reftable/stack.c
+20
@@ -116,6 +116,11 @@ static int fd_read_lines(int fd, char ***namesp)
116 }
117
118 REFTABLE_ALLOC_ARRAY(buf, size + 1);
119 + if (!buf) {
120 + err = REFTABLE_OUT_OF_MEMORY_ERROR;
121 + goto done;
122 + }
123 +
124 if (read_in_full(fd, buf, size) != size) {
125 err = REFTABLE_IO_ERROR;
126 goto done;
@@ -140,6 +145,8 @@ int read_lines(const char *filename, char ***namesp)
145 if (fd < 0) {
146 if (errno == ENOENT) {
147 REFTABLE_CALLOC_ARRAY(*namesp, 1);
148 + if (!*namesp)
149 + return REFTABLE_OUT_OF_MEMORY_ERROR;
150 return 0;
151 }
152
@@ -420,6 +427,10 @@ static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,
427 }
428
429 REFTABLE_CALLOC_ARRAY(names, 1);
430 + if (!names) {
431 + err = REFTABLE_OUT_OF_MEMORY_ERROR;
432 + goto out;
433 + }
434 } else {
435 err = fd_read_lines(fd, &names);
436 if (err < 0)
@@ -779,7 +790,11 @@ int reftable_stack_new_addition(struct reftable_addition **dest,
790 {
791 int err = 0;
792 struct reftable_addition empty = REFTABLE_ADDITION_INIT;
793 +
794 REFTABLE_CALLOC_ARRAY(*dest, 1);
795 + if (!*dest)
796 + return REFTABLE_OUT_OF_MEMORY_ERROR;
797 +
798 **dest = empty;
799 err = reftable_stack_init_addition(*dest, st);
800 if (err) {
@@ -886,7 +901,12 @@ int reftable_addition_add(struct reftable_addition *add,
901
902 REFTABLE_ALLOC_GROW(add->new_tables, add->new_tables_len + 1,
903 add->new_tables_cap);
904 + if (!add->new_tables) {
905 + err = REFTABLE_OUT_OF_MEMORY_ERROR;
906 + goto done;
907 + }
908 add->new_tables[add->new_tables_len++] = strbuf_detach(&next_name, NULL);
909 +
910 done:
911 delete_tempfile(&tab_file);
912 strbuf_release(&temp_tab_file_name);
reftable/writer.c
+11 -2
@@ -49,8 +49,14 @@ static int padded_write(struct reftable_writer *w, uint8_t *data, size_t len,
49 {
50 int n = 0;
51 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);
60 if (n < 0)
61 return n;
62
@@ -767,6 +773,9 @@ static int writer_flush_nonempty_block(struct reftable_writer *w)
773 * case we will end up with a multi-level index.
774 */
775 REFTABLE_ALLOC_GROW(w->index, w->index_len + 1, w->index_cap);
776 + if (!w->index)
777 + return REFTABLE_OUT_OF_MEMORY_ERROR;
778 +
779 index_record.offset = w->next;
780 strbuf_reset(&index_record.last_key);
781 strbuf_addbuf(&index_record.last_key, &w->block_writer->last_key);
t/unit-tests/t-reftable-block.c
+4
@@ -32,6 +32,7 @@ static void t_ref_block_read_write(void)
32 struct strbuf want = STRBUF_INIT, buf = STRBUF_INIT;
33
34 REFTABLE_CALLOC_ARRAY(block.data, block_size);
35 + check(block.data != NULL);
36 block.len = block_size;
37 block_source_from_strbuf(&block.source ,&buf);
38 ret = block_writer_init(&bw, BLOCK_TYPE_REF, block.data, block_size,
@@ -125,6 +126,7 @@ static void t_log_block_read_write(void)
126 struct strbuf want = STRBUF_INIT, buf = STRBUF_INIT;
127
128 REFTABLE_CALLOC_ARRAY(block.data, block_size);
129 + check(block.data != NULL);
130 block.len = block_size;
131 block_source_from_strbuf(&block.source ,&buf);
132 ret = block_writer_init(&bw, BLOCK_TYPE_LOG, block.data, block_size,
@@ -214,6 +216,7 @@ static void t_obj_block_read_write(void)
216 struct strbuf want = STRBUF_INIT, buf = STRBUF_INIT;
217
218 REFTABLE_CALLOC_ARRAY(block.data, block_size);
219 + check(block.data != NULL);
220 block.len = block_size;
221 block_source_from_strbuf(&block.source, &buf);
222 ret = block_writer_init(&bw, BLOCK_TYPE_OBJ, block.data, block_size,
@@ -297,6 +300,7 @@ static void t_index_block_read_write(void)
300 struct strbuf want = STRBUF_INIT, buf = STRBUF_INIT;
301
302 REFTABLE_CALLOC_ARRAY(block.data, block_size);
303 + check(block.data != NULL);
304 block.len = block_size;
305 block_source_from_strbuf(&block.source, &buf);
306 ret = block_writer_init(&bw, BLOCK_TYPE_INDEX, block.data, block_size,
t/unit-tests/t-reftable-merged.c
+4
@@ -29,7 +29,9 @@ merged_table_from_records(struct reftable_ref_record **refs,
29 int err;
30
31 REFTABLE_CALLOC_ARRAY(*readers, n);
32 + check(*readers != NULL);
33 REFTABLE_CALLOC_ARRAY(*source, n);
34 + check(*source != NULL);
35
36 for (size_t i = 0; i < n; i++) {
37 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,
287 int err;
288
289 REFTABLE_CALLOC_ARRAY(*readers, n);
290 + check(*readers != NULL);
291 REFTABLE_CALLOC_ARRAY(*source, n);
292 + check(*source != NULL);
293
294 for (size_t i = 0; i < n; i++) {
295 t_reftable_write_to_buf(&buf[i], NULL, 0, logs[i], sizes[i], &opts);
t/unit-tests/t-reftable-readwrite.c
+16 -11
@@ -52,8 +52,11 @@ static void write_table(char ***names, struct strbuf *buf, int N,
52 int i;
53
54 REFTABLE_CALLOC_ARRAY(*names, N + 1);
55 + check(*names != NULL);
56 REFTABLE_CALLOC_ARRAY(refs, N);
57 + check(refs != NULL);
58 REFTABLE_CALLOC_ARRAY(logs, N);
59 + check(logs != NULL);
60
61 for (i = 0; i < N; i++) {
62 refs[i].refname = (*names)[i] = xstrfmt("refs/heads/branch%02d", i);
@@ -150,23 +153,25 @@ static void t_log_overflow(void)
153
154 static void t_log_write_read(void)
155 {
153 - int N = 2;
154 - char **names = reftable_calloc(N + 1, sizeof(*names));
155 - int err;
156 struct reftable_write_options opts = {
157 .block_size = 256,
158 };
159 struct reftable_ref_record ref = { 0 };
160 - int i = 0;
160 struct reftable_log_record log = { 0 };
162 - int n;
161 struct reftable_iterator it = { 0 };
162 struct reftable_reader *reader;
163 struct reftable_block_source source = { 0 };
164 struct strbuf buf = STRBUF_INIT;
165 struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts);
166 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 +
173 reftable_writer_set_limits(w, 0, N);
174 +
175 for (i = 0; i < N; i++) {
176 char name[256];
177 struct reftable_ref_record ref = { 0 };
@@ -178,6 +183,7 @@ static void t_log_write_read(void)
183 err = reftable_writer_add_ref(w, &ref);
184 check(!err);
185 }
186 +
187 for (i = 0; i < N; i++) {
188 struct reftable_log_record log = { 0 };
189
@@ -476,8 +482,7 @@ static void t_table_read_write_seek_index(void)
482
483 static void t_table_refs_for(int indexed)
484 {
479 - int N = 50;
480 - char **want_names = reftable_calloc(N + 1, sizeof(*want_names));
485 + char **want_names;
486 int want_names_len = 0;
487 uint8_t want_hash[GIT_SHA1_RAWSZ];
488
@@ -485,15 +490,15 @@ static void t_table_refs_for(int indexed)
490 .block_size = 256,
491 };
492 struct reftable_ref_record ref = { 0 };
488 - int i = 0;
489 - int n;
490 - int err;
493 struct reftable_reader *reader;
494 struct reftable_block_source source = { 0 };
495 struct strbuf buf = STRBUF_INIT;
496 struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts);
497 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);
502
503 t_reftable_set_hash(want_hash, 4, GIT_SHA1_FORMAT_ID);
504