bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] cond expr: cleanup on errors


From: Grisha Levit
Subject: [PATCH] cond expr: cleanup on errors
Date: Fri, 31 May 2024 17:07:21 -0400

Two minor leak fixes for conditional command error conditions:

If a WORD token is read when COND_AND, COND_OR, COND_END, or a binary
operator are expected, the allocated WORD_DESC is leaked.

If a conditional command has a syntax error, the allocated COMMAND is
leaked.
---
 parse.y | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/parse.y b/parse.y
index 1349480b..d39d6737 100644
--- a/parse.y
+++ b/parse.y
@@ -3525,6 +3525,7 @@ read_token (int command)
       if (cond_token != COND_END)
        {
          cond_error ();
+         dispose_command (yylval.command);
          return (-1);
        }
       token_to_read = COND_END;
@@ -5003,6 +5004,9 @@ cond_skip_newlines (void)
 #define COND_RETURN_ERROR() \
   do { cond_token = COND_ERROR; return ((COND_COM *)NULL); } while (0)
 
+#define COND_TERM_DONE() \
+  do { if (cond_skip_newlines () == WORD) dispose_word (yylval.word); } while 
(0)
+
 static COND_COM *
 cond_term (void)
 {
@@ -5037,12 +5041,12 @@ cond_term (void)
          COND_RETURN_ERROR ();
        }
       term = make_cond_node (COND_EXPR, (WORD_DESC *)NULL, term, (COND_COM 
*)NULL);
-      (void)cond_skip_newlines ();
+      COND_TERM_DONE ();
     }
   else if (tok == BANG || (tok == WORD && (yylval.word->word[0] == '!' && 
yylval.word->word[1] == '\0')))
     {
       if (tok == WORD)
-       dispose_word (yylval.word);     /* not needed */
+       dispose_word (yylval.word);
       term = cond_term ();
       if (term)
        term->flags ^= CMD_INVERT_RETURN;
@@ -5069,7 +5073,7 @@ cond_term (void)
          COND_RETURN_ERROR ();
        }
 
-      (void)cond_skip_newlines ();
+      COND_TERM_DONE ();
     }
   else if (tok == WORD)                /* left argument to binary operator */
     {
@@ -5118,6 +5122,8 @@ cond_term (void)
          else
            parser_error (line_number, _("conditional binary operator 
expected"));
          dispose_cond_node (tleft);
+         if (tok == WORD)
+           dispose_word (yylval.word);
          COND_RETURN_ERROR ();
        }
 
@@ -5153,7 +5159,7 @@ cond_term (void)
          COND_RETURN_ERROR ();
        }
 
-      (void)cond_skip_newlines ();
+      COND_TERM_DONE ();
     }
   else
     {
-- 
2.45.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]