commit: 66090491b033778785f12ccd3f20cdf54e89c87a Author: Mike Frysinger <vapier <AT> gentoo <DOT> org> AuthorDate: Thu Nov 26 08:35:45 2015 +0000 Commit: Mike Frysinger <vapier <AT> gentoo <DOT> org> CommitDate: Thu Nov 26 08:35:45 2015 +0000 URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=66090491
xarray: sync logic with latest pax-utils This pulls in two fixes: - handling of empty arrays - invalid loads at end of arrays URL: https://bugs.gentoo.org/553368 Reported-by: Hanno Boeck <hanno <AT> gentoo.org> libq/xarray.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/libq/xarray.c b/libq/xarray.c index b4c3857..56f04da 100644 --- a/libq/xarray.c +++ b/libq/xarray.c @@ -12,8 +12,14 @@ typedef struct { } array_t; #define xrealloc_array(ptr, size, ele_size) xrealloc(ptr, (size) * (ele_size)) +/* The assignment after the check is unfortunate as we do a non-NULL check (we + * already do not permit pushing of NULL pointers), but we can't put it in the + * increment phase as that will cause a load beyond the bounds of valid memory. + */ #define array_for_each(arr, n, ele) \ - for (n = 0, ele = arr->eles[n]; n < arr->num; ++n, ele = arr->eles[n]) + for (n = 0, ele = array_cnt(arr) ? arr->eles[n] : NULL; \ + n < array_cnt(arr) && (ele = arr->eles[n]); \ + ++n) #define array_init_decl { .eles = NULL, .num = 0, } #define array_cnt(arr) (arr)->num #define DECLARE_ARRAY(arr) array_t _##arr = array_init_decl, *arr = &_##arr