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>
This commit is contained in:
Patrick Steinhardt 2024-06-07 08:37:52 +02:00 committed by Junio C Hamano
parent 23c32511b3
commit 66f892bb07
4 changed files with 63 additions and 63 deletions

View file

@ -42,7 +42,7 @@ static void test_block_read_write(void)
block_writer_init(&bw, BLOCK_TYPE_REF, block.data, block_size,
header_off, hash_size(GIT_SHA1_FORMAT_ID));
rec.u.ref.refname = "";
rec.u.ref.refname = (char *) "";
rec.u.ref.value_type = REFTABLE_REF_DELETION;
n = block_writer_add(&bw, &rec);
EXPECT(n == REFTABLE_API_ERROR);

View file

@ -124,13 +124,13 @@ static void readers_destroy(struct reftable_reader **readers, size_t n)
static void test_merged_between(void)
{
struct reftable_ref_record r1[] = { {
.refname = "b",
.refname = (char *) "b",
.update_index = 1,
.value_type = REFTABLE_REF_VAL1,
.value.val1 = { 1, 2, 3, 0 },
} };
struct reftable_ref_record r2[] = { {
.refname = "a",
.refname = (char *) "a",
.update_index = 2,
.value_type = REFTABLE_REF_DELETION,
} };
@ -165,38 +165,38 @@ static void test_merged(void)
{
struct reftable_ref_record r1[] = {
{
.refname = "a",
.refname = (char *) "a",
.update_index = 1,
.value_type = REFTABLE_REF_VAL1,
.value.val1 = { 1 },
},
{
.refname = "b",
.refname = (char *) "b",
.update_index = 1,
.value_type = REFTABLE_REF_VAL1,
.value.val1 = { 1 },
},
{
.refname = "c",
.refname = (char *) "c",
.update_index = 1,
.value_type = REFTABLE_REF_VAL1,
.value.val1 = { 1 },
}
};
struct reftable_ref_record r2[] = { {
.refname = "a",
.refname = (char *) "a",
.update_index = 2,
.value_type = REFTABLE_REF_DELETION,
} };
struct reftable_ref_record r3[] = {
{
.refname = "c",
.refname = (char *) "c",
.update_index = 3,
.value_type = REFTABLE_REF_VAL1,
.value.val1 = { 2 },
},
{
.refname = "d",
.refname = (char *) "d",
.update_index = 3,
.value_type = REFTABLE_REF_VAL1,
.value.val1 = { 1 },
@ -291,46 +291,46 @@ static void test_merged_logs(void)
{
struct reftable_log_record r1[] = {
{
.refname = "a",
.refname = (char *) "a",
.update_index = 2,
.value_type = REFTABLE_LOG_UPDATE,
.value.update = {
.old_hash = { 2 },
/* deletion */
.name = "jane doe",
.email = "jane@invalid",
.message = "message2",
.name = (char *) "jane doe",
.email = (char *) "jane@invalid",
.message = (char *) "message2",
}
},
{
.refname = "a",
.refname = (char *) "a",
.update_index = 1,
.value_type = REFTABLE_LOG_UPDATE,
.value.update = {
.old_hash = { 1 },
.new_hash = { 2 },
.name = "jane doe",
.email = "jane@invalid",
.message = "message1",
.name = (char *) "jane doe",
.email = (char *) "jane@invalid",
.message = (char *) "message1",
}
},
};
struct reftable_log_record r2[] = {
{
.refname = "a",
.refname = (char *) "a",
.update_index = 3,
.value_type = REFTABLE_LOG_UPDATE,
.value.update = {
.new_hash = { 3 },
.name = "jane doe",
.email = "jane@invalid",
.message = "message3",
.name = (char *) "jane doe",
.email = (char *) "jane@invalid",
.message = (char *) "message3",
}
},
};
struct reftable_log_record r3[] = {
{
.refname = "a",
.refname = (char *) "a",
.update_index = 2,
.value_type = REFTABLE_LOG_DELETION,
},
@ -406,7 +406,7 @@ static void test_default_write_opts(void)
reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
struct reftable_ref_record rec = {
.refname = "master",
.refname = (char *) "master",
.update_index = 1,
};
int err;

View file

@ -86,7 +86,7 @@ static void write_table(char ***names, struct strbuf *buf, int N,
log.update_index = update_index;
log.value_type = REFTABLE_LOG_UPDATE;
set_test_hash(log.value.update.new_hash, i);
log.value.update.message = "message";
log.value.update.message = (char *) "message";
n = reftable_writer_add_log(w, &log);
EXPECT(n == 0);
@ -118,15 +118,15 @@ static void test_log_buffer_size(void)
int err;
int i;
struct reftable_log_record
log = { .refname = "refs/heads/master",
log = { .refname = (char *) "refs/heads/master",
.update_index = 0xa,
.value_type = REFTABLE_LOG_UPDATE,
.value = { .update = {
.name = "Han-Wen Nienhuys",
.email = "hanwen@google.com",
.name = (char *) "Han-Wen Nienhuys",
.email = (char *) "hanwen@google.com",
.tz_offset = 100,
.time = 0x5e430672,
.message = "commit: 9\n",
.message = (char *) "commit: 9\n",
} } };
struct reftable_writer *w =
reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
@ -156,15 +156,15 @@ static void test_log_overflow(void)
};
int err;
struct reftable_log_record log = {
.refname = "refs/heads/master",
.refname = (char *) "refs/heads/master",
.update_index = 0xa,
.value_type = REFTABLE_LOG_UPDATE,
.value = {
.update = {
.old_hash = { 1 },
.new_hash = { 2 },
.name = "Han-Wen Nienhuys",
.email = "hanwen@google.com",
.name = (char *) "Han-Wen Nienhuys",
.email = (char *) "hanwen@google.com",
.tz_offset = 100,
.time = 0x5e430672,
.message = msg,
@ -293,14 +293,14 @@ static void test_log_zlib_corruption(void)
char message[100] = { 0 };
int err, i, n;
struct reftable_log_record log = {
.refname = "refname",
.refname = (char *) "refname",
.value_type = REFTABLE_LOG_UPDATE,
.value = {
.update = {
.new_hash = { 1 },
.old_hash = { 2 },
.name = "My Name",
.email = "myname@invalid",
.name = (char *) "My Name",
.email = (char *) "myname@invalid",
.message = message,
},
},
@ -728,7 +728,7 @@ static void test_write_empty_key(void)
struct reftable_writer *w =
reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
struct reftable_ref_record ref = {
.refname = "",
.refname = (char *) "",
.update_index = 1,
.value_type = REFTABLE_REF_DELETION,
};
@ -752,18 +752,18 @@ static void test_write_key_order(void)
reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
struct reftable_ref_record refs[2] = {
{
.refname = "b",
.refname = (char *) "b",
.update_index = 1,
.value_type = REFTABLE_REF_SYMREF,
.value = {
.symref = "target",
.symref = (char *) "target",
},
}, {
.refname = "a",
.refname = (char *) "a",
.update_index = 1,
.value_type = REFTABLE_REF_SYMREF,
.value = {
.symref = "target",
.symref = (char *) "target",
},
}
};

View file

@ -156,10 +156,10 @@ static void test_reftable_stack_add_one(void)
struct reftable_stack *st = NULL;
int err;
struct reftable_ref_record ref = {
.refname = "HEAD",
.refname = (char *) "HEAD",
.update_index = 1,
.value_type = REFTABLE_REF_SYMREF,
.value.symref = "master",
.value.symref = (char *) "master",
};
struct reftable_ref_record dest = { NULL };
struct stat stat_result = { 0 };
@ -216,16 +216,16 @@ static void test_reftable_stack_uptodate(void)
int err;
struct reftable_ref_record ref1 = {
.refname = "HEAD",
.refname = (char *) "HEAD",
.update_index = 1,
.value_type = REFTABLE_REF_SYMREF,
.value.symref = "master",
.value.symref = (char *) "master",
};
struct reftable_ref_record ref2 = {
.refname = "branch2",
.refname = (char *) "branch2",
.update_index = 2,
.value_type = REFTABLE_REF_SYMREF,
.value.symref = "master",
.value.symref = (char *) "master",
};
@ -264,10 +264,10 @@ static void test_reftable_stack_transaction_api(void)
struct reftable_addition *add = NULL;
struct reftable_ref_record ref = {
.refname = "HEAD",
.refname = (char *) "HEAD",
.update_index = 1,
.value_type = REFTABLE_REF_SYMREF,
.value.symref = "master",
.value.symref = (char *) "master",
};
struct reftable_ref_record dest = { NULL };
@ -313,7 +313,7 @@ static void test_reftable_stack_transaction_api_performs_auto_compaction(void)
struct reftable_ref_record ref = {
.update_index = reftable_stack_next_update_index(st),
.value_type = REFTABLE_REF_SYMREF,
.value.symref = "master",
.value.symref = (char *) "master",
};
char name[100];
@ -356,7 +356,7 @@ static void test_reftable_stack_transaction_api_performs_auto_compaction(void)
static void test_reftable_stack_auto_compaction_fails_gracefully(void)
{
struct reftable_ref_record ref = {
.refname = "refs/heads/master",
.refname = (char *) "refs/heads/master",
.update_index = 1,
.value_type = REFTABLE_REF_VAL1,
.value.val1 = {0x01},
@ -409,16 +409,16 @@ static void test_reftable_stack_update_index_check(void)
struct reftable_stack *st = NULL;
int err;
struct reftable_ref_record ref1 = {
.refname = "name1",
.refname = (char *) "name1",
.update_index = 1,
.value_type = REFTABLE_REF_SYMREF,
.value.symref = "master",
.value.symref = (char *) "master",
};
struct reftable_ref_record ref2 = {
.refname = "name2",
.refname = (char *) "name2",
.update_index = 1,
.value_type = REFTABLE_REF_SYMREF,
.value.symref = "master",
.value.symref = (char *) "master",
};
err = reftable_new_stack(&st, dir, cfg);
@ -561,7 +561,7 @@ static void test_reftable_stack_log_normalize(void)
struct reftable_stack *st = NULL;
char *dir = get_tmp_dir(__LINE__);
struct reftable_log_record input = {
.refname = "branch",
.refname = (char *) "branch",
.update_index = 1,
.value_type = REFTABLE_LOG_UPDATE,
.value = {
@ -582,11 +582,11 @@ static void test_reftable_stack_log_normalize(void)
err = reftable_new_stack(&st, dir, cfg);
EXPECT_ERR(err);
input.value.update.message = "one\ntwo";
input.value.update.message = (char *) "one\ntwo";
err = reftable_stack_add(st, &write_test_log, &arg);
EXPECT(err == REFTABLE_API_ERROR);
input.value.update.message = "one";
input.value.update.message = (char *) "one";
err = reftable_stack_add(st, &write_test_log, &arg);
EXPECT_ERR(err);
@ -594,7 +594,7 @@ static void test_reftable_stack_log_normalize(void)
EXPECT_ERR(err);
EXPECT(0 == strcmp(dest.value.update.message, "one\n"));
input.value.update.message = "two\n";
input.value.update.message = (char *) "two\n";
arg.update_index = 2;
err = reftable_stack_add(st, &write_test_log, &arg);
EXPECT_ERR(err);
@ -697,9 +697,9 @@ static void test_reftable_stack_hash_id(void)
int err;
struct reftable_ref_record ref = {
.refname = "master",
.refname = (char *) "master",
.value_type = REFTABLE_REF_SYMREF,
.value.symref = "target",
.value.symref = (char *) "target",
.update_index = 1,
};
struct reftable_write_options cfg32 = { .hash_id = GIT_SHA256_FORMAT_ID };
@ -879,7 +879,7 @@ static void test_reftable_stack_auto_compaction(void)
.refname = name,
.update_index = reftable_stack_next_update_index(st),
.value_type = REFTABLE_REF_SYMREF,
.value.symref = "master",
.value.symref = (char *) "master",
};
snprintf(name, sizeof(name), "branch%04d", i);
@ -913,7 +913,7 @@ static void test_reftable_stack_add_performs_auto_compaction(void)
struct reftable_ref_record ref = {
.update_index = reftable_stack_next_update_index(st),
.value_type = REFTABLE_REF_SYMREF,
.value.symref = "master",
.value.symref = (char *) "master",
};
/*
@ -964,7 +964,7 @@ static void test_reftable_stack_compaction_concurrent(void)
.refname = name,
.update_index = reftable_stack_next_update_index(st1),
.value_type = REFTABLE_REF_SYMREF,
.value.symref = "master",
.value.symref = (char *) "master",
};
snprintf(name, sizeof(name), "branch%04d", i);
@ -1014,7 +1014,7 @@ static void test_reftable_stack_compaction_concurrent_clean(void)
.refname = name,
.update_index = reftable_stack_next_update_index(st1),
.value_type = REFTABLE_REF_SYMREF,
.value.symref = "master",
.value.symref = (char *) "master",
};
snprintf(name, sizeof(name), "branch%04d", i);