Skip to content

Commit 08e8c26

Browse files
rscharfegitster
authored andcommitted
do full type check in BARF_UNLESS_COPYABLE
Use __builtin_types_compatible_p to perform a full type check if possible. Otherwise fall back to the old size comparison, but add a non-evaluated assignment to catch more type mismatches. It doesn't flag copies between arrays with different signedness, but that's as close to a full type check as it gets without the builtin, as far as I can see. Helped-by: Junio C Hamano <[email protected]> Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1891846 commit 08e8c26

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

git-compat-util.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,14 @@ struct strbuf;
9797
# define BARF_UNLESS_AN_ARRAY(arr) \
9898
BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(__typeof__(arr), \
9999
__typeof__(&(arr)[0])))
100+
# define BARF_UNLESS_COPYABLE(dst, src) \
101+
BUILD_ASSERT_OR_ZERO(__builtin_types_compatible_p(__typeof__(*(dst)), \
102+
__typeof__(*(src))))
100103
#else
101104
# define BARF_UNLESS_AN_ARRAY(arr) 0
105+
# define BARF_UNLESS_COPYABLE(dst, src) \
106+
BUILD_ASSERT_OR_ZERO(0 ? ((*(dst) = *(src)), 0) : \
107+
sizeof(*(dst)) == sizeof(*(src)))
102108
#endif
103109
/*
104110
* ARRAY_SIZE - get the number of elements in a visible array
@@ -1093,9 +1099,6 @@ int xstrncmpz(const char *s, const char *t, size_t len);
10931099
#define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x)))
10941100
#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
10951101

1096-
#define BARF_UNLESS_COPYABLE(dst, src) \
1097-
BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src)))
1098-
10991102
#define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \
11001103
BARF_UNLESS_COPYABLE((dst), (src)))
11011104
static inline void copy_array(void *dst, const void *src, size_t n, size_t size)

0 commit comments

Comments
 (0)