diff options
| author | seyko <seyko2@gmail.com> | 2015-04-10 15:17:22 +0300 |
|---|---|---|
| committer | seyko <seyko2@gmail.com> | 2015-04-10 15:17:22 +0300 |
| commit | 559675b90acec03b67ed3f2cece1940f00e44c49 (patch) | |
| tree | 3875745ba543b7b8bb8474d9b7910272fcf75b9d /tests/boundtest.c | |
| parent | e92dc595cd10405b3ed14b2663affeb50b47e169 (diff) | |
| download | tinycc-559675b90acec03b67ed3f2cece1940f00e44c49.tar.gz tinycc-559675b90acec03b67ed3f2cece1940f00e44c49.tar.bz2 | |
a bounds checking code for the ARCH=x86_64
Diffstat (limited to 'tests/boundtest.c')
| -rw-r--r-- | tests/boundtest.c | 61 |
1 files changed, 56 insertions, 5 deletions
diff --git a/tests/boundtest.c b/tests/boundtest.c index eb95a96..15bffb4 100644 --- a/tests/boundtest.c +++ b/tests/boundtest.c @@ -50,12 +50,15 @@ int test4(void) int i, sum = 0; int *tab4; + fprintf(stderr, "%s start\n", __FUNCTION__); + tab4 = malloc(20 * sizeof(int)); for(i=0;i<20;i++) { sum += tab4[i]; } free(tab4); + fprintf(stderr, "%s end\n", __FUNCTION__); return sum; } @@ -65,12 +68,15 @@ int test5(void) int i, sum = 0; int *tab4; + fprintf(stderr, "%s start\n", __FUNCTION__); + tab4 = malloc(20 * sizeof(int)); for(i=0;i<21;i++) { sum += tab4[i]; } free(tab4); + fprintf(stderr, "%s end\n", __FUNCTION__); return sum; } @@ -187,9 +193,44 @@ int test15(void) return strlen(p); } +/* ok */ +int test16() +{ + char *demo = "This is only a test."; + char *p; + + fprintf(stderr, "%s start\n", __FUNCTION__); + + p = alloca(16); + strcpy(p,"12345678901234"); + printf("alloca: p is %s\n", p); + + /* Test alloca embedded in a larger expression */ + printf("alloca: %s\n", strcpy(alloca(strlen(demo)+1),demo) ); + + fprintf(stderr, "%s end\n", __FUNCTION__); +} + +/* error */ +int test17() +{ + char *demo = "This is only a test."; + char *p; + + fprintf(stderr, "%s start\n", __FUNCTION__); + + p = alloca(16); + strcpy(p,"12345678901234"); + printf("alloca: p is %s\n", p); + + /* Test alloca embedded in a larger expression */ + printf("alloca: %s\n", strcpy(alloca(strlen(demo)),demo) ); + + fprintf(stderr, "%s end\n", __FUNCTION__); +} + int (*table_test[])(void) = { test1, - test1, test2, test3, test4, @@ -204,23 +245,33 @@ int (*table_test[])(void) = { test13, test14, test15, + test16, + test17, }; int main(int argc, char **argv) { int index; int (*ftest)(void); + int index_max = sizeof(table_test)/sizeof(table_test[0]); if (argc < 2) { - printf("usage: boundtest n\n" - "test TCC bound checking system\n" - ); + printf( + "test TCC bound checking system\n" + "usage: boundtest N\n" + " 1 <= N <= %d\n", index_max); exit(1); } index = 0; if (argc >= 2) - index = atoi(argv[1]); + index = atoi(argv[1]) - 1; + + if ((index < 0) || (index >= index_max)) { + printf("N is outside of the valid range (%d)\n", index); + exit(2); + } + /* well, we also use bounds on this ! */ ftest = table_test[index]; ftest(); |
