diff options
author | Mike Frysinger <vapier@gentoo.org> | 2014-03-20 07:59:27 +0000 |
---|---|---|
committer | Mike Frysinger <vapier@gentoo.org> | 2014-03-20 07:59:27 +0000 |
commit | e29ec52af9c2a2d5f4a7a152e8fbec0c9e08922d (patch) | |
tree | b817448d3f37693cf41991e9c118791301a249a8 /xfuncs.h | |
parent | fix possible memory read errors when walking arrays (diff) | |
download | pax-utils-e29ec52af9c2a2d5f4a7a152e8fbec0c9e08922d.tar.gz pax-utils-e29ec52af9c2a2d5f4a7a152e8fbec0c9e08922d.tar.bz2 pax-utils-e29ec52af9c2a2d5f4a7a152e8fbec0c9e08922d.zip |
move array_cnt check into array_for_each init
atm, if you try to use array_for_each or array_flatten_str on an array that has no members, you will get a segfault. this is an easy rule to forget (and the current code does just that in at least one place), so move the array_cnt check into the init phase. theres negligible code size impact so it should not be a big deal.
Diffstat (limited to 'xfuncs.h')
-rw-r--r-- | xfuncs.h | 6 |
1 files changed, 4 insertions, 2 deletions
@@ -1,7 +1,7 @@ /* * Copyright 2003-2012 Gentoo Foundation * Distributed under the terms of the GNU General Public License v2 - * $Header: /var/cvsroot/gentoo-projects/pax-utils/xfuncs.h,v 1.7 2012/11/04 07:26:24 vapier Exp $ + * $Header: /var/cvsroot/gentoo-projects/pax-utils/xfuncs.h,v 1.8 2014/03/20 07:59:27 vapier Exp $ * * Copyright 2003-2012 Ned Ludd - <solar@gentoo.org> * Copyright 2004-2012 Mike Frysinger - <vapier@gentoo.org> @@ -29,7 +29,9 @@ void xarraypush(array_t *array, const void *ele, size_t ele_len); void xarrayfree(array_t *array); #define xrealloc_array(ptr, size, ele_size) xrealloc(ptr, (size) * (ele_size)) #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]) #define array_init_decl { .eles = NULL, .num = 0, } #define array_cnt(arr) (arr)->num char *array_flatten_str(array_t *array); |