libsepol: do not create a string list with initial size zero

Currently is it implementation defined, due to the size being passed to
calloc(3), whether the operations fails nor not.
Also strs_add() does not handle a size of zero, cause it just multiplies
the size by two.

Use a default size of 1 if 0 is passed and swap the calloc arguments for
consistency.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
This commit is contained in:
Christian Göttsche 2021-12-09 17:49:13 +01:00 committed by James Carter
parent 35ef9b95e7
commit 691e6aff4c

View file

@ -107,6 +107,10 @@ int strs_init(struct strs **strs, size_t size)
{ {
struct strs *new; struct strs *new;
if (size == 0) {
size = 1;
}
*strs = NULL; *strs = NULL;
new = malloc(sizeof(struct strs)); new = malloc(sizeof(struct strs));
@ -115,7 +119,7 @@ int strs_init(struct strs **strs, size_t size)
return -1; return -1;
} }
new->list = calloc(sizeof(char *), size); new->list = calloc(size, sizeof(char *));
if (!new->list) { if (!new->list) {
sepol_log_err("Out of memory"); sepol_log_err("Out of memory");
free(new); free(new);