|
| 1 | +// Test arithmetic on null pointers. |
| 2 | +// |
| 3 | +// RUN: %clang %s -o %t1 %checkedc_target_flags |
| 4 | + |
| 5 | +// RUN: %checkedc_rununder %t1 1 -DARR | FileCheck %s --check-prefix FAIL1 |
| 6 | +// RUN: %checkedc_rununder %t1 2 -DARR | FileCheck %s --check-prefix FAIL2 |
| 7 | +// RUN: %checkedc_rununder %t1 3 -DARR | FileCheck %s --check-prefix FAIL3 |
| 8 | + |
| 9 | +// RUN: %checkedc_rununder %t1 1 | FileCheck %s --check-prefix FAIL1 |
| 10 | +// RUN: %checkedc_rununder %t1 2 | FileCheck %s --check-prefix FAIL2 |
| 11 | +// RUN: %checkedc_rununder %t1 3 | FileCheck %s --check-prefix FAIL3 |
| 12 | + |
| 13 | +// RUN: %checkedc_rununder %t1 100 -DARR | FileCheck %s --check-prefix PASS1 |
| 14 | +// RUN: %checkedc_rununder %t1 100 | FileCheck %s --check-prefix PASS1 |
| 15 | + |
| 16 | +#include <assert.h> |
| 17 | +#include <signal.h> |
| 18 | +#include <stdio.h> |
| 19 | +#include <stdlib.h> |
| 20 | +#include <string.h> |
| 21 | +#include <stdchecked.h> |
| 22 | + |
| 23 | +array_ptr<char> arr_p : count(3) = NULL; |
| 24 | +nt_array_ptr<char> nt_arr_p : count(3) = NULL; |
| 25 | + |
| 26 | +array_ptr<char> arr_s : count(3) = "abc"; |
| 27 | +int *t = NULL; |
| 28 | + |
| 29 | +void handle_error(int err) { |
| 30 | + puts("Error: null pointer arithmetic"); |
| 31 | + _Exit(0); |
| 32 | +} |
| 33 | + |
| 34 | +void fail1() { |
| 35 | + // FAIL1: Error: null pointer arithmetic |
| 36 | +#ifdef ARR |
| 37 | + arr_p++; |
| 38 | +#else |
| 39 | + nt_arr_p++; |
| 40 | +#endif |
| 41 | +} |
| 42 | + |
| 43 | +void fail2() { |
| 44 | + // FAIL2: Error: null pointer arithmetic |
| 45 | +#ifdef ARR |
| 46 | + arr_p[1]--; |
| 47 | +#else |
| 48 | + nt_arr_p[1]--; |
| 49 | +#endif |
| 50 | +} |
| 51 | + |
| 52 | +void f1(array_ptr<char> a) {} |
| 53 | +void f2(nt_array_ptr<char> a) {} |
| 54 | + |
| 55 | +void fail3() { |
| 56 | + // FAIL3: Error: null pointer arithmetic |
| 57 | +#ifdef ARR |
| 58 | + f1(--arr_p); |
| 59 | +#else |
| 60 | + f2(--nt_arr_p); |
| 61 | +#endif |
| 62 | +} |
| 63 | + |
| 64 | +void pass1() { |
| 65 | + // PASS1: PASS |
| 66 | +#ifdef ARR |
| 67 | + arr_s++; |
| 68 | +#else |
| 69 | + t++; |
| 70 | +#endif |
| 71 | + puts("PASS"); |
| 72 | +} |
| 73 | + |
| 74 | +int main(int argc, array_ptr<char*> argv : count(argc)) { |
| 75 | + signal(SIGILL, handle_error); |
| 76 | + |
| 77 | + // This makes sure output is not buffered for when |
| 78 | + // we hit errors. |
| 79 | + int err = setvbuf(stdout, NULL, _IONBF, 0); |
| 80 | + if (err) { |
| 81 | + // CHECK-NOT: Error Setting Up Buffering |
| 82 | + puts("Error Setting Up Buffering"); |
| 83 | + return EXIT_FAILURE; |
| 84 | + } |
| 85 | + |
| 86 | + if (argc < 2) { |
| 87 | + // CHECK-NOT: Requires Argument |
| 88 | + puts("Requires Argument"); |
| 89 | + return EXIT_FAILURE; |
| 90 | + } |
| 91 | + |
| 92 | + int testcase = atoi(argv[1]); |
| 93 | + switch (testcase) { |
| 94 | + case 1: |
| 95 | + fail1(); |
| 96 | + break; |
| 97 | + case 2: |
| 98 | + fail2(); |
| 99 | + break; |
| 100 | + case 3: |
| 101 | + fail3(); |
| 102 | + break; |
| 103 | + |
| 104 | + case 100: |
| 105 | + pass1(); |
| 106 | + break; |
| 107 | + |
| 108 | + default: |
| 109 | + // CHECK-NOT: Unexpected test case |
| 110 | + puts("Unexpected test case"); |
| 111 | + return EXIT_FAILURE; |
| 112 | + } |
| 113 | + return EXIT_SUCCESS; |
| 114 | +} |
0 commit comments