Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Successful run of analyzer integration tests on x86_64-pc-linux-gnu.
Pushed to trunk as r14-9076-g5651ad62b08096.
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>
---
gcc/analyzer/varargs.cc | 38 ++++++++--
.../analyzer/stdarg-pr111289-int.c | 69 +++++++++++++++++++
.../analyzer/stdarg-pr111289-ptr.c | 39 +++++++++++
3 files changed, 142 insertions(+), 4 deletions(-)
create mode 100644 gcc/testsuite/c-c++-common/analyzer/stdarg-pr111289-int.c
create mode 100644 gcc/testsuite/c-c++-common/analyzer/stdarg-pr111289-ptr.c
@@ -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
{
new file mode 100644
@@ -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);
+}
new file mode 100644
@@ -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);
+}