Skip to content

Commit

Permalink
lib: darr: fix bug with nested macro use
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
choppsv1 committed Dec 16, 2024
1 parent 73e54e4 commit 05de014
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions lib/darr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 05de014

Please sign in to comment.