From ce5720447c69286599b96bae53ae854b1bbe41fa Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Thu, 4 Feb 2021 14:50:23 -0700 Subject: PR c/97882 - Segmentation Fault on improper redeclaration of function gcc/c/ChangeLog: PR c/97882 * c-decl.c (locate_old_decl): Add type to diagnostic output. (diagnose_mismatched_decls): Same. (start_function): Introduce temporaries for better readability. * c-typeck.c (comptypes_internal): Only consider complete enum types in comparisons with integers. gcc/testsuite/ChangeLog: PR c/97882 * gcc.dg/decl-8.c: Adjust text of expected diagnostic. * gcc.dg/label-decl-4.c: Same. * gcc.dg/mismatch-decl-1.c: Same. * gcc.dg/old-style-then-proto-1.c: Same. * gcc.dg/parm-mismatch-1.c: Same. * gcc.dg/pr35445.c: Same. * gcc.dg/redecl-11.c: Same. * gcc.dg/redecl-12.c: Same. * gcc.dg/redecl-13.c: Same. * gcc.dg/redecl-15.c: Same. * gcc.dg/tls/thr-init-1.c: Same. * objc.dg/id-1.m: Same. * objc.dg/tls/diag-3.m: Same. * gcc.dg/pr97882.c: New test. * gcc.dg/qual-return-7.c: New test. * gcc.dg/qual-return-8.c: New test. --- gcc/c/c-decl.c | 38 ++++++++++++++++++++++++-------------- gcc/c/c-typeck.c | 8 ++++++-- 2 files changed, 30 insertions(+), 16 deletions(-) (limited to 'gcc/c') diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index be95643..a5852a3 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -1910,15 +1910,22 @@ validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype) static void locate_old_decl (tree decl) { - if (TREE_CODE (decl) == FUNCTION_DECL && fndecl_built_in_p (decl) + if (TREE_CODE (decl) == FUNCTION_DECL + && fndecl_built_in_p (decl) && !C_DECL_DECLARED_BUILTIN (decl)) ; else if (DECL_INITIAL (decl)) - inform (input_location, "previous definition of %q+D was here", decl); + inform (input_location, + "previous definition of %q+D with type %qT", + decl, TREE_TYPE (decl)); else if (C_DECL_IMPLICIT (decl)) - inform (input_location, "previous implicit declaration of %q+D was here", decl); + inform (input_location, + "previous implicit declaration of %q+D with type %qT", + decl, TREE_TYPE (decl)); else - inform (input_location, "previous declaration of %q+D was here", decl); + inform (input_location, + "previous declaration of %q+D with type %qT", + decl, TREE_TYPE (decl)); } /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL. @@ -2083,7 +2090,8 @@ diagnose_mismatched_decls (tree newdecl, tree olddecl, && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl)) { pedwarned = pedwarn (input_location, 0, - "conflicting types for %q+D", newdecl); + "conflicting types for %q+D; have %qT", + newdecl, newtype); /* Make sure we keep void as the return type. */ TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype; } @@ -2119,7 +2127,7 @@ diagnose_mismatched_decls (tree newdecl, tree olddecl, error ("conflicting type qualifiers for %q+D", newdecl); } else - error ("conflicting types for %q+D", newdecl); + error ("conflicting types for %q+D; have %qT", newdecl, newtype); diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype); locate_old_decl (olddecl); return false; @@ -9457,27 +9465,29 @@ start_function (struct c_declspecs *declspecs, struct c_declarator *declarator, current_function_prototype_locus = UNKNOWN_LOCATION; current_function_prototype_built_in = false; current_function_prototype_arg_types = NULL_TREE; - if (!prototype_p (TREE_TYPE (decl1))) + tree newtype = TREE_TYPE (decl1); + tree oldtype = old_decl ? TREE_TYPE (old_decl) : newtype; + if (!prototype_p (newtype)) { + tree oldrt = TREE_TYPE (oldtype); + tree newrt = TREE_TYPE (newtype); if (old_decl != NULL_TREE - && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE - && comptypes (TREE_TYPE (TREE_TYPE (decl1)), - TREE_TYPE (TREE_TYPE (old_decl)))) + && TREE_CODE (oldtype) == FUNCTION_TYPE + && comptypes (oldrt, newrt)) { - if (stdarg_p (TREE_TYPE (old_decl))) + if (stdarg_p (oldtype)) { auto_diagnostic_group d; warning_at (loc, 0, "%q+D defined as variadic function " "without prototype", decl1); locate_old_decl (old_decl); } - TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl), - TREE_TYPE (decl1)); + TREE_TYPE (decl1) = composite_type (oldtype, newtype); current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl); current_function_prototype_built_in = C_DECL_BUILTIN_PROTOTYPE (old_decl); current_function_prototype_arg_types - = TYPE_ARG_TYPES (TREE_TYPE (decl1)); + = TYPE_ARG_TYPES (newtype); } if (TREE_PUBLIC (decl1)) { diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 8ef0843..725dc51 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -1107,7 +1107,9 @@ comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p, not transitive: two enumerated types in the same translation unit are compatible with each other only if they are the same type. */ - if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE) + if (TREE_CODE (t1) == ENUMERAL_TYPE + && COMPLETE_TYPE_P (t1) + && TREE_CODE (t2) != ENUMERAL_TYPE) { t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1)); if (TREE_CODE (t2) != VOID_TYPE) @@ -1118,7 +1120,9 @@ comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p, *different_types_p = true; } } - else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE) + else if (TREE_CODE (t2) == ENUMERAL_TYPE + && COMPLETE_TYPE_P (t2) + && TREE_CODE (t1) != ENUMERAL_TYPE) { t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2)); if (TREE_CODE (t1) != VOID_TYPE) -- cgit v1.1 From a19dd5e644ae8725f17bf52c54354b61f771d2c4 Mon Sep 17 00:00:00 2001 From: GCC Administrator Date: Fri, 5 Feb 2021 00:16:23 +0000 Subject: Daily bump. --- gcc/c/ChangeLog | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'gcc/c') diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index 9afa103..b306491 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,12 @@ +2021-02-04 Martin Sebor + + PR c/97882 + * c-decl.c (locate_old_decl): Add type to diagnostic output. + (diagnose_mismatched_decls): Same. + (start_function): Introduce temporaries for better readability. + * c-typeck.c (comptypes_internal): Only consider complete enum + types in comparisons with integers. + 2021-02-01 Martin Sebor PR middle-end/97172 -- cgit v1.1 From 27a804bc62805aedb1b097a00eb2c0059244680a Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Wed, 10 Feb 2021 12:07:10 -0500 Subject: 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. --- gcc/c/c-parser.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) (limited to 'gcc/c') diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c index a8df208..2a49d07 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)); -- cgit v1.1 From 0c5cdb31bd423f650aaa3917cc21de0cde91e0fc Mon Sep 17 00:00:00 2001 From: GCC Administrator Date: Fri, 12 Feb 2021 00:16:25 +0000 Subject: Daily bump. --- gcc/c/ChangeLog | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'gcc/c') diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog index b306491..1fee3e7 100644 --- a/gcc/c/ChangeLog +++ b/gcc/c/ChangeLog @@ -1,3 +1,7 @@ +2021-02-11 Marek Polacek + + * c-parser.c (c_parser_if_statement): Use vec_free. + 2021-02-04 Martin Sebor PR c/97882 -- cgit v1.1