Skip to content

Commit 7099153

Browse files
rscharfegitster
authored andcommitted
tag: avoid NULL pointer arithmetic
lookup_blob() etc. can return NULL if the referenced object isn't of the expected type. In theory it's wrong to reference the object member in that case. In practice it's OK because it's located at offset 0 for all types, so the pointer arithmetic (NULL + 0) is optimized out by the compiler. The issue is reported by Clang's AddressSanitizer, though. Avoid the ASan error by casting the results of the lookup functions to struct object pointers. That works fine with NULL pointers as well. We already rely on the object member being first in all object types in other places in the code. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4010f1d commit 7099153

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

tag.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
142142
bufptr = nl + 1;
143143

144144
if (!strcmp(type, blob_type)) {
145-
item->tagged = &lookup_blob(&oid)->object;
145+
item->tagged = (struct object *)lookup_blob(&oid);
146146
} else if (!strcmp(type, tree_type)) {
147-
item->tagged = &lookup_tree(&oid)->object;
147+
item->tagged = (struct object *)lookup_tree(&oid);
148148
} else if (!strcmp(type, commit_type)) {
149-
item->tagged = &lookup_commit(&oid)->object;
149+
item->tagged = (struct object *)lookup_commit(&oid);
150150
} else if (!strcmp(type, tag_type)) {
151-
item->tagged = &lookup_tag(&oid)->object;
151+
item->tagged = (struct object *)lookup_tag(&oid);
152152
} else {
153153
error("Unknown type %s", type);
154154
item->tagged = NULL;

0 commit comments

Comments
 (0)