Skip to content

Commit 31eedd1

Browse files
pks-tttaylorr
authored andcommitted
t/unit-tests: check for reftable_buf allocation errors
Adapt our unit tests to check for allocations errors. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent f177d49 commit 31eedd1

File tree

5 files changed

+44
-44
lines changed

5 files changed

+44
-44
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
113113
};
114114

115115
for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
116-
reftable_buf_addstr(&a, cases[i].a);
117-
reftable_buf_addstr(&b, cases[i].b);
116+
check(!reftable_buf_addstr(&a, cases[i].a));
117+
check(!reftable_buf_addstr(&b, cases[i].b));
118118
check_int(common_prefix_size(&a, &b), ==, cases[i].want);
119119
reftable_buf_reset(&a);
120120
reftable_buf_reset(&b);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static void t_log_block_read_write(void)
167167
for (i = 0; i < N; i++) {
168168
block_iter_reset(&it);
169169
reftable_buf_reset(&want);
170-
reftable_buf_addstr(&want, recs[i].u.log.refname);
170+
check(!reftable_buf_addstr(&want, recs[i].u.log.refname));
171171

172172
ret = block_iter_seek_key(&it, &br, &want);
173173
check_int(ret, ==, 0);
@@ -314,7 +314,7 @@ static void t_index_block_read_write(void)
314314

315315
reftable_buf_init(&recs[i].u.idx.last_key);
316316
recs[i].type = BLOCK_TYPE_INDEX;
317-
reftable_buf_addstr(&recs[i].u.idx.last_key, buf);
317+
check(!reftable_buf_addstr(&recs[i].u.idx.last_key, buf));
318318
recs[i].u.idx.offset = i;
319319

320320
ret = block_writer_add(&bw, &recs[i]);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static void t_buffer(void)
2323
struct reftable_block out = { 0 };
2424
int n;
2525
uint8_t in[] = "hello";
26-
reftable_buf_add(&buf, in, sizeof(in));
26+
check(!reftable_buf_add(&buf, in, sizeof(in)));
2727
block_source_from_buf(&source, &buf);
2828
check_int(block_source_size(&source), ==, 6);
2929
n = block_source_read_block(&source, &out, 0, sizeof(in));
@@ -443,8 +443,8 @@ static void t_table_read_write_seek(int index, int hash_id)
443443
reftable_iterator_destroy(&it);
444444
}
445445

446-
reftable_buf_addstr(&pastLast, names[N - 1]);
447-
reftable_buf_addstr(&pastLast, "/");
446+
check(!reftable_buf_addstr(&pastLast, names[N - 1]));
447+
check(!reftable_buf_addstr(&pastLast, "/"));
448448

449449
err = reftable_reader_init_ref_iterator(reader, &it);
450450
check(!err);
@@ -901,7 +901,7 @@ static void t_corrupt_table(void)
901901
struct reftable_block_source source = { 0 };
902902
struct reftable_reader *reader;
903903
int err;
904-
reftable_buf_add(&buf, zeros, sizeof(zeros));
904+
check(!reftable_buf_add(&buf, zeros, sizeof(zeros)));
905905

906906
block_source_from_buf(&source, &buf);
907907
err = reftable_reader_new(&reader, &source, "file.log");

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,14 @@ static void t_key_roundtrip(void)
335335
int n, m;
336336
uint8_t rt_extra;
337337

338-
reftable_buf_addstr(&last_key, "refs/heads/master");
339-
reftable_buf_addstr(&key, "refs/tags/bla");
338+
check(!reftable_buf_addstr(&last_key, "refs/heads/master"));
339+
check(!reftable_buf_addstr(&key, "refs/tags/bla"));
340340
extra = 6;
341341
n = reftable_encode_key(&restart, dest, last_key, key, extra);
342342
check(!restart);
343343
check_int(n, >, 0);
344344

