public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-9076] analyzer: fix -Wanalyzer-va-arg-type-mismatch false +ve on int types [PR111289]
@ 2024-02-19 23:10 David Malcolm
  0 siblings, 0 replies; only message in thread
From: David Malcolm @ 2024-02-19 23:10 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:5651ad62b08096a155a7e394c7494f5ff1c04f4f

commit r14-9076-g5651ad62b08096a155a7e394c7494f5ff1c04f4f
Author: David Malcolm <dmalcolm@redhat.com>
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 <dmalcolm@redhat.com>

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 <stdarg.h>
+#include <stdint.h>
+#include <limits.h>
+
+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 <stdarg.h>
+
+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);
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-02-19 23:10 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-19 23:10 [gcc r14-9076] analyzer: fix -Wanalyzer-va-arg-type-mismatch false +ve on int types [PR111289] David Malcolm

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).