Skip to content

Commit c112084

Browse files
rscharfegitster
authored andcommitted
fast-export: avoid NULL pointer arithmetic
Clang 6 reports the following warning, which is turned into an error in a DEVELOPER build: builtin/fast-export.c:162:28: error: performing pointer arithmetic on a null pointer has undefined behavior [-Werror,-Wnull-pointer-arithmetic] return ((uint32_t *)NULL) + mark; ~~~~~~~~~~~~~~~~~~ ^ 1 error generated. The compiler is correct, and the error message speaks for itself. There is no need for any undefined operation -- just cast mark to void * or uint32_t after an intermediate cast to uintptr_t. That encodes the integer value into a pointer and later decodes it as intended. While at it remove an outdated comment -- intptr_t has been used since ffe659f (parse-options: make some arguments optional, add callbacks), committed in October 2007. Signed-off-by: Rene Scharfe <[email protected]> Acked-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 27dea46 commit c112084

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

builtin/fast-export.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,14 @@ static void anonymize_path(struct strbuf *out, const char *path,
154154
}
155155
}
156156

157-
/* Since intptr_t is C99, we do not use it here */
158-
static inline uint32_t *mark_to_ptr(uint32_t mark)
157+
static inline void *mark_to_ptr(uint32_t mark)
159158
{
160-
return ((uint32_t *)NULL) + mark;
159+
return (void *)(uintptr_t)mark;
161160
}
162161

163162
static inline uint32_t ptr_to_mark(void * mark)
164163
{
165-
return (uint32_t *)mark - (uint32_t *)NULL;
164+
return (uint32_t)(uintptr_t)mark;
166165
}
167166

168167
static inline void mark_object(struct object *object, uint32_t mark)

0 commit comments

Comments
 (0)