History

VLA bcheck works via bound alloca; add test, remove warning

Joe Soroka committed 15 years ago

Commit c94f8051 parent 810aca92 files · +34 −4Browse files

Unified · Split

2 files
  1. tccgen.c +1 −3
  2. tests/tcctest.c +33 −1
tccgen.c+1−3
@@ -5213,9 +5213,7 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, · 5212 unmodified lines
52135213 sec = NULL;
52145214 #ifdef CONFIG_TCC_BCHECK
52155215 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5216− if (type->t & VT_VLA)
5217− warning("Array bound check don't work for VLA");
5218− else
5216+ if (!(type->t & VT_VLA))
52195217 loc--;
52205218 }
52215219 #endif
tests/tcctest.c+33−1
@@ -2070,11 +2070,17 @@ void alloca_test() · 2069 unmodified lines
20702070 #endif
20712071 }
20722072
2073+void *bounds_checking_is_enabled()
2074+{
2075+ char ca[10], *cp = ca-1;
2076+ return (ca != cp + 1) ? cp : NULL;
2077+}
2078+
20732079 void c99_vla_test(int size1, int size2)
20742080 {
20752081 #if defined __i386__ || defined __x86_64__
20762082 int tab1[size1 * size2][2], tab2[10][2];
2077− void *tab1_ptr, *tab2_ptr;
2083+ void *tab1_ptr, *tab2_ptr, *bad_ptr;
20782084
20792085 printf("Test C99 VLA 1 (sizeof): ");
20802086 printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");
@@ -2087,6 +2093,32 @@ void c99_vla_test(int size1, int size2) · 6 unmodified lines
20872093 printf("Test C99 VLA 4 (ptr access): ");
20882094 tab1[size1][1] = 42;
20892095 printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");
2096+
2097+ printf("Test C99 VLA 5 (bounds checking (might be disabled)): ");
2098+ if (bad_ptr = bounds_checking_is_enabled()) {
2099+ int *t1 = &tab1[size1 * size2 - 1][3];
2100+ int *t2 = &tab2[9][3];
2101+ printf("%s ", bad_ptr == t1 ? "PASSED" : "FAILED");
2102+ printf("%s ", bad_ptr == t2 ? "PASSED" : "FAILED");
2103+
2104+ char*c1 = 1 + sizeof(tab1) + (char*)tab1;
2105+ char*c2 = 1 + sizeof(tab2) + (char*)tab2;
2106+ printf("%s ", bad_ptr == c1 ? "PASSED" : "FAILED");
2107+ printf("%s ", bad_ptr == c2 ? "PASSED" : "FAILED");
2108+
2109+ int *i1 = tab1[-1];
2110+ int *i2 = tab2[-1];
2111+ printf("%s ", bad_ptr == i1 ? "PASSED" : "FAILED");
2112+ printf("%s ", bad_ptr == i2 ? "PASSED" : "FAILED");
2113+
2114+ int *x1 = tab1[size1 * size2 + 1];
2115+ int *x2 = tab2[10 + 1];
2116+ printf("%s ", bad_ptr == x1 ? "PASSED" : "FAILED");
2117+ printf("%s ", bad_ptr == x2 ? "PASSED" : "FAILED");
2118+ } else {
2119+ printf("PASSED PASSED PASSED PASSED PASSED PASSED PASSED PASSED ");
2120+ }
2121+ printf("\n");
20902122 #endif
20912123 }
20922124

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