]> git.ipfire.org Git - thirdparty/git.git/commitdiff
object-file: mark cached object buffers as const
authorPatrick Steinhardt <ps@pks.im>
Fri, 7 Jun 2024 06:38:30 +0000 (08:38 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 7 Jun 2024 17:30:51 +0000 (10:30 -0700)
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>
object-file.c

index 610b1f465c4248e8c0520687049a707a8497195e..08c00dcc0275b9ad81b85564466e5a659a4c8e77 100644 (file)
@@ -277,7 +277,7 @@ int hash_algo_by_length(int len)
 static struct cached_object {
        struct object_id oid;
        enum object_type type;
-       void *buf;
+       const void *buf;
        unsigned long size;
 } *cached_objects;
 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,
                        struct object_id *oid)
 {
        struct cached_object *co;
+       char *co_buf;
 
        hash_object_file(the_hash_algo, buf, len, type, oid);
        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,
        co = &cached_objects[cached_object_nr++];
        co->size = len;
        co->type = type;
-       co->buf = xmalloc(len);
-       memcpy(co->buf, buf, len);
+       co_buf = xmalloc(len);
+       memcpy(co_buf, buf, len);
+       co->buf = co_buf;
        oidcpy(&co->oid, oid);
        return 0;
 }