From 0ebca2b10fa814cc11f8145261e77f3beb0c0bf8 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Fri, 12 Jan 2024 00:34:54 +0100 Subject: [PATCH] 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; > | ^ --- expat/examples/element_declarations.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/expat/examples/element_declarations.c b/expat/examples/element_declarations.c index 437b0996..c6c9105e 100644 --- a/expat/examples/element_declarations.c +++ b/expat/examples/element_declarations.c @@ -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; } }