diff options
author | Lenny Maiorani <lenny@colorado.edu> | 2011-04-12 17:08:43 +0000 |
---|---|---|
committer | Lenny Maiorani <lenny@colorado.edu> | 2011-04-12 17:08:43 +0000 |
commit | 318dd92ad834857ea5bb91de288c1eb56cdbec1a (patch) | |
tree | 7cd0964513e7e99202a35600bfdec78774dce1d5 /test | |
parent | 627788c29976fbeb4ad47bcfcb3576889070e357 (diff) | |
download | clang-318dd92ad834857ea5bb91de288c1eb56cdbec1a.tar.gz |
This patch adds modeling of strcmp() to the CString checker. Validates inputs are not NULL and are real C strings, then does the comparison and binds the proper return value. Unit tests included.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@129364 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test')
-rw-r--r-- | test/Analysis/string.c | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/test/Analysis/string.c b/test/Analysis/string.c index d9e0cf8c4e..a68b4d7759 100644 --- a/test/Analysis/string.c +++ b/test/Analysis/string.c @@ -596,3 +596,89 @@ void strncat_no_overflow_2(char *y) { if (strlen(y) == 4) strncat(x, y, 1); // no-warning } + +//===----------------------------------------------------------------------=== +// strcmp() +//===----------------------------------------------------------------------=== + +#define strcmp BUILTIN(strcmp) +int strcmp(const char *restrict s1, const char *restrict s2); + +void strcmp_constant0() { + if (strcmp("123", "123") != 0) + (void)*(char*)0; // no-warning +} + +void strcmp_constant_and_var_0() { + char *x = "123"; + if (strcmp(x, "123") != 0) + (void)*(char*)0; // no-warning +} + +void strcmp_constant_and_var_1() { + char *x = "123"; + if (strcmp("123", x) != 0) + (void)*(char*)0; // no-warning +} + +void strcmp_0() { + char *x = "123"; + char *y = "123"; + if (strcmp(x, y) != 0) + (void)*(char*)0; // no-warning +} + +void strcmp_1() { + char *x = "234"; + char *y = "123"; + if (strcmp(x, y) != 1) + (void)*(char*)0; // no-warning +} + +void strcmp_2() { + char *x = "123"; + char *y = "234"; + if (strcmp(x, y) != -1) + (void)*(char*)0; // no-warning +} + +void strcmp_null_0() { + char *x = NULL; + char *y = "123"; + strcmp(x, y); // expected-warning{{Null pointer argument in call to byte string function}} +} + +void strcmp_null_1() { + char *x = "123"; + char *y = NULL; + strcmp(x, y); // expected-warning{{Null pointer argument in call to byte string function}} +} + +void strcmp_diff_length_0() { + char *x = "12345"; + char *y = "234"; + if (strcmp(x, y) != -1) + (void)*(char*)0; // no-warning +} + +void strcmp_diff_length_1() { + char *x = "123"; + char *y = "23456"; + if (strcmp(x, y) != -1) + (void)*(char*)0; // no-warning +} + +void strcmp_diff_length_2() { + char *x = "12345"; + char *y = "123"; + if (strcmp(x, y) != 1) + (void)*(char*)0; // no-warning +} + +void strcmp_diff_length_3() { + char *x = "123"; + char *y = "12345"; + if (strcmp(x, y) != -1) + (void)*(char*)0; // no-warning +} + |