Skip to content

Commit 9f03e48

Browse files
pks-tgitster
authored andcommitted
object-file: make buf parameter of index_mem() a constant
The `buf` parameter of `index_mem()` is a non-constant string. This will break once we enable `-Wwrite-strings` because we also pass constants from at least one callsite. Adapt the parameter to be a constant. As we cannot free the buffer without casting now, this also requires us to move the lifetime of the nested buffer around. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 724b6d1 commit 9f03e48

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

object-file.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,12 +2484,13 @@ static int hash_format_check_report(struct fsck_options *opts UNUSED,
24842484
}
24852485

24862486
static int index_mem(struct index_state *istate,
2487-
struct object_id *oid, void *buf, size_t size,
2487+
struct object_id *oid,
2488+
const void *buf, size_t size,
24882489
enum object_type type,
24892490
const char *path, unsigned flags)
24902491
{
2492+
struct strbuf nbuf = STRBUF_INIT;
24912493
int ret = 0;
2492-
int re_allocated = 0;
24932494
int write_object = flags & HASH_WRITE_OBJECT;
24942495

24952496
if (!type)
@@ -2499,11 +2500,10 @@ static int index_mem(struct index_state *istate,
24992500
* Convert blobs to git internal format
25002501
*/
25012502
if ((type == OBJ_BLOB) && path) {
2502-
struct strbuf nbuf = STRBUF_INIT;
25032503
if (convert_to_git(istate, path, buf, size, &nbuf,
25042504
get_conv_flags(flags))) {
2505-
buf = strbuf_detach(&nbuf, &size);
2506-
re_allocated = 1;
2505+
buf = nbuf.buf;
2506+
size = nbuf.len;
25072507
}
25082508
}
25092509
if (flags & HASH_FORMAT_CHECK) {
@@ -2520,8 +2520,8 @@ static int index_mem(struct index_state *istate,
25202520
ret = write_object_file(buf, size, type, oid);
25212521
else
25222522
hash_object_file(the_hash_algo, buf, size, type, oid);
2523-
if (re_allocated)
2524-
free(buf);
2523+
2524+
strbuf_release(&nbuf);
25252525
return ret;
25262526
}
25272527

0 commit comments

Comments
 (0)