History

Add some missing nocode_wanted guard

int i = i++ causes a segfault because of missing guard. Looking recursively at all backend functions called from middle end several more guard appeared to be missing.

Thomas Preud'homme committed 11 years ago

Commit 5bcc3ee1 parent 68605ab4 files · +30 −16Browse files

Unified · Split

4 files
  1. tccgen.c +27 −16
  2. tests/tests2/74_nocode_wanted.c +1 −0
  3. tests/tests2/74_nocode_wanted.expect +1 −0
  4. tests/tests2/Makefile +1 −0
tccgen.c+27−16
@@ -683,7 +683,7 @@ static void move_reg(int r, int s, int t) · 682 unmodified lines
683683 /* get address of vtop (vtop MUST BE an lvalue) */
684684 ST_FUNC void gaddrof(void)
685685 {
686− if (vtop->r & VT_REF)
686+ if (vtop->r & VT_REF && !nocode_wanted)
687687 gv(RC_INT);
688688 vtop->r &= ~VT_LVAL;
689689 /* tricky: if saved lvalue, then we can go back to lvalue */
@@ -1848,7 +1848,7 @@ ST_FUNC void gen_op(int op) · 1158 unmodified lines
18481848 }
18491849 }
18501850 // Make sure that we have converted to an rvalue:
1851− if (vtop->r & VT_LVAL)
1851+ if (vtop->r & VT_LVAL && !nocode_wanted)
18521852 gv(is_float(vtop->type.t & VT_BTYPE) ? RC_FLOAT : RC_INT);
18531853 }
18541854
@@ -1951,7 +1951,7 @@ static void gen_cast(CType *type) · 96 unmodified lines
19511951 }
19521952
19531953 /* bitfields first get cast to ints */
1954− if (vtop->type.t & VT_BITFIELD) {
1954+ if (vtop->type.t & VT_BITFIELD && !nocode_wanted) {
19551955 gv(RC_INT);
19561956 }
19571957
@@ -2057,7 +2057,7 @@ static void gen_cast(CType *type) · 99 unmodified lines
20572057 }
20582058 #if !defined(TCC_TARGET_ARM64) && !defined(TCC_TARGET_X86_64)
20592059 } else if ((dbt & VT_BTYPE) == VT_LLONG) {
2060− if ((sbt & VT_BTYPE) != VT_LLONG) {
2060+ if ((sbt & VT_BTYPE) != VT_LLONG && !nocode_wanted) {
20612061 /* scalar to long long */
20622062 /* machine independent conversion */
20632063 gv(RC_INT);
@@ -2085,7 +2085,7 @@ static void gen_cast(CType *type) · 21 unmodified lines
20852085 (dbt & VT_BTYPE) == VT_FUNC) {
20862086 if ((sbt & VT_BTYPE) != VT_LLONG &&
20872087 (sbt & VT_BTYPE) != VT_PTR &&
2088− (sbt & VT_BTYPE) != VT_FUNC) {
2088+ (sbt & VT_BTYPE) != VT_FUNC && !nocode_wanted) {
20892089 /* need to convert from 32bit to 64bit */
20902090 gv(RC_INT);
20912091 if (sbt != (VT_INT | VT_UNSIGNED)) {
@@ -2115,7 +2115,7 @@ static void gen_cast(CType *type) · 23 unmodified lines
21152115 force_charshort_cast(dbt);
21162116 } else if ((dbt & VT_BTYPE) == VT_INT) {
21172117 /* scalar to int */
2118− if (sbt == VT_LLONG) {
2118+ if (sbt == VT_LLONG && !nocode_wanted) {
21192119 /* from long long: just take low order word */
21202120 lexpand();
21212121 vpop();
@@ -2614,15 +2614,15 @@ ST_FUNC void vstore(void) · 492 unmodified lines
26142614 vpop();
26152615
26162616 } else {
2617+ if (!nocode_wanted) {
26172618 #ifdef CONFIG_TCC_BCHECK
2618− /* bound check case */
2619− if (vtop[-1].r & VT_MUSTBOUND) {
2620− vswap();
2621− gbound();
2622− vswap();
2623− }
2619+ /* bound check case */
2620+ if (vtop[-1].r & VT_MUSTBOUND) {
2621+ vswap();
2622+ gbound();
2623+ vswap();
2624+ }
26242625 #endif
2625− if (!nocode_wanted) {
26262626 rc = RC_INT;
26272627 if (is_float(ft)) {
26282628 rc = RC_FLOAT;
@@ -2686,7 +2686,10 @@ ST_FUNC void inc(int post, int c) · 57 unmodified lines
26862686 test_lvalue();
26872687 vdup(); /* save lvalue */
26882688 if (post) {
2689− gv_dup(); /* duplicate value */
2689+ if (!nocode_wanted)
2690+ gv_dup(); /* duplicate value */
2691+ else
2692+ vdup(); /* duplicate value */
26902693 vrotb(3);
26912694 vrotb(3);
26922695 }
@@ -3773,6 +3776,8 @@ ST_FUNC void unary(void) · 1080 unmodified lines
37733776 gen_cast(&type);
37743777 }
37753778 } else if (tok == '{') {
3779+ if (nocode_wanted)
3780+ tcc_error("statement expression in global scope");
37763781 /* save all registers */
37773782 save_regs(0);
37783783 /* statement expression : we do not accept break/continue
@@ -3813,10 +3818,12 @@ ST_FUNC void unary(void) · 34 unmodified lines
38133818 vtop->c.i = !vtop->c.i;
38143819 } else if ((vtop->r & VT_VALMASK) == VT_CMP)
38153820 vtop->c.i = vtop->c.i ^ 1;
3816− else {
3821+ else if (!nocode_wanted) {
38173822 save_regs(1);
38183823 vseti(VT_JMP, gvtst(1, 0));
38193824 }
3825+ else
3826+ vtop--;
38203827 break;
38213828 case '~':
38223829 next();
@@ -3954,6 +3961,8 @@ ST_FUNC void unary(void) · 131 unmodified lines
39543961
39553962 #ifdef TCC_TARGET_ARM64
39563963 case TOK___va_start: {
3964+ if (!nocode_wanted)
3965+ tcc_error("statement in global scope");
39573966 next();
39583967 skip('(');
39593968 expr_eq();
@@ -3967,6 +3976,8 @@ ST_FUNC void unary(void) · 7 unmodified lines
39673976 break;
39683977 }
39693978 case TOK___va_arg: {
3979+ if (!nocode_wanted)
3980+ tcc_error("statement in global scope");
39703981 CType type;
39713982 next();
39723983 skip('(');
@@ -3992,7 +4003,7 @@ ST_FUNC void unary(void) · 19 unmodified lines
39924003 break;
39934004 }
39944005 #endif
3995−
4006+ /* pre operations */
39964007 case TOK_INC:
39974008 case TOK_DEC:
39984009 t = tok;
tests/tests2/74_nocode_wanted.c+1−0
@@ -0,0 +1 @@
1+int i = i++;
tests/tests2/74_nocode_wanted.expect+1−0
@@ -0,0 +1 @@
1+74_nocode_wanted.c:1: error: initializer element is not constant
tests/tests2/Makefile+1−0
@@ -93,6 +93,7 @@ TESTS = \ · 92 unmodified lines
9393 71_macro_empty_arg.test \
9494 72_long_long_constant.test \
9595 73_arm64.test \
96+ 74_nocode_wanted.test
9697
9798
9899 # 34_array_assignment.test -- array assignment is not in C standard

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