From: Marek Polacek Date: Wed, 10 Feb 2021 17:07:10 +0000 (-0500) Subject: c, c++: Plug -Wduplicated-cond memory leaks [PR99057] X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=27a804bc62805aedb1b097a00eb2c0059244680a;p=gcc.git c, c++: Plug -Wduplicated-cond memory leaks [PR99057] 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. --- diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c index a8df208493c..2a49d07bab4 100644 --- a/gcc/c/c-parser.c +++ b/gcc/c/c-parser.c @@ -6499,12 +6499,9 @@ c_parser_if_statement (c_parser *parser, bool *if_p, vec *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 *chain) "suggest explicit braces to avoid ambiguous %"); 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)); diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 5da8670f0e2..d68dcb7d74f 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -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" " %"); 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. */