History

Fix incorrect one-sided void handling in ?:

Also make the case when one side in ?: is a ptr and the other is neither a ptr nor a null-ptr constant fail cleanly instead of segfaulting.

Petr Skocik committed 7 years ago

Commit f7779ef1 parent 49dfb572 files · +26 −20Browse files

Unified · Split

2 files
  1. tccgen.c +8 −6
  2. tests/tests2/33_ternary_op.c +18 −14
tccgen.c+8−6
@@ -5598,7 +5598,9 @@ static void expr_cond(void) · 5597 unmodified lines
55985598
55995599
56005600 /* cast operands to correct type according to ISOC rules */
5601− if (is_float(bt1) || is_float(bt2)) {
5601+ if (bt1 == VT_VOID || bt2 == VT_VOID) {
5602+ type.t = VT_VOID; /* NOTE: as an extension, we accept void on only one side */
5603+ }else if (is_float(bt1) || is_float(bt2)) {
56025604 if (bt1 == VT_LDOUBLE || bt2 == VT_LDOUBLE) {
56035605 type.t = VT_LDOUBLE;
56045606
@@ -5625,8 +5627,11 @@ static void expr_cond(void) · 20 unmodified lines
56255627 if (is_null_pointer (vtop)) type = type1;
56265628 else if (is_null_pointer (&sv)) type = type2;
56275629 else{
5628− int pbt1 = (pointed_type(&type1)->t&VT_BTYPE);
5629− int pbt2 = (pointed_type(&type2)->t&VT_BTYPE);
5630+ int pbt1, pbt2;
5631+ if (bt1!=bt2)
5632+ tcc_error("incompatible types in conditional expressions");
5633+ pbt1 = (pointed_type(&type1)->t&VT_BTYPE);
5634+ pbt2 = (pointed_type(&type2)->t&VT_BTYPE);
56305635 /*pointers to void get preferred, otherwise the pointed to types minus qualifs should be compatible*/
56315636 type = (pbt1==VT_VOID) ? type1 : type2;
56325637 if (pbt1!=VT_VOID && pbt2!=VT_VOID){
@@ -5652,9 +5657,6 @@ static void expr_cond(void) · 19 unmodified lines
56525657 } else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
56535658 /* XXX: test structure compatibility */
56545659 type = bt1 == VT_STRUCT ? type1 : type2;
5655− } else if (bt1 == VT_VOID || bt2 == VT_VOID) {
5656− /* NOTE: as an extension, we accept void on only one side */
5657− type.t = VT_VOID;
56585660 } else {
56595661 /* integer operations */
56605662 type.t = VT_INT | (VT_LONG & (t1 | t2));
tests/tests2/33_ternary_op.c+18−14
@@ -6,20 +6,24 @@ static void f (void){} · 5 unmodified lines
66 void (*fp)(void) = f;
77 void call_fp()
88 {
9− (fp?f:f)();
10− (fp?fp:fp)();
11− (fp?fp:&f)();
12− (fp?&f:fp)();
13− (fp?&f:&f)();
14− _Generic(0?arr:arr, char*: (void)0);
15− _Generic(0?&arr[0]:arr, char*: (void)0);
16− _Generic(0?arr:&arr[0], char*: (void)0);
17− _Generic(1?arr:arr, char*: (void)0);
18− _Generic(1?&arr[0]:arr, char*: (void)0);
19− _Generic(1?arr:&arr[0], char*: (void)0);
20− _Generic((__typeof(1?f:f)*){0}, void (**)(void): (void)0);
21− (fp?&f:f)();
22− (fp?f:&f)();
9+ (fp?f:f)();
10+ (fp?fp:fp)();
11+ (fp?fp:&f)();
12+ (fp?&f:fp)();
13+ (fp?&f:&f)();
14+ _Generic(0?arr:arr, char*: (void)0);
15+ _Generic(0?&arr[0]:arr, char*: (void)0);
16+ _Generic(0?arr:&arr[0], char*: (void)0);
17+ _Generic(1?arr:arr, char*: (void)0);
18+ _Generic(1?&arr[0]:arr, char*: (void)0);
19+ _Generic(1?arr:&arr[0], char*: (void)0);
20+ _Generic((__typeof(1?f:f)*){0}, void (**)(void): (void)0);
21+ (fp?&f:f)();
22+ (fp?f:&f)();
23+ _Generic((__typeof(fp?0L:(void)0)*){0}, void*: (void)0);
24+
25+ //Should cleanly fail, not segfault:
26+ /*(fp?f:1);*/
2327 }
2428
2529 int main()

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t