brintos

brintos / llvm-project-archived public Read only

0
0
Text · 7.8 KiB · f30f977 Raw
237 lines · cpp
1// RUN: %clang_analyze_cc1 %s \2// RUN:   -analyzer-checker=core \3// RUN:   -analyzer-checker=unix.StdCLibraryFunctions \4// RUN:   -analyzer-checker=debug.StdCLibraryFunctionsTester \5// RUN:   -analyzer-config unix.StdCLibraryFunctions:DisplayLoadedSummaries=true \6// RUN:   -analyzer-checker=debug.ExprInspection \7// RUN:   -analyzer-config eagerly-assume=false \8// RUN:   -triple i686-unknown-linux \9// RUN:   -verify10 11// In this test we verify that each argument constraints are described properly.12 13// Check NotNullConstraint violation notes.14int __not_null(int *);15void test_not_null(int *x) {16  __not_null(nullptr); // \17  // expected-warning{{The 1st argument to '__not_null' is NULL but should not be NULL [}}18}19 20// Check the BufferSizeConstraint violation notes.21using size_t = decltype(sizeof(int));22int __buf_size_arg_constraint_concrete(const void *); // size <= 1023int __buf_size_arg_constraint(const void *, size_t);  // size <= Arg124int __buf_size_arg_constraint_mul(const void *, size_t, size_t); // size <= Arg1 * Arg225void test_buffer_size(int x) {26  switch (x) {27  case 1: {28    char buf[9];29    __buf_size_arg_constraint_concrete(buf); // \30    // expected-warning{{The 1st argument to '__buf_size_arg_constraint_concrete' is a buffer with size 9 but should be a buffer with size equal to or greater than 10 [}}31    break;32  }33  case 2: {34    char buf[3];35    __buf_size_arg_constraint(buf, 4); // \36    // expected-warning{{The 1st argument to '__buf_size_arg_constraint' is a buffer with size 3 but should be a buffer with size equal to or greater than the value of the 2nd argument (which is 4) [}}37    break;38  }39  case 3: {40    char buf[3];41    __buf_size_arg_constraint_mul(buf, 4, 2); // \42    // expected-warning{{The 1st argument to '__buf_size_arg_constraint_mul' is a buffer with size 3 but should be a buffer with size equal to or greater than the value of the 2nd argument (which is 4) times the 3rd argument (which is 2) [}}43    break;44  }45  }46}47 48// Check the RangeConstraint violation notes.49 50int __single_val_0(int); // [0, 0]51int __single_val_1(int); // [1, 1]52int __range_1_2(int); // [1, 2]53int __range_m1_1(int); // [-1, 1]54int __range_m2_m1(int); // [-2, -1]55int __range_m10_10(int); // [-10, 10]56int __range_m1_inf(int); // [-1, inf]57int __range_0_inf(int); // [0, inf]58int __range_1_inf(int); // [1, inf]59int __range_minf_m1(int); // [-inf, -1]60int __range_minf_0(int); // [-inf, 0]61int __range_minf_1(int); // [-inf, 1]62int __range_1_2__4_6(int); // [1, 2], [4, 6]63int __range_1_2__4_inf(int); // [1, 2], [4, inf]64 65int __single_val_out_0(int); // [0, 0]66int __single_val_out_1(int); // [1, 1]67int __range_out_1_2(int); // [1, 2]68int __range_out_m1_1(int); // [-1, 1]69int __range_out_m2_m1(int); // [-2, -1]70int __range_out_m10_10(int); // [-10, 10]71int __range_out_m1_inf(int); // [-1, inf]72int __range_out_0_inf(int); // [0, inf]73int __range_out_1_inf(int); // [1, inf]74int __range_out_minf_m1(int); // [-inf, -1]75int __range_out_minf_0(int); // [-inf, 0]76int __range_out_minf_1(int); // [-inf, 1]77int __range_out_1_2__4_6(int); // [1, 2], [4, 6]78int __range_out_1_2__4_inf(int); // [1, 2], [4, inf]79 80int __test_case_range_1_2__4_6(int);81 82void test_range_values(int x) {83  switch (x) {84  case 0:85    __single_val_0(1); // expected-warning{{is 1 but should be zero}}86    break;87  case 1:88    __single_val_1(2); // expected-warning{{is 2 but should be 1}}89    break;90  case 2:91    __range_1_2(3); // expected-warning{{is 3 but should be 1 or 2}}92    break;93  case 3:94    __range_m1_1(3); // expected-warning{{is 3 but should be between -1 and 1}}95    break;96  case 4:97    __range_m2_m1(1); // expected-warning{{is 1 but should be -2 or -1}}98    break;99  case 5:100    __range_m10_10(11); // expected-warning{{is 11 but should be between -10 and 10}}101    break;102  case 6:103    __range_m10_10(-11); // expected-warning{{is -11 but should be between -10 and 10}}104    break;105  case 7:106    __range_1_2__4_6(3); // expected-warning{{is 3 but should be 1 or 2 or between 4 and 6}}107    break;108  case 8:109    __range_1_2__4_inf(3); // expected-warning{{is 3 but should be 1 or 2 or >= 4}}110    break;111  }112}113 114void test_range_values_inf(int x) {115  switch (x) {116  case 1:117    __range_m1_inf(-2); // expected-warning{{is -2 but should be >= -1}}118    break;119  case 2:120    __range_0_inf(-1); // expected-warning{{is -1 but should be >= 0}}121    break;122  case 3:123    __range_1_inf(0); // expected-warning{{is 0 but should be > 0}}124    break;125  case 4:126    __range_minf_m1(0); // expected-warning{{is 0 but should be < 0}}127    break;128  case 5:129    __range_minf_0(1); // expected-warning{{is 1 but should be <= 0}}130    break;131  case 6:132    __range_minf_1(2); // expected-warning{{is 2 but should be <= 1}}133    break;134  }135}136 137void test_range_values_out(int x) {138  switch (x) {139  case 0:140    __single_val_out_0(0); // expected-warning{{is 0 but should be nonzero}}141    break;142  case 1:143    __single_val_out_1(1); // expected-warning{{is 1 but should be not equal to 1}}144    break;145  case 2:146    __range_out_1_2(2); // expected-warning{{is 2 but should be not 1 and not 2}}147    break;148  case 3:149    __range_out_m1_1(-1); // expected-warning{{is -1 but should be not between -1 and 1}}150    break;151  case 4:152    __range_out_m2_m1(-2); // expected-warning{{is -2 but should be not -2 and not -1}}153    break;154  case 5:155    __range_out_m10_10(0); // expected-warning{{is 0 but should be not between -10 and 10}}156    break;157  case 6:158    __range_out_1_2__4_6(1); // expected-warning{{is 1 but should be not 1 and not 2 and not between 4 and 6}}159    break;160  case 7:161    __range_out_1_2__4_inf(4); // expected-warning{{is 4 but should be not 1 and not 2 and < 4}}162    break;163  }164}165 166void test_range_values_out_inf(int x) {167  switch (x) {168  case 1:169    __range_out_minf_m1(-1); // expected-warning{{is -1 but should be >= 0}}170    break;171  case 2:172    __range_out_minf_0(0); // expected-warning{{is 0 but should be > 0}}173    break;174  case 3:175    __range_out_minf_1(1); // expected-warning{{is 1 but should be > 1}}176    break;177  case 4:178    __range_out_m1_inf(-1); // expected-warning{{is -1 but should be < -1}}179    break;180  case 5:181    __range_out_0_inf(0); // expected-warning{{is 0 but should be < 0}}182    break;183  case 6:184    __range_out_1_inf(1); // expected-warning{{is 1 but should be <= 0}}185    break;186  }187}188 189void test_explanation(int x, int y) {190  switch (y) {191  case 1:192    if (x > 0)193      __single_val_0(x); // expected-warning{{is > 0 but should be zero [}}194    return;195  case 2:196    if (x < 0)197      __single_val_0(x); // expected-warning{{is < 0 but should be zero [}}198    return;199  case 3:200    if (x < -1)201      __single_val_0(x); // expected-warning{{is < 0 but should be zero [}}202    return;203  case 4:204    if (x != 0)205      __single_val_0(x); // expected-warning{{is out of the accepted range; It should be zero [}}206    return;207  case 5:208    if (x == 3)209      __range_1_2__4_6(x); // expected-warning{{is 3 but should be 1 or 2 or between 4 and 6 [}}210    return;211  case 6:212    if (x > 6)213      __range_1_2__4_6(x); // expected-warning{{is >= 7 but should be 1 or 2 or between 4 and 6 [}}214    return;215  case 7:216    if (x < 1)217      __range_1_2__4_6(x); // expected-warning{{is <= 0 but should be 1 or 2 or between 4 and 6 [}}218    return;219  case 8:220    if (__test_case_range_1_2__4_6(x) == 1)221      __range_1_2__4_6(x); // expected-warning{{is 3 or <= 0 but should be 1 or 2 or between 4 and 6 [}}222    return;223  case 9:224    if (__test_case_range_1_2__4_6(x) == 2)225      __range_1_2__4_6(x); // expected-warning{{is 3 or >= 7 but should be 1 or 2 or between 4 and 6 [}}226    return;227  case 10:228    if (__test_case_range_1_2__4_6(x) == 3)229      __range_1_2__4_6(x); // expected-warning{{is <= 0 or >= 7 but should be 1 or 2 or between 4 and 6 [}}230    return;231  case 11:232    if (__test_case_range_1_2__4_6(x) == 4)233      __range_1_2__4_6(x); // expected-warning{{is out of the accepted range; It should be 1 or 2 or between 4 and 6 [}}234    return;235  }236}237