examples/element_declarations.c: Fix memleak in dumpContentModel on OOM

clang-tidy output was:
> [..]/examples/element_declarations.c:163:16: warning: Potential leak of memory pointed to by 'stackTop' [clang-analyzer-unix.Malloc]
>   163 |         return false;
>       |                ^
This commit is contained in:
Sebastian Pipping 2024-01-12 00:34:54 +01:00
parent 716fd10bd4
commit 0ebca2b10f

View file

@ -157,11 +157,17 @@ dumpContentModel(const XML_Char *name, const XML_Content *root) {
stackTop = stackPopFree(stackTop);
for (size_t u = model->numchildren; u >= 1; u--) {
stackTop
Stack *const newStackTop
= stackPushMalloc(stackTop, model->children + (u - 1), level + 1);
if (! stackTop) {
if (! newStackTop) {
// We ran out of memory, so let's free all memory allocated
// earlier in this function, to be leak-clean:
while (stackTop != NULL) {
stackTop = stackPopFree(stackTop);
}
return false;
}
stackTop = newStackTop;
}
}