diff options
| author | Jordy Rose <jediknil@belkadan.com> | 2010-07-07 08:15:01 +0000 |
|---|---|---|
| committer | Jordy Rose <jediknil@belkadan.com> | 2010-07-07 08:15:01 +0000 |
| commit | 65136fb66985a54cc30b67474ea994f761ef63f1 (patch) | |
| tree | 654c258cc0d700691cfb1cd98da9b3d001e1584b /clang/test/Analysis/bstring.c | |
| parent | 86f86f532a3ce7ca73795656ca305493f4453497 (diff) | |
| download | bcm5719-llvm-65136fb66985a54cc30b67474ea994f761ef63f1.tar.gz bcm5719-llvm-65136fb66985a54cc30b67474ea994f761ef63f1.zip | |
Add memcmp() and bcmp() to CStringChecker. These check for valid access to the buffer arguments and have a special-case for when the buffer arguments are known to be the same address, or when the size is zero.
llvm-svn: 107761
Diffstat (limited to 'clang/test/Analysis/bstring.c')
| -rw-r--r-- | clang/test/Analysis/bstring.c | 70 |
1 files changed, 68 insertions, 2 deletions
diff --git a/clang/test/Analysis/bstring.c b/clang/test/Analysis/bstring.c index 467b87b8788..59d6318db33 100644 --- a/clang/test/Analysis/bstring.c +++ b/clang/test/Analysis/bstring.c @@ -8,8 +8,9 @@ //===----------------------------------------------------------------------=== // Some functions are so similar to each other that they follow the same code -// path, such as memcpy and __memcpy_chk. If VARIANT is defined, make sure to -// use the variants instead to make sure they are still checked by the analyzer. +// path, such as memcpy and __memcpy_chk, or memcmp and bcmp. If VARIANT is +// defined, make sure to use the variants instead to make sure they are still +// checked by the analyzer. // Some functions are implemented as builtins. These should be #defined as // BUILTIN(f), which will prepend "__builtin_" if USE_BUILTINS is defined. @@ -173,6 +174,71 @@ void memmove2 () { } //===----------------------------------------------------------------------=== +// memcmp() +//===----------------------------------------------------------------------=== + +#ifdef VARIANT + +#define bcmp BUILTIN(bcmp) +// __builtin_bcmp is not defined with const in Builtins.def. +int bcmp(/*const*/ void *s1, /*const*/ void *s2, size_t n); +#define memcmp bcmp + +#else /* VARIANT */ + +#define memcmp BUILTIN(memcmp) +int memcmp(const void *s1, const void *s2, size_t n); + +#endif /* VARIANT */ + + +void memcmp0 () { + char a[] = {1, 2, 3, 4}; + char b[4] = { 0 }; + + memcmp(a, b, 4); // no-warning +} + +void memcmp1 () { + char a[] = {1, 2, 3, 4}; + char b[10] = { 0 }; + + memcmp(a, b, 5); // expected-warning{{out-of-bound}} +} + +void memcmp2 () { + char a[] = {1, 2, 3, 4}; + char b[1] = { 0 }; + + memcmp(a, b, 4); // expected-warning{{out-of-bound}} +} + +void memcmp3 () { + char a[] = {1, 2, 3, 4}; + + if (memcmp(a, a, 4)) + (void)*(char*)0; // no-warning +} + +void memcmp4 (char *input) { + char a[] = {1, 2, 3, 4}; + + if (memcmp(a, input, 4)) + (void)*(char*)0; // expected-warning{{null}} +} + +void memcmp5 (char *input) { + char a[] = {1, 2, 3, 4}; + + if (memcmp(a, 0, 0)) // no-warning + (void)*(char*)0; // no-warning + if (memcmp(0, a, 0)) // no-warning + (void)*(char*)0; // no-warning + if (memcmp(a, input, 0)) // no-warning + (void)*(char*)0; // no-warning +} + +//===----------------------------------------------------------------------=== // bcopy() //===----------------------------------------------------------------------=== |

