Skip to content

Commit 724b6d1

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 32f9929 commit 724b6d1

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

object-file.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ int hash_algo_by_length(int len)
277277
static struct cached_object {
278278
struct object_id oid;
279279
enum object_type type;
280-
void *buf;
280+
const void *buf;
281281
unsigned long size;
282282
} *cached_objects;
283283
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,
17781778
struct object_id *oid)
17791779
{
17801780
struct cached_object *co;
1781+
char *co_buf;
17811782

17821783
hash_object_file(the_hash_algo, buf, len, type, oid);
17831784
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,
17871788
co = &cached_objects[cached_object_nr++];
17881789
co->size = len;
17891790
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;
17921794
oidcpy(&co->oid, oid);
17931795
return 0;
17941796
}

0 commit comments

Comments
 (0)