Skip to content

Commit 63cf124

Browse files
committed
Merge branch 'jk/tighten-alloc' into maint
Protect our code from over-eager compilers. * jk/tighten-alloc: inline xalloc_flex() into FLEXPTR_ALLOC_MEM avoid pointer arithmetic involving NULL in FLEX_ALLOC_MEM
2 parents 39000e8 + 0ac52a3 commit 63cf124

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

git-compat-util.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -848,26 +848,21 @@ static inline void copy_array(void *dst, const void *src, size_t n, size_t size)
848848
* times, and it must be assignable as an lvalue.
849849
*/
850850
#define FLEX_ALLOC_MEM(x, flexname, buf, len) do { \
851-
(x) = NULL; /* silence -Wuninitialized for offset calculation */ \
852-
(x) = xalloc_flex(sizeof(*(x)), (char *)(&((x)->flexname)) - (char *)(x), (buf), (len)); \
851+
size_t flex_array_len_ = (len); \
852+
(x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
853+
memcpy((void *)(x)->flexname, (buf), flex_array_len_); \
853854
} while (0)
854855
#define FLEXPTR_ALLOC_MEM(x, ptrname, buf, len) do { \
855-
(x) = xalloc_flex(sizeof(*(x)), sizeof(*(x)), (buf), (len)); \
856+
size_t flex_array_len_ = (len); \
857+
(x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
858+
memcpy((x) + 1, (buf), flex_array_len_); \
856859
(x)->ptrname = (void *)((x)+1); \
857860
} while(0)
858861
#define FLEX_ALLOC_STR(x, flexname, str) \
859862
FLEX_ALLOC_MEM((x), flexname, (str), strlen(str))
860863
#define FLEXPTR_ALLOC_STR(x, ptrname, str) \
861864
FLEXPTR_ALLOC_MEM((x), ptrname, (str), strlen(str))
862865

863-
static inline void *xalloc_flex(size_t base_len, size_t offset,
864-
const void *src, size_t src_len)
865-
{
866-
unsigned char *ret = xcalloc(1, st_add3(base_len, src_len, 1));
867-
memcpy(ret + offset, src, src_len);
868-
return ret;
869-
}
870-
871866
static inline char *xstrdup_or_null(const char *str)
872867
{
873868
return str ? xstrdup(str) : NULL;

0 commit comments

Comments
 (0)