Skip to content

Commit 66f892b

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 23c3251 commit 66f892b

File tree

4 files changed

+63
-63
lines changed

4 files changed

+63
-63
lines changed

reftable/block_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static void test_block_read_write(void)
4242
block_writer_init(&bw, BLOCK_TYPE_REF, block.data, block_size,
4343
header_off, hash_size(GIT_SHA1_FORMAT_ID));
4444

45-
rec.u.ref.refname = "";
45+
rec.u.ref.refname = (char *) "";
4646
rec.u.ref.value_type = REFTABLE_REF_DELETION;
4747
n = block_writer_add(&bw, &rec);
4848
EXPECT(n == REFTABLE_API_ERROR);

reftable/merged_test.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ static void readers_destroy(struct reftable_reader **readers, size_t n)
124124
static void test_merged_between(void)
125125
{
126126
struct reftable_ref_record r1[] = { {
127-
.refname = "b",
127+
.refname = (char *) "b",
128128
.update_index = 1,
129129
.value_type = REFTABLE_REF_VAL1,
130130
.value.val1 = { 1, 2, 3, 0 },
131131
} };
132132
struct reftable_ref_record r2[] = { {
133-
.refname = "a",
133+
.refname = (char *) "a",
134134
.update_index = 2,
135135
.value_type = REFTABLE_REF_DELETION,
136136
} };
@@ -165,38 +165,38 @@ static void test_merged(void)
165165
{
166166
struct reftable_ref_record r1[] = {
167167
{
168-
.refname = "a",
168+
.refname = (char *) "a",
169169
.update_index = 1,
170170
.value_type = REFTABLE_REF_VAL1,
171171
.value.val1 = { 1 },
172172
},
173173
{
174-
.refname = "b",
174+
.refname = (char *) "b",
175175
.update_index = 1,
176176
.value_type = REFTABLE_REF_VAL1,
177177
.value.val1 = { 1 },
178178
},
179179
{
180-
.refname = "c",
180+
.refname = (char *) "c",
181181
.update_index = 1,
182182
.value_type = REFTABLE_REF_VAL1,
183183
.value.val1 = { 1 },
184184
}
185185
};
186186
struct reftable_ref_record r2[] = { {
187-
.refname = "a",
187+
.refname = (char *) "a",
188188
.update_index = 2,
189189
.value_type = REFTABLE_REF_DELETION,
190190
} };
191191
struct reftable_ref_record r3[] = {
192192
{
193-
.refname = "c",
193+
.refname = (char *) "c",
194194
.update_index = 3,
195195
.value_type = REFTABLE_REF_VAL1,
196196
.value.val1 = { 2 },
197197
},
198198
{
199-
.refname = "d",
199+
.refname = (char *) "d",
200200
.update_index = 3,
201201
.value_type = REFTABLE_REF_VAL1,
202202
.value.val1 = { 1 },
@@ -291,46 +291,46 @@ static void test_merged_logs(void)
291291
{
292292
struct reftable_log_record r1[] = {
293293
{
294-
.refname = "a",
294+
.refname = (char *) "a",
295295
.update_index = 2,
296296
.value_type = REFTABLE_LOG_UPDATE,
297297
.value.update = {
298298
.old_hash = { 2 },
299299
/* 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",
303303
}
304304
},
305305
{
306-
.refname = "a",
306+
.refname = (char *) "a",
307307
.update_index = 1,
308308
.value_type = REFTABLE_LOG_UPDATE,
309309
.value.update = {
310310
.old_hash = { 1 },
311311
.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",
315315
}
316316
},
317317
};
318318
struct reftable_log_record r2[] = {
319319
{
320-
.refname = "a",
320+
.refname = (char *) "a",
321321
.update_index = 3,
322322
.value_type = REFTABLE_LOG_UPDATE,
323323
.value.update = {
324324
.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",
328328
}
329329
},
330330
};
331331
struct reftable_log_record r3[] = {
332332
{
333-
.refname = "a",
333+
.refname = (char *) "a",
334334
.update_index = 2,
335335
.value_type = REFTABLE_LOG_DELETION,
336336
},
@@ -406,7 +406,7 @@ static void test_default_write_opts(void)
406406
reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
407407

408408
struct reftable_ref_record rec = {
409-
.refname = "master",
409+
.refname = (char *) "master",
410410
.update_index = 1,
411411
};
412412
int err;

reftable/readwrite_test.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static void write_table(char ***names, struct strbuf *buf, int N,
8686
log.update_index = update_index;
8787
log.value_type = REFTABLE_LOG_UPDATE;
8888
set_test_hash(log.value.update.new_hash, i);
89-
log.value.update.message = "message";
89+
log.value.update.message = (char *) "message";
9090

9191
n = reftable_writer_add_log(w, &log);
9292
EXPECT(n == 0);
@@ -118,15 +118,15 @@ static void test_log_buffer_size(void)
118118
int err;
119119
int i;
120120
struct reftable_log_record
121-
log = { .refname = "refs/heads/master",
121+
log = { .refname = (char *) "refs/heads/master",
122122
.update_index = 0xa,
123123
.value_type = REFTABLE_LOG_UPDATE,
124124
.value = { .update = {
125-
.name = "Han-Wen Nienhuys",
126-
.email = "[email protected]",
125+
.name = (char *) "Han-Wen Nienhuys",
126+
.email = (char *) "[email protected]",
127127
.tz_offset = 100,
128128
.time = 0x5e430672,
129-
.message = "commit: 9\n",
129+
.message = (char *) "commit: 9\n",
130130
} } };
131131
struct reftable_writer *w =
132132
reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
@@ -156,15 +156,15 @@ static void test_log_overflow(void)
156156
};
157157
int err;
158158
struct reftable_log_record log = {
159-
.refname = "refs/heads/master",
159+
.refname = (char *) "refs/heads/master",
160160
.update_index = 0xa,
161161
.value_type = REFTABLE_LOG_UPDATE,
162162
.value = {
163163
.update = {
164164
.old_hash = { 1 },
165165
.new_hash = { 2 },
166-
.name = "Han-Wen Nienhuys",
167-
.email = "[email protected]",
166+
.name = (char *) "Han-Wen Nienhuys",
167+
.email = (char *) "[email protected]",
168168
.tz_offset = 100,
169169
.time = 0x5e430672,
170170
.message = msg,
@@ -293,14 +293,14 @@ static void test_log_zlib_corruption(void)
293293
char message[100] = { 0 };
294294
int err, i, n;
295295
struct reftable_log_record log = {
296-
.refname = "refname",
296+
.refname = (char *) "refname",
297297
.value_type = REFTABLE_LOG_UPDATE,
298298
.value = {
299299
.update = {
300300
.new_hash = { 1 },
301301
.old_hash = { 2 },
302-
.name = "My Name",
303-
.email = "myname@invalid",
302+
.name = (char *) "My Name",
303+
.email = (char *) "myname@invalid",
304304
.message = message,
305305
},
306306
},
@@ -728,7 +728,7 @@ static void test_write_empty_key(void)
728728
struct reftable_writer *w =
729729
reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
730730
struct reftable_ref_record ref = {
731-
.refname = "",
731+
.refname = (char *) "",
732732
.update_index = 1,
733733
.value_type = REFTABLE_REF_DELETION,
734734
};
@@ -752,18 +752,18 @@ static void test_write_key_order(void)
752752
reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
753753
struct reftable_ref_record refs[2] = {
754754
{
755-
.refname = "b",
755+
.refname = (char *) "b",
756756
.update_index = 1,
757757
.value_type = REFTABLE_REF_SYMREF,
758758
.value = {
759-
.symref = "target",
759+
.symref = (char *) "target",
760760
},
761761
}, {
762-
.refname = "a",
762+
.refname = (char *) "a",
763763
.update_index = 1,
764764
.value_type = REFTABLE_REF_SYMREF,
765765
.value = {
766-
.symref = "target",
766+
.symref = (char *) "target",
767767
},
768768
}
769769
};

0 commit comments

Comments
 (0)