From fb85d18f59153bad58c5ce951e95affe0e2a5562 Mon Sep 17 00:00:00 2001 From: Christian Hopps Date: Sun, 15 Dec 2024 21:37:41 -0500 Subject: [PATCH] lib: darr: fix bug with nested macro use - WHen declaring macro scoped variables, can run into problem if the macro variable passed in has the same name as the new variable introduced in the inner scope. We don't get a warning and the uses will be wrong. e.g., ``` { int __len = 10; foo(__len); // => 10 and not 15 as we wanted. } ``` Signed-off-by: Christian Hopps --- lib/darr.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/darr.h b/lib/darr.h index 2b9a0a0c025b..121e3dd14e54 100644 --- a/lib/darr.h +++ b/lib/darr.h @@ -272,10 +272,10 @@ void *__darr_resize(void *a, uint count, size_t esize, struct memtype *mt); */ #define darr_ensure_avail_mt(A, S, MT) \ ({ \ - ssize_t need = (ssize_t)(S) - \ - (ssize_t)(darr_cap(A) - darr_len(A)); \ - if (need > 0) \ - _darr_resize_mt((A), darr_cap(A) + need, MT); \ + ssize_t __dea_need = (ssize_t)(S) - \ + (ssize_t)(darr_cap(A) - darr_len(A)); \ + if (__dea_need > 0) \ + _darr_resize_mt((A), darr_cap(A) + __dea_need, MT); \ (A); \ }) #define darr_ensure_avail(A, S) darr_ensure_avail_mt(A, S, MTYPE_DARR) @@ -301,9 +301,9 @@ void *__darr_resize(void *a, uint count, size_t esize, struct memtype *mt); #define darr_ensure_cap_mt(A, C, MT) \ ({ \ /* Cast to avoid warning when C == 0 */ \ - uint _c = (C) > 0 ? (C) : 1; \ - if ((size_t)darr_cap(A) < _c) \ - _darr_resize_mt((A), _c, MT); \ + uint __dec_c = (C) > 0 ? (C) : 1; \ + if ((size_t)darr_cap(A) < __dec_c) \ + _darr_resize_mt((A), __dec_c, MT); \ (A); \ }) #define darr_ensure_cap(A, C) darr_ensure_cap_mt(A, C, MTYPE_DARR) @@ -428,12 +428,12 @@ void *__darr_resize(void *a, uint count, size_t esize, struct memtype *mt); #define _darr_append_n(A, N, Z, MT) \ ({ \ - uint __len = darr_len(A); \ - darr_ensure_cap_mt(A, __len + (N), MT); \ - _darr_len(A) = __len + (N); \ + uint __da_len = darr_len(A); \ + darr_ensure_cap_mt(A, __da_len + (N), MT); \ + _darr_len(A) = __da_len + (N); \ if (Z) \ - memset(&(A)[__len], 0, (N)*_darr_esize(A)); \ - &(A)[__len]; \ + memset(&(A)[__da_len], 0, (N)*_darr_esize(A)); \ + &(A)[__da_len]; \ }) /** * Extending the array's length by N.