t/unit-tests: check for `reftable_buf` allocation errors
Adapt our unit tests to check for allocations errors. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Taylor Blau <me@ttaylorr.com>
Patrick Steinhardt committed
Oct 17, 2024 at 06:54 UTC
31eedd1d115c086a84aaa5b53b14294d0afda4ae
5 files changed
+44
-44
t/unit-tests/t-reftable-basics.c
+2
-2
@@ -113,8 +113,8 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
113
};
114
115
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));
118
check_int(common_prefix_size(&a, &b), ==, cases[i].want);
119
reftable_buf_reset(&a);
120
reftable_buf_reset(&b);
t/unit-tests/t-reftable-block.c
+2
-2
@@ -167,7 +167,7 @@ static void t_log_block_read_write(void)
167
for (i = 0; i < N; i++) {
168
block_iter_reset(&it);
169
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));
171
172
ret = block_iter_seek_key(&it, &br, &want);
173
check_int(ret, ==, 0);
@@ -314,7 +314,7 @@ static void t_index_block_read_write(void)
314
315
reftable_buf_init(&recs[i].u.idx.last_key);
316
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));
318
recs[i].u.idx.offset = i;
319
320
ret = block_writer_add(&bw, &recs[i]);
t/unit-tests/t-reftable-readwrite.c
+4
-4
@@ -23,7 +23,7 @@ static void t_buffer(void)
23
struct reftable_block out = { 0 };
24
int n;
25
uint8_t in[] = "hello";
26
- reftable_buf_add(&buf, in, sizeof(in));
26
+ check(!reftable_buf_add(&buf, in, sizeof(in)));
27
block_source_from_buf(&source, &buf);
28
check_int(block_source_size(&source), ==, 6);
29
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)
443
reftable_iterator_destroy(&it);
444
}
445
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, "/"));
448
449
err = reftable_reader_init_ref_iterator(reader, &it);
450
check(!err);
@@ -901,7 +901,7 @@ static void t_corrupt_table(void)
901
struct reftable_block_source source = { 0 };
902
struct reftable_reader *reader;
903
int err;
904
- reftable_buf_add(&buf, zeros, sizeof(zeros));
904
+ check(!reftable_buf_add(&buf, zeros, sizeof(zeros)));
905
906
block_source_from_buf(&source, &buf);
907
err = reftable_reader_new(&reader, &source, "file.log");
t/unit-tests/t-reftable-record.c
+7
-7
@@ -335,14 +335,14 @@ static void t_key_roundtrip(void)
335
int n, m;
336
uint8_t rt_extra;
337
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"));
340
extra = 6;
341
n = reftable_encode_key(&restart, dest, last_key, key, extra);
342
check(!restart);
343
check_int(n, >, 0);
344
345
- reftable_buf_addstr(&roundtrip, "refs/heads/master");
345
+ check(!reftable_buf_addstr(&roundtrip, "refs/heads/master"));
346
m = reftable_decode_key(&roundtrip, &rt_extra, dest);
347
check_int(n, ==, m);
348
check(!reftable_buf_cmp(&key, &roundtrip));
@@ -469,9 +469,9 @@ static void t_reftable_index_record_comparison(void)
469
.u.idx.last_key = REFTABLE_BUF_INIT,
470
},
471
};
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"));
475
476
check(!reftable_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
477
check(!reftable_record_cmp(&in[0], &in[1]));
@@ -510,7 +510,7 @@ static void t_reftable_index_record_roundtrip(void)
510
int n, m;
511
uint8_t extra;
512
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"));
514
reftable_record_key(&in, &key);
515
t_copy(&in);
516
t/unit-tests/t-reftable-stack.c
+29
-29
@@ -172,17 +172,17 @@ static void t_reftable_stack_add_one(void)
172
check_int(st->readers_len, >, 0);
173
174
#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"));
177
err = stat(scratch.buf, &stat_result);
178
check(!err);
179
check_int((stat_result.st_mode & 0777), ==, opts.default_permissions);
180
181
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, "/"));
184
/* 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));
186
err = stat(scratch.buf, &stat_result);
187
check(!err);
188
check_int((stat_result.st_mode & 0777), ==, opts.default_permissions);
@@ -432,10 +432,10 @@ static void t_reftable_stack_auto_compaction_fails_gracefully(void)
432
* Adding a new table to the stack should not be impacted by this, even
433
* though auto-compaction will now fail.
434
*/
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"));
439
write_file_buf(table_path.buf, "", 0);
440
441
ref.update_index = 2;
@@ -575,17 +575,17 @@ static void t_reftable_stack_add(void)
575
}
576
577
#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"));
580
err = stat(path.buf, &stat_result);
581
check(!err);
582
check_int((stat_result.st_mode & 0777), ==, opts.default_permissions);
583
584
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, "/"));
587
/* 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));
589
err = stat(path.buf, &stat_result);
590
check(!err);
591
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)
1078
* size, we expect that auto-compaction will want to compact all of the
1079
* tables. Locking any of the tables will keep it from doing so.
1080
*/
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"));
1085
write_file_buf(buf.buf, "", 0);
1086
1087
/*
@@ -1164,10 +1164,10 @@ static void t_reftable_stack_compaction_with_locked_tables(void)
1164
check_int(st->merged->readers_len, ==, 3);
1165
1166
/* 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"));
1171
write_file_buf(buf.buf, "", 0);
1172
1173
/*
@@ -1324,13 +1324,13 @@ static void t_reftable_stack_reload_with_missing_table(void)
1324
* our old readers. This should trigger a partial reload of the stack,
1325
* where we try to reuse our old readers.
1326
*/
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"));
1334
write_file_buf(table_path.buf, content.buf, content.len);
1335
err = rename(table_path.buf, st->list_file);
1336
check(!err);