c, c++: Plug -Wduplicated-cond memory leaks [PR99057]
authorMarek Polacek <polacek@redhat.com>
Wed, 10 Feb 2021 17:07:10 +0000 (12:07 -0500)
committerMarek Polacek <polacek@redhat.com>
Thu, 11 Feb 2021 03:17:43 +0000 (22:17 -0500)
Freeing the condition chain needs to use vec_free which does ->release,
or we leak memory.

gcc/c/ChangeLog:

* c-parser.c (c_parser_if_statement): Use vec_free.

gcc/cp/ChangeLog:

* parser.c (cp_parser_selection_statement): Use vec_free.

gcc/c/c-parser.c
gcc/cp/parser.c

index a8df208493cad7511be0c879305aca12ce2a857e..2a49d07bab4c0b5ece796c1bd515a2f5c7cad775 100644 (file)
@@ -6499,12 +6499,9 @@ c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
                chain->safe_push (cond);
            }
          else if (!c_parser_next_token_is_keyword (parser, RID_IF))
-           {
-             /* This is if-else without subsequent if.  Zap the condition
-                chain; we would have already warned at this point.  */
-             delete chain;
-             chain = NULL;
-           }
+           /* This is if-else without subsequent if.  Zap the condition
+              chain; we would have already warned at this point.  */
+           vec_free (chain);
        }
       second_body = c_parser_else_body (parser, else_tinfo, chain);
       /* Set IF_P to true to indicate that this if statement has an
@@ -6524,12 +6521,9 @@ c_parser_if_statement (c_parser *parser, bool *if_p, vec<tree> *chain)
                    "suggest explicit braces to avoid ambiguous %<else%>");
 
       if (warn_duplicated_cond)
-       {
-         /* This if statement does not have an else clause.  We don't
-            need the condition chain anymore.  */
-         delete chain;
-         chain = NULL;
-       }
+       /* This if statement does not have an else clause.  We don't
+          need the condition chain anymore.  */
+       vec_free (chain);
     }
   c_finish_if_stmt (loc, cond, first_body, second_body);
   add_stmt (c_end_compound_stmt (loc, block, flag_isoc99));
index 5da8670f0e22fc5a4efd417373ec95efe5b660ea..d68dcb7d74fc949368c9bf31cedb35012e815985 100644 (file)
@@ -12247,12 +12247,9 @@ cp_parser_selection_statement (cp_parser* parser, bool *if_p,
                       "init-statement in selection statements only available "
                       "with %<-std=c++17%> or %<-std=gnu++17%>");
            if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
-             {
-               /* A non-empty init-statement can have arbitrary side
-                  effects.  */
-               delete chain;
-               chain = NULL;
-             }
+             /* A non-empty init-statement can have arbitrary side
+                effects.  */
+             vec_free (chain);
            cp_parser_init_statement (parser, &decl);
          }
 
@@ -12343,13 +12340,10 @@ cp_parser_selection_statement (cp_parser* parser, bool *if_p,
                      }
                    else if (!cp_lexer_next_token_is_keyword (parser->lexer,
                                                              RID_IF))
-                     {
-                       /* This is if-else without subsequent if.  Zap the
-                          condition chain; we would have already warned at
-                          this point.  */
-                       delete chain;
-                       chain = NULL;
-                     }
+                     /* This is if-else without subsequent if.  Zap the
+                        condition chain; we would have already warned at
+                        this point.  */
+                     vec_free (chain);
                  }
                begin_else_clause (statement);
                /* Parse the else-clause.  */
@@ -12384,11 +12378,8 @@ cp_parser_selection_statement (cp_parser* parser, bool *if_p,
                              "suggest explicit braces to avoid ambiguous"
                              " %<else%>");
                if (warn_duplicated_cond)
-                 {
-                   /* We don't need the condition chain anymore.  */
-                   delete chain;
-                   chain = NULL;
-                 }
+                 /* We don't need the condition chain anymore.  */
+                 vec_free (chain);
              }
 
            /* Now we're all done with the if-statement.  */