Avoid a false positive since Coverity find a path in virResizeN which
could return 0 prior to the allocation of memory and thus flags a
possible NULL dereference. Instead use multiple loops to first count
the number of matches, perform one allocation, and then again search
for matches. It's a 'n' string comparisons and allocations versus
2*'n' string comparisons and one allocation.
Signed-off-by: John Ferlan <jferlan(a)redhat.com>
---
src/util/virtypedparam.c | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c
index 106403c..79c7cab 100644
--- a/src/util/virtypedparam.c
+++ b/src/util/virtypedparam.c
@@ -501,7 +501,7 @@ virTypedParamsFilter(virTypedParameterPtr params,
const char *name,
virTypedParameterPtr **ret)
{
- size_t i, alloc = 0, n = 0;
+ size_t i, n = 0;
virCheckNonNullArgGoto(params, error);
virCheckNonNullArgGoto(name, error);
@@ -510,12 +510,16 @@ virTypedParamsFilter(virTypedParameterPtr params,
*ret = NULL;
for (i = 0; i < nparams; i++) {
- if (STREQ(params[i].field, name)) {
- if (VIR_RESIZE_N(*ret, alloc, n, 1) < 0)
- goto error;
+ if (STREQ(params[i].field, name))
+ n++;
+ }
- (*ret)[n] = ¶ms[i];
+ if (VIR_ALLOC_N(*ret, n) < 0)
+ goto error;
+ for (n = 0, i = 0; i < nparams; i++) {
+ if (STREQ(params[i].field, name)) {
+ (*ret)[n] = ¶ms[i];
n++;
}
}
--
2.1.0