libsepol: do not dereference a failed allocated pointer

When strs_stack_init(&stack) fails to allocate memory and stack is still
NULL, it should not be dereferenced with strs_stack_pop(stack).

This issue has been found using Infer static analyzer.

Signed-off-by: Nicolas Iooss <nicolas.iooss@m4x.org>
This commit is contained in:
Nicolas Iooss 2019-09-15 21:10:39 +02:00 committed by James Carter
parent 4459d635b8
commit 4a266cc3ce
2 changed files with 20 additions and 12 deletions

View file

@ -108,10 +108,12 @@ static char *cond_expr_to_str(struct policydb *pdb, struct cond_expr *expr)
return str;
exit:
while ((new_val = strs_stack_pop(stack)) != NULL) {
free(new_val);
if (stack) {
while ((new_val = strs_stack_pop(stack)) != NULL) {
free(new_val);
}
strs_stack_destroy(&stack);
}
strs_stack_destroy(&stack);
return NULL;
}
@ -251,10 +253,12 @@ static char *constraint_expr_to_str(struct policydb *pdb, struct constraint_expr
return str;
exit:
while ((new_val = strs_stack_pop(stack)) != NULL) {
free(new_val);
if (stack) {
while ((new_val = strs_stack_pop(stack)) != NULL) {
free(new_val);
}
strs_stack_destroy(&stack);
}
strs_stack_destroy(&stack);
return NULL;
}

View file

@ -106,10 +106,12 @@ static char *cond_expr_to_str(struct policydb *pdb, struct cond_expr *expr)
return str;
exit:
while ((new_val = strs_stack_pop(stack)) != NULL) {
free(new_val);
if (stack) {
while ((new_val = strs_stack_pop(stack)) != NULL) {
free(new_val);
}
strs_stack_destroy(&stack);
}
strs_stack_destroy(&stack);
return NULL;
}
@ -247,10 +249,12 @@ static char *constraint_expr_to_str(struct policydb *pdb, struct constraint_expr
return str;
exit:
while ((new_val = strs_stack_pop(stack)) != NULL) {
free(new_val);
if (stack) {
while ((new_val = strs_stack_pop(stack)) != NULL) {
free(new_val);
}
strs_stack_destroy(&stack);
}
strs_stack_destroy(&stack);
return NULL;
}