]> git.draconx.ca Git - cdecl99.git/blobdiff - src/parse-decl.c
libcdecl: Fix memory leak when parsing e.g., int () int.
[cdecl99.git] / src / parse-decl.c
index a31a37756427a7f096a26ab3c5718f90535eaef4..1268fc387810cbd2c7a1ef380b01bc09097e3342 100644 (file)
@@ -500,9 +500,9 @@ static int forall_declarators(struct cdecl *decl,
 
 static struct cdecl *do_parse(const char *str, int english_mode)
 {
+       struct cdecl *decl = NULL;
        YY_BUFFER_STATE state;
        yyscan_t scanner;
-       struct cdecl *decl;
 
 #if YYDEBUG
        extern int cdecl__yydebug;
@@ -514,8 +514,15 @@ static struct cdecl *do_parse(const char *str, int english_mode)
                return NULL;
 
        state = cdecl__yy_scan_string(str, scanner);
-       if (cdecl__yyparse(scanner, &decl) != 0)
+       if (cdecl__yyparse(scanner, &decl) != 0) {
+               /*
+                * If the input consists of a complete, valid declaration
+                * followed by some garbage, that parsed declaration will
+                * be output by the parser and we need to free it here.
+                */
+               cdecl__free(decl);
                decl = NULL;
+       }
        cdecl__yy_delete_buffer(state, scanner);
        cdecl__yylex_destroy(scanner);