diff options
author | duna.oh <duna.oh@samsung.com> | 2024-09-25 20:27:11 +0900 |
---|---|---|
committer | duna.oh <duna.oh@samsung.com> | 2024-09-27 11:29:20 +0900 |
commit | 00203d5022219895e2885c86ef773cbf58feb495 (patch) | |
tree | e52785ae2a5f8674ac469e45ca1d77fdf62ece95 /src | |
parent | 63d9d57f04c1b16e370fa28b328fc33489819e07 (diff) | |
download | libxkbcommon-tizen.tar.gz libxkbcommon-tizen.tar.bz2 libxkbcommon-tizen.zip |
darray: check integer overflowHEADtizen_9.0_m2_releaseaccepted/tizen/unified/x/asan/20241014.000209accepted/tizen/unified/x/20240929.082835accepted/tizen/unified/toolchain/20241004.101829accepted/tizen/unified/20240927.171126accepted/tizen/9.0/unified/20241030.231557tizen_9.0tizenaccepted/tizen_unified_x_asanaccepted/tizen_unified_xaccepted/tizen_unified_toolchainaccepted/tizen_unifiedaccepted/tizen_9.0_unified
In commit b9b3593cbdeb7f5b02d50cecaba6a0b47d4979ad,
darray is changed to use unsigned int instead of size_t
to reduce memory usage.
However, an integer overflow may occur due to arithmetic operation
(addition) between values.
To address this issue, this commit adds checks if this sums don't
exceed their type's maximum value.
Change-Id: Iaa689bb3beff0f991c8b9b6d8fc541f971d8c735
Diffstat (limited to 'src')
-rw-r--r-- | src/darray.h | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/src/darray.h b/src/darray.h index b75d85f..1fcce2b 100644 --- a/src/darray.h +++ b/src/darray.h @@ -97,8 +97,11 @@ typedef darray (unsigned long) darray_ulong; #define darray_append_items(arr, items, count) do { \ unsigned __count = (count), __oldSize = (arr).size; \ - darray_resize(arr, __oldSize + __count); \ - memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \ + size_t __total_size = __oldSize + __count; \ + if (__total_size <= UINT32_MAX) { \ + darray_resize(arr, __oldSize + __count); \ + memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \ + } \ } while (0) #define darray_from_items(arr, items, count) do { \ @@ -134,18 +137,24 @@ typedef darray (unsigned long) darray_ulong; #define darray_appends_nullterminate(arr, items, count) do { \ unsigned __count = (count), __oldSize = (arr).size; \ - darray_resize(arr, __oldSize + __count + 1); \ - memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \ - (arr).item[--(arr).size] = 0; \ + size_t __total_size = __oldSize + __count + 1; \ + if (__total_size <= UINT32_MAX) { \ + darray_resize(arr, __oldSize + __count + 1); \ + memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \ + (arr).item[--(arr).size] = 0; \ + } \ } while (0) #define darray_prepends_nullterminate(arr, items, count) do { \ unsigned __count = (count), __oldSize = (arr).size; \ - darray_resize(arr, __count + __oldSize + 1); \ - memmove((arr).item + __count, (arr).item, \ - __oldSize * sizeof(*(arr).item)); \ - memcpy((arr).item, items, __count * sizeof(*(arr).item)); \ - (arr).item[--(arr).size] = 0; \ + size_t __total_size = __count + __oldSize + 1; \ + if (__total_size <= UINT32_MAX) { \ + darray_resize(arr, __count + __oldSize + 1); \ + memmove((arr).item + __count, (arr).item, \ + __oldSize * sizeof(*(arr).item)); \ + memcpy((arr).item, items, __count * sizeof(*(arr).item)); \ + (arr).item[--(arr).size] = 0; \ + } \ } while (0) /*** Size management ***/ |