Skip to content

Commit 4ef44ce

Browse files
committed
Merge branch 'jk/tighten-alloc' into pu
* jk/tighten-alloc: inline xalloc_flex() into FLEXPTR_ALLOC_MEM avoid pointer arithmetic involving NULL in FLEX_ALLOC_MEM
2 parents 3040b07 + 0ac52a3 commit 4ef44ce

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
@@ -851,26 +851,21 @@ static inline void copy_array(void *dst, const void *src, size_t n, size_t size)
851851
* times, and it must be assignable as an lvalue.
852852
*/
853853
#define FLEX_ALLOC_MEM(x, flexname, buf, len) do { \
854-
(x) = NULL; /* silence -Wuninitialized for offset calculation */ \
855-
(x) = xalloc_flex(sizeof(*(x)), (char *)(&((x)->flexname)) - (char *)(x), (buf), (len)); \
854+
size_t flex_array_len_ = (len); \
855+
(x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
856+
memcpy((void *)(x)->flexname, (buf), flex_array_len_); \
856857
} while (0)
857858
#define FLEXPTR_ALLOC_MEM(x, ptrname, buf, len) do { \
858-
(x) = xalloc_flex(sizeof(*(x)), sizeof(*(x)), (buf), (len)); \
859+
size_t flex_array_len_ = (len); \
860+
(x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
861+
memcpy((x) + 1, (buf), flex_array_len_); \
859862
(x)->ptrname = (void *)((x)+1); \
860863
} while(0)
861864
#define FLEX_ALLOC_STR(x, flexname, str) \
862865
FLEX_ALLOC_MEM((x), flexname, (str), strlen(str))
863866
#define FLEXPTR_ALLOC_STR(x, ptrname, str) \
864867
FLEXPTR_ALLOC_MEM((x), ptrname, (str), strlen(str))
865868

866-
static inline void *xalloc_flex(size_t base_len, size_t offset,
867-
const void *src, size_t src_len)
868-
{
869-
unsigned char *ret = xcalloc(1, st_add3(base_len, src_len, 1));
870-
memcpy(ret + offset, src, src_len);
871-
return ret;
872-
}
873-
874869
static inline char *xstrdup_or_null(const char *str)
875870
{
876871
return str ? xstrdup(str) : NULL;

0 commit comments

Comments
 (0)