From: yan-bin2009 <[email protected]>
C11 6.3.2.3 and 6.5.4 forbid both implicit and explicit conversions
between pointers (including function pointers) and floating-point
types. verify_assign_cast() and gen_cast() were silently allowing
them by treating the pointer value as an integer, so code like
double a = "foo";
double b = (double)main;
compiled and produced the address converted to a double.
Add the missing checks in verify_assign_cast() and gen_cast() so
these are diagnosed at compile time, and extend the
60_errors_and_warnings test with three cases covering implicit
conversion, explicit cast, and function-pointer argument.
Reported on the tinycc-devel mailing list:
https://lists.nongnu.org/archive/html/tinycc-devel/2026-09/msg00016.html
---
tccgen.c | 9 +++++++++
tests/tests2/60_errors_and_warnings.c | 23 ++++++++++++++++++++++
tests/tests2/60_errors_and_warnings.expect | 9 +++++++++
3 files changed, 41 insertions(+)
diff --git a/tccgen.c b/tccgen.c
index 1b634671..881baf33 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -3297,6 +3297,8 @@ error:
cast_error(&vtop->type, type);
}
+ if (df && !sf && !is_integer_btype(sbt_bt))
+ goto error;
c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
if (c) {
/* constant case: we can do it now */
@@ -3697,6 +3699,13 @@ static void verify_assign_cast(CType *dt)
}
/* XXX: more tests */
break;
+ case VT_FLOAT:
+ case VT_DOUBLE:
+ case VT_LDOUBLE:
+ if (sbt == VT_PTR || sbt == VT_FUNC)
+ goto error;
+ /* XXX: more tests */
+ break;
case VT_STRUCT:
case_VT_STRUCT:
if (!is_compatible_unqualified_types(dt, st)) {
diff --git a/tests/tests2/60_errors_and_warnings.c
b/tests/tests2/60_errors_and_warnings.c
index 2990bc43..0c05496c 100644
--- a/tests/tests2/60_errors_and_warnings.c
+++ b/tests/tests2/60_errors_and_warnings.c
@@ -698,5 +698,28 @@ void test(void)
{
destination = source;
}
+#elif defined test_ptr_to_double
+int main(void)
+{
+ double d = "foo";
+ return 0;
+}
+
+#elif defined test_ptr_to_double_explicit
+
+int main(void)
+{
+ double d = (double)"foo";
+ return 0;
+}
+
+#elif defined test_funcptr_to_double
+
+static double f(double x) { return x; }
+
+int main(void)
+{
+ return (int)f(main);
+}
#endif
diff --git a/tests/tests2/60_errors_and_warnings.expect
b/tests/tests2/60_errors_and_warnings.expect
index 375d6b4a..5a30516a 100644
--- a/tests/tests2/60_errors_and_warnings.expect
+++ b/tests/tests2/60_errors_and_warnings.expect
@@ -305,3 +305,12 @@ bar 15 12 34
[test_discarded_restrict]
60_errors_and_warnings.c:699: warning: assignment discards qualifiers from
pointer target type
+
+[test_ptr_to_double]
+60_errors_and_warnings.c:705: error: cannot convert 'char [4]' to 'double'
+
+[test_ptr_to_double_explicit]
+60_errors_and_warnings.c:713: error: cannot convert 'char [4]' to 'double'
+
+[test_funcptr_to_double]
+60_errors_and_warnings.c:723: error: cannot convert 'int ()' to 'double'
--
2.43.0
_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel