Skip to content

Commit c638ddf

Browse files
lemon-yan163yan-bin2009
authored andcommitted
tccgen: reject pointer/function-pointer to floating-point conversions
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
1 parent 2d3eea2 commit c638ddf

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

‎tccgen.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3297,6 +3297,8 @@ static void gen_cast(CType *type)
32973297
cast_error(&vtop->type, type);
32983298
}
32993299

3300+
if (df && !sf && !is_integer_btype(sbt_bt))
3301+
goto error;
33003302
c = (vtop->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST;
33013303
if (c) {
33023304
/* constant case: we can do it now */
@@ -3697,6 +3699,13 @@ static void verify_assign_cast(CType *dt)
36973699
}
36983700
/* XXX: more tests */
36993701
break;
3702+
case VT_FLOAT:
3703+
case VT_DOUBLE:
3704+
case VT_LDOUBLE:
3705+
if (sbt == VT_PTR || sbt == VT_FUNC)
3706+
goto error;
3707+
/* XXX: more tests */
3708+
break;
37003709
case VT_STRUCT:
37013710
case_VT_STRUCT:
37023711
if (!is_compatible_unqualified_types(dt, st)) {

‎tests/tests2/60_errors_and_warnings.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,5 +698,28 @@ void test(void)
698698
{
699699
destination = source;
700700
}
701+
#elif defined test_ptr_to_double
701702

703+
int main(void)
704+
{
705+
double d = "foo";
706+
return 0;
707+
}
708+
709+
#elif defined test_ptr_to_double_explicit
710+
711+
int main(void)
712+
{
713+
double d = (double)"foo";
714+
return 0;
715+
}
716+
717+
#elif defined test_funcptr_to_double
718+
719+
static double f(double x) { return x; }
720+
721+
int main(void)
722+
{
723+
return (int)f(main);
724+
}
702725
#endif

‎tests/tests2/60_errors_and_warnings.expect‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,12 @@ bar 15 12 34
305305

306306
[test_discarded_restrict]
307307
60_errors_and_warnings.c:699: warning: assignment discards qualifiers from pointer target type
308+
309+
[test_ptr_to_double]
310+
60_errors_and_warnings.c:705: error: cannot convert 'char [4]' to 'double'
311+
312+
[test_ptr_to_double_explicit]
313+
60_errors_and_warnings.c:713: error: cannot convert 'char [4]' to 'double'
314+
315+
[test_funcptr_to_double]
316+
60_errors_and_warnings.c:723: error: cannot convert 'int ()' to 'double'

0 commit comments

Comments
 (0)