345-
reftable_buf_addstr(&roundtrip, "refs/heads/master");
345+
check(!reftable_buf_addstr(&roundtrip, "refs/heads/master"));
346346
m = reftable_decode_key(&roundtrip, &rt_extra, dest);
347347
check_int(n, ==, m);
348348
check(!reftable_buf_cmp(&key, &roundtrip));
@@ -469,9 +469,9 @@ static void t_reftable_index_record_comparison(void)
469469
.u.idx.last_key = REFTABLE_BUF_INIT,
470470
},
471471
};
472-
reftable_buf_addstr(&in[0].u.idx.last_key, "refs/heads/master");
473-
reftable_buf_addstr(&in[1].u.idx.last_key, "refs/heads/master");
474-
reftable_buf_addstr(&in[2].u.idx.last_key, "refs/heads/branch");
472+
check(!reftable_buf_addstr(&in[0].u.idx.last_key, "refs/heads/master"));
473+
check(!reftable_buf_addstr(&in[1].u.idx.last_key, "refs/heads/master"));
474+
check(!reftable_buf_addstr(&in[2].u.idx.last_key, "refs/heads/branch"));
475475

476476
check(!reftable_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
477477
check(!reftable_record_cmp(&in[0], &in[1]));
@@ -510,7 +510,7 @@ static void t_reftable_index_record_roundtrip(void)
510510
int n, m;
511511
uint8_t extra;
512512

513-
reftable_buf_addstr(&in.u.idx.last_key, "refs/heads/master");
513+
check(!reftable_buf_addstr(&in.u.idx.last_key, "refs/heads/master"));
514514
reftable_record_key(&in, &key);
515515
t_copy(&in);
516516

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

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,17 @@ static void t_reftable_stack_add_one(void)
172172
check_int(st->readers_len, >, 0);
173173

174174
#ifndef GIT_WINDOWS_NATIVE
175-
reftable_buf_addstr(&scratch, dir);
176-
reftable_buf_addstr(&scratch, "/tables.list");
175+
check(!reftable_buf_addstr(&scratch, dir));
176+
check(!reftable_buf_addstr(&scratch, "/tables.list"));
177177
err = stat(scratch.buf, &stat_result);
178178
check(!err);
179179
check_int((stat_result.st_mode & 0777), ==, opts.default_permissions);
180180

181181
reftable_buf_reset(&scratch);
182-
reftable_buf_addstr(&scratch, dir);
183-
reftable_buf_addstr(&scratch, "/");
182+
check(!reftable_buf_addstr(&scratch, dir));
183+
check(!reftable_buf_addstr(&scratch, "/"));
184184
/* do not try at home; not an external API for reftable. */
185-
reftable_buf_addstr(&scratch, st->readers[0]->name);
185+
check(!reftable_buf_addstr(&scratch, st->readers[0]->name));
186186
err = stat(scratch.buf, &stat_result);
187187
check(!err);
188188
check_int((stat_result.st_mode & 0777), ==, opts.default_permissions);
@@ -432,10 +432,10 @@ static void t_reftable_stack_auto_compaction_fails_gracefully(void)
432432
* Adding a new table to the stack should not be impacted by this, even
433433
* though auto-compaction will now fail.
434434
*/
435-
reftable_buf_addstr(&table_path, dir);
436-
reftable_buf_addstr(&table_path, "/");
437-
reftable_buf_addstr(&table_path, st->readers[0]->name);
438-
reftable_buf_addstr(&table_path, ".lock");
435+
check(!reftable_buf_addstr(&table_path, dir));
436+
check(!reftable_buf_addstr(&table_path, "/"));
437+
check(!reftable_buf_addstr(&table_path, st->readers[0]->name));
438+
check(!reftable_buf_addstr(&table_path, ".lock"));
439439
write_file_buf(table_path.buf, "", 0);
440440

441441
ref.update_index = 2;
@@ -575,17 +575,17 @@ static void t_reftable_stack_add(void)
575575
}
576576

577577
#ifndef GIT_WINDOWS_NATIVE
578-
reftable_buf_addstr(&path, dir);
579-
reftable_buf_addstr(&path, "/tables.list");
578+
check(!reftable_buf_addstr(&path, dir));
579+
check(!reftable_buf_addstr(&path, "/tables.list"));
580580
err = stat(path.buf, &stat_result);
581581
check(!err);
582582
check_int((stat_result.st_mode & 0777), ==, opts.default_permissions);
583583

