If we exited this function early due to an error, h->len would contain
the number of elements that *ought* to be in h->val, but not all of
those elements must have been initialized. Subsequently trying to free
this partially-uninitialized structure with free_Keys() could have bad
results.
Avoid this by ensuring that h->len accurately reports the actual number
of initialized elements.
Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
ZERO_STRUCTP(h);
- h->len = s->len;
if (s->val != NULL) {
- h->val = malloc(h->len * sizeof(Key));
+ h->val = malloc(s->len * sizeof(Key));
if (h->val == NULL) {
return ENOMEM;
}
- for (i = 0; i < h->len; i++) {
+ for (i = 0; i < s->len; i++) {
ret = sdb_key_to_Key(&s->val[i],
&h->val[i]);
if (ret != 0) {
free_Keys(h);
return ENOMEM;
}
+
+ ++h->len;
}
} else {
h->val = NULL;