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
5213 sec = NULL;5213 sec = NULL;
5214#ifdef CONFIG_TCC_BCHECK5214#ifdef CONFIG_TCC_BCHECK
5215 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {5215 if (tcc_state->do_bounds_check && (type->t & VT_ARRAY)) {
5216 if (type->t & VT_VLA)5216 if (!(type->t & VT_VLA))
5217 warning("Array bound check don't work for VLA");
5218 else
5219 loc--;5217 loc--;
5220 }5218 }
5221#endif5219#endif
tests/tcctest.c+33−1
@@ -2070,11 +2070,17 @@ void alloca_test() · 2069 unmodified lines
2070#endif2070#endif
2071}2071}
2072 2072
2073void *bounds_checking_is_enabled()
2074{
2075 char ca[10], *cp = ca-1;
2076 return (ca != cp + 1) ? cp : NULL;
2077}
2078
2073void c99_vla_test(int size1, int size2)2079void c99_vla_test(int size1, int size2)
2074{2080{
2075#if defined __i386__ || defined __x86_64__2081#if defined __i386__ || defined __x86_64__
2076 int tab1[size1 * size2][2], tab2[10][2];2082 int tab1[size1 * size2][2], tab2[10][2];
2077 void *tab1_ptr, *tab2_ptr;2083 void *tab1_ptr, *tab2_ptr, *bad_ptr;
2078 2084
2079 printf("Test C99 VLA 1 (sizeof): ");2085 printf("Test C99 VLA 1 (sizeof): ");
2080 printf("%s\n", (sizeof tab1 == size1 * size2 * 2 * sizeof(int)) ? "PASSED" : "FAILED");2086 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
2087 printf("Test C99 VLA 4 (ptr access): ");2093 printf("Test C99 VLA 4 (ptr access): ");
2088 tab1[size1][1] = 42;2094 tab1[size1][1] = 42;
2089 printf("%s\n", (*((int *) (tab1_ptr + (size1 * 2 + 1) * sizeof(int))) == 42) ? "PASSED" : "FAILED");2095 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");
2090#endif2122#endif
2091}2123}
2092 2124

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