584584
reftable_buf_reset(&path);
585-
reftable_buf_addstr(&path, dir);
586-
reftable_buf_addstr(&path, "/");
585+
check(!reftable_buf_addstr(&path, dir));
586+
check(!reftable_buf_addstr(&path, "/"));
587587
/* do not try at home; not an external API for reftable. */
588-
reftable_buf_addstr(&path, st->readers[0]->name);
588+
check(!reftable_buf_addstr(&path, st->readers[0]->name));
589589
err = stat(path.buf, &stat_result);
590590
check(!err);
591591
check_int((stat_result.st_mode & 0777), ==, opts.default_permissions);
@@ -1078,10 +1078,10 @@ static void t_reftable_stack_auto_compaction_with_locked_tables(void)
10781078
* size, we expect that auto-compaction will want to compact all of the
10791079
* tables. Locking any of the tables will keep it from doing so.
10801080
*/
1081-
reftable_buf_addstr(&buf, dir);
1082-
reftable_buf_addstr(&buf, "/");
1083-
reftable_buf_addstr(&buf, st->readers[2]->name);
1084-
reftable_buf_addstr(&buf, ".lock");
1081+
check(!reftable_buf_addstr(&buf, dir));
1082+
check(!reftable_buf_addstr(&buf, "/"));
1083+
check(!reftable_buf_addstr(&buf, st->readers[2]->name));
1084+
check(!reftable_buf_addstr(&buf, ".lock"));
10851085
write_file_buf(buf.buf, "", 0);
10861086

10871087
/*
@@ -1164,10 +1164,10 @@ static void t_reftable_stack_compaction_with_locked_tables(void)
11641164
check_int(st->merged->readers_len, ==, 3);
11651165

11661166
/* Lock one of the tables that we're about to compact. */
1167-
reftable_buf_addstr(&buf, dir);
1168-
reftable_buf_addstr(&buf, "/");
1169-
reftable_buf_addstr(&buf, st->readers[1]->name);
1170-
reftable_buf_addstr(&buf, ".lock");
1167+
check(!reftable_buf_addstr(&buf, dir));
1168+
check(!reftable_buf_addstr(&buf, "/"));
1169+
check(!reftable_buf_addstr(&buf, st->readers[1]->name));
1170+
check(!reftable_buf_addstr(&buf, ".lock"));
11711171
write_file_buf(buf.buf, "", 0);
11721172

11731173
/*
@@ -1324,13 +1324,13 @@ static void t_reftable_stack_reload_with_missing_table(void)
13241324
* our old readers. This should trigger a partial reload of the stack,
13251325
* where we try to reuse our old readers.
13261326
*/
1327-
reftable_buf_addstr(&content, st->readers[0]->name);
1328-
reftable_buf_addstr(&content, "\n");
1329-
reftable_buf_addstr(&content, st->readers[1]->name);
1330-
reftable_buf_addstr(&content, "\n");
1331-
reftable_buf_addstr(&content, "garbage\n");
1332-
reftable_buf_addstr(&table_path, st->list_file);
1333-
reftable_buf_addstr(&table_path, ".lock");
1327+
check(!reftable_buf_addstr(&content, st->readers[0]->name));
1328+
check(!reftable_buf_addstr(&content, "\n"));
1329+
check(!reftable_buf_addstr(&content, st->readers[1]->name));
1330+
check(!reftable_buf_addstr(&content, "\n"));
1331+
check(!reftable_buf_addstr(&content, "garbage\n"));
1332+
check(!reftable_buf_addstr(&table_path, st->list_file));
1333+
check(!reftable_buf_addstr(&table_path, ".lock"));
13341334
write_file_buf(table_path.buf, content.buf, content.len);
13351335
err = rename(table_path.buf, st->list_file);
13361336
check(!err);

0 commit comments

Comments
 (0)