From bf2c87bfe6b3826d187dfee2ca894b5a39f6d17e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 27 Jul 2021 13:10:41 -0600 Subject: [PATCH] Add hb_memcpy() that does len=0 check --- src/hb-algs.hh | 8 ++++++++ src/hb-set.hh | 8 ++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index bc170b054..57b87b05c 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -760,6 +760,14 @@ static inline unsigned int ARRAY_LENGTH (const Type (&)[n]) { return n; } #define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0]))) +static inline void * +hb_memcpy (void *__restrict dst, const void *__restrict src, size_t len) +{ + /* It's illegal to pass 0 as size to memcpy. */ + if (unlikely (!len)) return dst; + return memcpy (dst, src, len); +} + static inline int hb_memcmp (const void *a, const void *b, unsigned int len) { diff --git a/src/hb-set.hh b/src/hb-set.hh index a75ca32cf..5688619e8 100644 --- a/src/hb-set.hh +++ b/src/hb-set.hh @@ -489,12 +489,8 @@ struct hb_set_t return; population = other.population; - if (!count) - // memcpy is not necessary if the vectors are zero length. This avoids possibly - // passing nullptr to memcpy. - return; - memcpy ((void *) pages, (const void *) other.pages, count * pages.item_size); - memcpy ((void *) page_map, (const void *) other.page_map, count * page_map.item_size); + hb_memcpy ((void *) pages, (const void *) other.pages, count * pages.item_size); + hb_memcpy ((void *) page_map, (const void *) other.page_map, count * page_map.item_size); } bool is_equal (const hb_set_t &other) const