object-file: mark cached object buffers as const

The buffers of cached objects are never modified, but are still stored as a non-constant pointer. This will cause a compiler warning once we enable the `-Wwrite-strings` compiler warning as we assign an empty constant string when initializing the static `empty_tree` cached object. Convert the field to be constant. This requires us to shuffle around the code a bit because we memcpy(3P) into the allocated buffer in `pretend_object_file()`. This is easily fixed though by allocating the buffer into a temporary variable first. 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 724b6d1e18606ca2fa14d7cc48c3bf3884363e25
1 file changed +5 -3
object-file.c
+5 -3
@@ -277,7 +277,7 @@ int hash_algo_by_length(int len)
277 static struct cached_object {
278 struct object_id oid;
279 enum object_type type;
280 - void *buf;
280 + const void *buf;
281 unsigned long size;
282 } *cached_objects;
283 static int cached_object_nr, cached_object_alloc;
@@ -1778,6 +1778,7 @@ int pretend_object_file(void *buf, unsigned long len, enum object_type type,
1778 struct object_id *oid)
1779 {
1780 struct cached_object *co;
1781 + char *co_buf;
1782
1783 hash_object_file(the_hash_algo, buf, len, type, oid);
1784 if (repo_has_object_file_with_flags(the_repository, oid, OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT) ||
@@ -1787,8 +1788,9 @@ int pretend_object_file(void *buf, unsigned long len, enum object_type type,
1788 co = &cached_objects[cached_object_nr++];
1789 co->size = len;
1790 co->type = type;
1790 - co->buf = xmalloc(len);
1791 - memcpy(co->buf, buf, len);
1791 + co_buf = xmalloc(len);
1792 + memcpy(co_buf, buf, len);
1793 + co->buf = co_buf;
1794 oidcpy(&co->oid, oid);
1795 return 0;
1796 }