From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2209) id 0B9693858D35; Mon, 19 Feb 2024 23:10:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0B9693858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1708384208; bh=goQskMs0/O0CmFuvb7D8nwo8dlrmQfMQgeNwBUtz2ig=; h=From:To:Subject:Date:From; b=J0KcKAS9tbQo5oZc8MxSLVhldPD2qsHZIqzdIsPHvXuWqs0JRL5PLnCiDackJtjJU lZ+6j125I02tzT529pboUgNhlNAgIUEZBFH3eRZb8n5FqUvtLUwA8QQbX6oz7eP6uk tN5uSrU8NlT+B4cKbHfxm+zjGqbs9iA9RZfrHTAA= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: David Malcolm To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-9076] analyzer: fix -Wanalyzer-va-arg-type-mismatch false +ve on int types [PR111289] X-Act-Checkin: gcc X-Git-Author: David Malcolm X-Git-Refname: refs/heads/master X-Git-Oldrev: eb37ea529745c38dcf86c3cdbedb66df69ea9e35 X-Git-Newrev: 5651ad62b08096a155a7e394c7494f5ff1c04f4f Message-Id: <20240219231008.0B9693858D35@sourceware.org> Date: Mon, 19 Feb 2024 23:10:06 +0000 (GMT) List-Id: https://gcc.gnu.org/g:5651ad62b08096a155a7e394c7494f5ff1c04f4f commit r14-9076-g5651ad62b08096a155a7e394c7494f5ff1c04f4f Author: David Malcolm Date: Mon Feb 19 18:08:05 2024 -0500 analyzer: fix -Wanalyzer-va-arg-type-mismatch false +ve on int types [PR111289] gcc/analyzer/ChangeLog: PR analyzer/111289 * varargs.cc (representable_in_integral_type_p): New. (va_arg_compatible_types_p): Add "arg_sval" param. Handle integer types. (kf_va_arg::impl_call_pre): Pass arg_sval to va_arg_compatible_types_p. gcc/testsuite/ChangeLog: PR analyzer/111289 * c-c++-common/analyzer/stdarg-pr111289-int.c: New test. * c-c++-common/analyzer/stdarg-pr111289-ptr.c: New test. Signed-off-by: David Malcolm Diff: --- gcc/analyzer/varargs.cc | 38 ++++++++++-- .../c-c++-common/analyzer/stdarg-pr111289-int.c | 69 ++++++++++++++++++++++ .../c-c++-common/analyzer/stdarg-pr111289-ptr.c | 39 ++++++++++++ 3 files changed, 142 insertions(+), 4 deletions(-) diff --git a/gcc/analyzer/varargs.cc b/gcc/analyzer/varargs.cc index ac0e1cc1af38..3348121a0ef8 100644 --- a/gcc/analyzer/varargs.cc +++ b/gcc/analyzer/varargs.cc @@ -950,13 +950,43 @@ public: } }; -/* Return true if it's OK to copy a value from ARG_TYPE to LHS_TYPE via +static bool +representable_in_integral_type_p (const svalue &sval, const_tree type) +{ + gcc_assert (INTEGRAL_TYPE_P (type)); + + if (tree cst = sval.maybe_get_constant ()) + return wi::fits_to_tree_p (wi::to_wide (cst), type); + + return true; +} + +/* Return true if it's OK to copy ARG_SVAL from ARG_TYPE to LHS_TYPE via va_arg (where argument promotion has already happened). */ static bool -va_arg_compatible_types_p (tree lhs_type, tree arg_type) +va_arg_compatible_types_p (tree lhs_type, tree arg_type, const svalue &arg_sval) { - return compat_types_p (arg_type, lhs_type); + if (compat_types_p (arg_type, lhs_type)) + return true; + + /* It's OK if both types are integer types, where one is signed and the + other type the corresponding unsigned type, when the value is + representable in both types. */ + if (INTEGRAL_TYPE_P (lhs_type) + && INTEGRAL_TYPE_P (arg_type) + && TYPE_UNSIGNED (lhs_type) != TYPE_UNSIGNED (arg_type) + && TYPE_PRECISION (lhs_type) == TYPE_PRECISION (arg_type) + && representable_in_integral_type_p (arg_sval, lhs_type) + && representable_in_integral_type_p (arg_sval, arg_type)) + return true; + + /* It's OK if one type is a pointer to void and the other is a + pointer to a character type. + This is handled by compat_types_p. */ + + /* Otherwise the types are not compatible. */ + return false; } /* If AP_SVAL is a pointer to a var_arg_region, return that var_arg_region. @@ -1022,7 +1052,7 @@ kf_va_arg::impl_call_pre (const call_details &cd) const { tree lhs_type = cd.get_lhs_type (); tree arg_type = arg_sval->get_type (); - if (va_arg_compatible_types_p (lhs_type, arg_type)) + if (va_arg_compatible_types_p (lhs_type, arg_type, *arg_sval)) cd.maybe_set_lhs (arg_sval); else { diff --git a/gcc/testsuite/c-c++-common/analyzer/stdarg-pr111289-int.c b/gcc/testsuite/c-c++-common/analyzer/stdarg-pr111289-int.c new file mode 100644 index 000000000000..33d83169c3e0 --- /dev/null +++ b/gcc/testsuite/c-c++-common/analyzer/stdarg-pr111289-int.c @@ -0,0 +1,69 @@ +#include +#include +#include + +typedef unsigned int mode_t; + +extern void openat (int, const char *, int, mode_t); + +/* Signed vs unsigned of same integral type. */ + +static void +test_1 (char const *name, ...) +{ + va_list arg; + va_start (arg, name); + + mode_t mode = va_arg (arg, mode_t); /* { dg-bogus "-Wanalyzer-va-arg-type-mismatch" } */ + + va_end (arg); + openat (-42, name, 0, mode); +} + +void +call_test_1 () +{ + test_1 ("nonexist.ent/", 0600); +} + +/* Not the same size: small enough for int promotion. */ + +int16_t global_2; + +static void +test_2 (char const *name, ...) +{ + va_list arg; + va_start (arg, name); + + global_2 = va_arg (arg, int16_t); /* { dg-warning "promoted to 'int'" } */ + + va_end (arg); +} + +void +call_test_2 () +{ + test_2 ("nonexist.ent/", 42); +} + +/* Not the same size: too big for int promotion. */ + +long long global_3; + +static void +test_3 (char const *name, ...) +{ + va_list arg; + va_start (arg, name); + + global_3 = va_arg (arg, long long); /* { dg-warning "'va_arg' expected 'long long int' but received 'int' for variadic argument 1 of 'arg'" } */ + + va_end (arg); +} + +void +call_test_3 () +{ + test_3 ("nonexist.ent/", 42); +} diff --git a/gcc/testsuite/c-c++-common/analyzer/stdarg-pr111289-ptr.c b/gcc/testsuite/c-c++-common/analyzer/stdarg-pr111289-ptr.c new file mode 100644 index 000000000000..7bdbf256d593 --- /dev/null +++ b/gcc/testsuite/c-c++-common/analyzer/stdarg-pr111289-ptr.c @@ -0,0 +1,39 @@ +#include + +static void * +test_1 (const char *fmt, ...) +{ + va_list arg; + va_start (arg, fmt); + + void *p = va_arg (arg, void *); /* { dg-bogus "-Wanalyzer-va-arg-type-mismatch" } */ + + va_end (arg); + + return p; +} + +void * +call_test_1 () +{ + return test_1 ("fmt", "foo"); +} + +static char * +test_2 (const char *fmt, ...) +{ + va_list arg; + va_start (arg, fmt); + + char *p = va_arg (arg, char *); /* { dg-bogus "-Wanalyzer-va-arg-type-mismatch" } */ + + va_end (arg); + + return p; +} + +char * +call_test_2 (void *q) +{ + return test_2 ("fmt", q); +}