reftable: cast away constness when assigning constants to records

The reftable records are used in multiple ways throughout the reftable library. In many of those cases they merely act as input to a function without getting modified by it at all. Most importantly, this happens when writing records and when querying for records. We rely on this in our tests and thus assign string constants to those fields, which is about to generate warnings as those fields are of type `char *`. While we could go through the process and instead allocate those strings in all of our tests, this feels quite unnecessary. Instead, add casts to `char *` for all of those strings. As this is part of our tests, this also nicely serves as a demonstration that nothing writes or frees those string constants, which would otherwise lead to segfaults. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jun 7, 2024 at 08:37 UTC 66f892bb075f19bed784b86c7850a89c9a865aca
4 files changed +63 -63
reftable/block_test.c
+1 -1
@@ -42,7 +42,7 @@ static void test_block_read_write(void)
42 block_writer_init(&bw, BLOCK_TYPE_REF, block.data, block_size,
43 header_off, hash_size(GIT_SHA1_FORMAT_ID));
44
45 - rec.u.ref.refname = "";
45 + rec.u.ref.refname = (char *) "";
46 rec.u.ref.value_type = REFTABLE_REF_DELETION;
47 n = block_writer_add(&bw, &rec);
48 EXPECT(n == REFTABLE_API_ERROR);
reftable/merged_test.c
+22 -22
@@ -124,13 +124,13 @@ static void readers_destroy(struct reftable_reader **readers, size_t n)
124 static void test_merged_between(void)
125 {
126 struct reftable_ref_record r1[] = { {
127 - .refname = "b",
127 + .refname = (char *) "b",
128 .update_index = 1,
129 .value_type = REFTABLE_REF_VAL1,
130 .value.val1 = { 1, 2, 3, 0 },
131 } };
132 struct reftable_ref_record r2[] = { {
133 - .refname = "a",
133 + .refname = (char *) "a",
134 .update_index = 2,
135 .value_type = REFTABLE_REF_DELETION,
136 } };
@@ -165,38 +165,38 @@ static void test_merged(void)
165 {
166 struct reftable_ref_record r1[] = {
167 {
168 - .refname = "a",
168 + .refname = (char *) "a",
169 .update_index = 1,
170 .value_type = REFTABLE_REF_VAL1,
171 .value.val1 = { 1 },
172 },
173 {
174 - .refname = "b",
174 + .refname = (char *) "b",
175 .update_index = 1,
176 .value_type = REFTABLE_REF_VAL1,
177 .value.val1 = { 1 },
178 },
179 {
180 - .refname = "c",
180 + .refname = (char *) "c",
181 .update_index = 1,
182 .value_type = REFTABLE_REF_VAL1,
183 .value.val1 = { 1 },
184 }
185 };
186 struct reftable_ref_record r2[] = { {
187 - .refname = "a",
187 + .refname = (char *) "a",
188 .update_index = 2,
189 .value_type = REFTABLE_REF_DELETION,
190 } };
191 struct reftable_ref_record r3[] = {
192 {
193 - .refname = "c",
193 + .refname = (char *) "c",
194 .update_index = 3,
195 .value_type = REFTABLE_REF_VAL1,
196 .value.val1 = { 2 },
197 },
198 {
199 - .refname = "d",
199 + .refname = (char *) "d",
200 .update_index = 3,
201 .value_type = REFTABLE_REF_VAL1,
202 .value.val1 = { 1 },
@@ -291,46 +291,46 @@ static void test_merged_logs(void)
291 {
292 struct reftable_log_record r1[] = {
293 {
294 - .refname = "a",
294 + .refname = (char *) "a",
295 .update_index = 2,
296 .value_type = REFTABLE_LOG_UPDATE,
297 .value.update = {
298 .old_hash = { 2 },
299 /* deletion */
300 - .name = "jane doe",
301 - .email = "jane@invalid",
302 - .message = "message2",
300 + .name = (char *) "jane doe",
301 + .email = (char *) "jane@invalid",
302 + .message = (char *) "message2",
303 }
304 },
305 {
306 - .refname = "a",
306 + .refname = (char *) "a",
307 .update_index = 1,
308 .value_type = REFTABLE_LOG_UPDATE,
309 .value.update = {
310 .old_hash = { 1 },
311 .new_hash = { 2 },
312 - .name = "jane doe",
313 - .email = "jane@invalid",
314 - .message = "message1",
312 + .name = (char *) "jane doe",
313 + .email = (char *) "jane@invalid",
314 + .message = (char *) "message1",
315 }
316 },
317 };
318 struct reftable_log_record r2[] = {
319 {
320 - .refname = "a",
320 + .refname = (char *) "a",
321 .update_index = 3,
322 .value_type = REFTABLE_LOG_UPDATE,
323 .value.update = {
324 .new_hash = { 3 },
325 - .name = "jane doe",
326 - .email = "jane@invalid",
327 - .message = "message3",
325 + .name = (char *) "jane doe",
326 + .email = (char *) "jane@invalid",
327 + .message = (char *) "message3",
328 }
329 },
330 };
331 struct reftable_log_record r3[] = {
332 {
333 - .refname = "a",
333 + .refname = (char *) "a",
334 .update_index = 2,
335 .value_type = REFTABLE_LOG_DELETION,
336 },
@@ -406,7 +406,7 @@ static void test_default_write_opts(void)
406 reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
407
408 struct reftable_ref_record rec = {
409 - .refname = "master",
409 + .refname = (char *) "master",
410 .update_index = 1,
411 };
412 int err;
reftable/readwrite_test.c
+16 -16
@@ -86,7 +86,7 @@ static void write_table(char ***names, struct strbuf *buf, int N,
86 log.update_index = update_index;
87 log.value_type = REFTABLE_LOG_UPDATE;
88 set_test_hash(log.value.update.new_hash, i);
89 - log.value.update.message = "message";
89 + log.value.update.message = (char *) "message";
90
91 n = reftable_writer_add_log(w, &log);
92 EXPECT(n == 0);
@@ -118,15 +118,15 @@ static void test_log_buffer_size(void)
118 int err;
119 int i;
120 struct reftable_log_record
121 - log = { .refname = "refs/heads/master",
121 + log = { .refname = (char *) "refs/heads/master",
122 .update_index = 0xa,
123 .value_type = REFTABLE_LOG_UPDATE,
124 .value = { .update = {
125 - .name = "Han-Wen Nienhuys",
126 - .email = "hanwen@google.com",
125 + .name = (char *) "Han-Wen Nienhuys",
126 + .email = (char *) "hanwen@google.com",
127 .tz_offset = 100,
128 .time = 0x5e430672,
129 - .message = "commit: 9\n",
129 + .message = (char *) "commit: 9\n",
130 } } };
131 struct reftable_writer *w =
132 reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
@@ -156,15 +156,15 @@ static void test_log_overflow(void)
156 };
157 int err;
158 struct reftable_log_record log = {
159 - .refname = "refs/heads/master",
159 + .refname = (char *) "refs/heads/master",
160 .update_index = 0xa,
161 .value_type = REFTABLE_LOG_UPDATE,
162 .value = {
163 .update = {
164 .old_hash = { 1 },
165 .new_hash = { 2 },
166 - .name = "Han-Wen Nienhuys",
167 - .email = "hanwen@google.com",
166 + .name = (char *) "Han-Wen Nienhuys",
167 + .email = (char *) "hanwen@google.com",
168 .tz_offset = 100,
169 .time = 0x5e430672,
170 .message = msg,
@@ -293,14 +293,14 @@ static void test_log_zlib_corruption(void)
293 char message[100] = { 0 };
294 int err, i, n;
295 struct reftable_log_record log = {
296 - .refname = "refname",
296 + .refname = (char *) "refname",
297 .value_type = REFTABLE_LOG_UPDATE,
298 .value = {
299 .update = {
300 .new_hash = { 1 },
301 .old_hash = { 2 },
302 - .name = "My Name",
303 - .email = "myname@invalid",
302 + .name = (char *) "My Name",
303 + .email = (char *) "myname@invalid",
304 .message = message,
305 },
306 },
@@ -728,7 +728,7 @@ static void test_write_empty_key(void)
728 struct reftable_writer *w =
729 reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
730 struct reftable_ref_record ref = {
731 - .refname = "",
731 + .refname = (char *) "",
732 .update_index = 1,
733 .value_type = REFTABLE_REF_DELETION,
734 };
@@ -752,18 +752,18 @@ static void test_write_key_order(void)
752 reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
753 struct reftable_ref_record refs[2] = {
754 {
755 - .refname = "b",
755 + .refname = (char *) "b",
756 .update_index = 1,
757 .value_type = REFTABLE_REF_SYMREF,
758 .value = {
759 - .symref = "target",
759 + .symref = (char *) "target",
760 },
761 }, {
762 - .refname = "a",
762 + .refname = (char *) "a",
763 .update_index = 1,
764 .value_type = REFTABLE_REF_SYMREF,
765 .value = {
766 - .symref = "target",
766 + .symref = (char *) "target",
767 },
768 }
769 };
reftable/stack_test.c
+24 -24
@@ -156,10 +156,10 @@ static void test_reftable_stack_add_one(void)
156 struct reftable_stack *st = NULL;
157 int err;
158 struct reftable_ref_record ref = {
159 - .refname = "HEAD",
159 + .refname = (char *) "HEAD",
160 .update_index = 1,
161 .value_type = REFTABLE_REF_SYMREF,
162 - .value.symref = "master",
162 + .value.symref = (char *) "master",
163 };
164 struct reftable_ref_record dest = { NULL };
165 struct stat stat_result = { 0 };
@@ -216,16 +216,16 @@ static void test_reftable_stack_uptodate(void)
216
217 int err;
218 struct reftable_ref_record ref1 = {
219 - .refname = "HEAD",
219 + .refname = (char *) "HEAD",
220 .update_index = 1,
221 .value_type = REFTABLE_REF_SYMREF,
222 - .value.symref = "master",
222 + .value.symref = (char *) "master",
223 };
224 struct reftable_ref_record ref2 = {
225 - .refname = "branch2",
225 + .refname = (char *) "branch2",
226 .update_index = 2,
227 .value_type = REFTABLE_REF_SYMREF,
228 - .value.symref = "master",
228 + .value.symref = (char *) "master",
229 };
230
231
@@ -264,10 +264,10 @@ static void test_reftable_stack_transaction_api(void)
264 struct reftable_addition *add = NULL;
265
266 struct reftable_ref_record ref = {
267 - .refname = "HEAD",
267 + .refname = (char *) "HEAD",
268 .update_index = 1,
269 .value_type = REFTABLE_REF_SYMREF,
270 - .value.symref = "master",
270 + .value.symref = (char *) "master",
271 };
272 struct reftable_ref_record dest = { NULL };
273
@@ -313,7 +313,7 @@ static void test_reftable_stack_transaction_api_performs_auto_compaction(void)
313 struct reftable_ref_record ref = {
314 .update_index = reftable_stack_next_update_index(st),
315 .value_type = REFTABLE_REF_SYMREF,
316 - .value.symref = "master",
316 + .value.symref = (char *) "master",
317 };
318 char name[100];
319
@@ -356,7 +356,7 @@ static void test_reftable_stack_transaction_api_performs_auto_compaction(void)
356 static void test_reftable_stack_auto_compaction_fails_gracefully(void)
357 {
358 struct reftable_ref_record ref = {
359 - .refname = "refs/heads/master",
359 + .refname = (char *) "refs/heads/master",
360 .update_index = 1,
361 .value_type = REFTABLE_REF_VAL1,
362 .value.val1 = {0x01},
@@ -409,16 +409,16 @@ static void test_reftable_stack_update_index_check(void)
409 struct reftable_stack *st = NULL;
410 int err;
411 struct reftable_ref_record ref1 = {
412 - .refname = "name1",
412 + .refname = (char *) "name1",
413 .update_index = 1,
414 .value_type = REFTABLE_REF_SYMREF,
415 - .value.symref = "master",
415 + .value.symref = (char *) "master",
416 };
417 struct reftable_ref_record ref2 = {
418 - .refname = "name2",
418 + .refname = (char *) "name2",
419 .update_index = 1,
420 .value_type = REFTABLE_REF_SYMREF,
421 - .value.symref = "master",
421 + .value.symref = (char *) "master",
422 };
423
424 err = reftable_new_stack(&st, dir, cfg);
@@ -561,7 +561,7 @@ static void test_reftable_stack_log_normalize(void)
561 struct reftable_stack *st = NULL;
562 char *dir = get_tmp_dir(__LINE__);
563 struct reftable_log_record input = {
564 - .refname = "branch",
564 + .refname = (char *) "branch",
565 .update_index = 1,
566 .value_type = REFTABLE_LOG_UPDATE,
567 .value = {
@@ -582,11 +582,11 @@ static void test_reftable_stack_log_normalize(void)
582 err = reftable_new_stack(&st, dir, cfg);
583 EXPECT_ERR(err);
584
585 - input.value.update.message = "one\ntwo";
585 + input.value.update.message = (char *) "one\ntwo";
586 err = reftable_stack_add(st, &write_test_log, &arg);
587 EXPECT(err == REFTABLE_API_ERROR);
588
589 - input.value.update.message = "one";
589 + input.value.update.message = (char *) "one";
590 err = reftable_stack_add(st, &write_test_log, &arg);
591 EXPECT_ERR(err);
592
@@ -594,7 +594,7 @@ static void test_reftable_stack_log_normalize(void)
594 EXPECT_ERR(err);
595 EXPECT(0 == strcmp(dest.value.update.message, "one\n"));
596
597 - input.value.update.message = "two\n";
597 + input.value.update.message = (char *) "two\n";
598 arg.update_index = 2;
599 err = reftable_stack_add(st, &write_test_log, &arg);
600 EXPECT_ERR(err);
@@ -697,9 +697,9 @@ static void test_reftable_stack_hash_id(void)
697 int err;
698
699 struct reftable_ref_record ref = {
700 - .refname = "master",
700 + .refname = (char *) "master",
701 .value_type = REFTABLE_REF_SYMREF,
702 - .value.symref = "target",
702 + .value.symref = (char *) "target",
703 .update_index = 1,
704 };
705 struct reftable_write_options cfg32 = { .hash_id = GIT_SHA256_FORMAT_ID };
@@ -879,7 +879,7 @@ static void test_reftable_stack_auto_compaction(void)
879 .refname = name,
880 .update_index = reftable_stack_next_update_index(st),
881 .value_type = REFTABLE_REF_SYMREF,
882 - .value.symref = "master",
882 + .value.symref = (char *) "master",
883 };
884 snprintf(name, sizeof(name), "branch%04d", i);
885
@@ -913,7 +913,7 @@ static void test_reftable_stack_add_performs_auto_compaction(void)
913 struct reftable_ref_record ref = {
914 .update_index = reftable_stack_next_update_index(st),
915 .value_type = REFTABLE_REF_SYMREF,
916 - .value.symref = "master",
916 + .value.symref = (char *) "master",
917 };
918
919 /*
@@ -964,7 +964,7 @@ static void test_reftable_stack_compaction_concurrent(void)
964 .refname = name,
965 .update_index = reftable_stack_next_update_index(st1),
966 .value_type = REFTABLE_REF_SYMREF,
967 - .value.symref = "master",
967 + .value.symref = (char *) "master",
968 };
969 snprintf(name, sizeof(name), "branch%04d", i);
970
@@ -1014,7 +1014,7 @@ static void test_reftable_stack_compaction_concurrent_clean(void)
1014 .refname = name,
1015 .update_index = reftable_stack_next_update_index(st1),
1016 .value_type = REFTABLE_REF_SYMREF,
1017 - .value.symref = "master",
1017 + .value.symref = (char *) "master",
1018 };
1019 snprintf(name, sizeof(name), "branch%04d", i);
1020