[PATCH:iceauth] Free old argv array if realloc fails to enlarge it

Alan Coopersmith alan.coopersmith at oracle.com
Sun Dec 8 12:55:57 PST 2013


Found by cppcheck:
[app/iceauth/process.c:302]: (error) Common realloc mistake:
 'argv' nulled but not freed upon failure

Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 process.c |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/process.c b/process.c
index 09cb7ef..6f7396d 100644
--- a/process.c
+++ b/process.c
@@ -298,9 +298,13 @@ static char **split_into_words (  /* argvify string */
 	savec = *src;
 	*src = '\0';
 	if (cur == total) {
+	    char **prevargv = argv;
 	    total += WORDSTOALLOC;
 	    argv = (char **) realloc (argv, total * sizeof (char *));
-	    if (!argv) return NULL;
+	    if (!argv) {
+		free (prevargv);
+		return NULL;
+	    }
 	}
 	argv[cur++] = jword;
 	if (savec) src++;		/* if not last on line advance */
-- 
1.7.9.2



More information about the xorg-devel mailing list