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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jun 7, 2024 at 08:38 UTC 9f03e4813a5b4e469b3a7f5ad4ada3a9c3f92bfd
1 file changed +7 -7
object-file.c
+7 -7
@@ -2484,12 +2484,13 @@ static int hash_format_check_report(struct fsck_options *opts UNUSED,
2484 }
2485
2486 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,
2489 enum object_type type,
2490 const char *path, unsigned flags)
2491 {
2492 + struct strbuf nbuf = STRBUF_INIT;
2493 int ret = 0;
2492 - int re_allocated = 0;
2494 int write_object = flags & HASH_WRITE_OBJECT;
2495
2496 if (!type)
@@ -2499,11 +2500,10 @@ static int index_mem(struct index_state *istate,
2500 * Convert blobs to git internal format
2501 */
2502 if ((type == OBJ_BLOB) && path) {
2502 - struct strbuf nbuf = STRBUF_INIT;
2503 if (convert_to_git(istate, path, buf, size, &nbuf,
2504 get_conv_flags(flags))) {
2505 - buf = strbuf_detach(&nbuf, &size);
2506 - re_allocated = 1;
2505 + buf = nbuf.buf;
2506 + size = nbuf.len;
2507 }
2508 }
2509 if (flags & HASH_FORMAT_CHECK) {
@@ -2520,8 +2520,8 @@ static int index_mem(struct index_state *istate,
2520 ret = write_object_file(buf, size, type, oid);
2521 else
2522 hash_object_file(the_hash_algo, buf, size, type, oid);
2523 - if (re_allocated)
2524 - free(buf);
2523 +
2524 + strbuf_release(&nbuf);
2525 return ret;
2526 }
2527