diff options
author | cgyurgyik <gyurgyikcp@gmail.com> | 2020-06-23 07:19:45 -0400 |
---|---|---|
committer | cgyurgyik <gyurgyikcp@gmail.com> | 2020-06-23 07:24:03 -0400 |
commit | 4ffe2b24f5c7a856e607370e1e559e4c94803809 (patch) | |
tree | 089fd5e8061b5fb4005258047d0ab7713f001e4f /libc | |
parent | 5540765be603676a6e93745bce0ca9f3cce3e6a0 (diff) | |
download | llvm-4ffe2b24f5c7a856e607370e1e559e4c94803809.zip llvm-4ffe2b24f5c7a856e607370e1e559e4c94803809.tar.gz llvm-4ffe2b24f5c7a856e607370e1e559e4c94803809.tar.bz2 |
[libc] Add fuzz test for strcmp.
Summary:
Adds a fuzz test for string comparison.
This takes in two strings with associated lengths.
Verifies each string contains at least one character, and that the last character is the null terminator.
Then, finds the first instance where one of the following does not hold:
1. i < min(size1, size2)
2. s1[i] == s2[i]
3. s1[i] != '\0'
The result of strcmp is then compared to the value of the difference between s1[i] and s2[i]. For thoroughness, the operands are reversed and also checked.
Reviewers: sivachandra, PaulkaToast
Reviewed By: sivachandra, PaulkaToast
Subscribers: mgorny, tschuett, ecnelises, libc-commits
Tags: #libc-project
Differential Revision: https://reviews.llvm.org/D82247
Diffstat (limited to 'libc')
-rw-r--r-- | libc/fuzzing/string/strcmp_fuzz.cpp | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/libc/fuzzing/string/strcmp_fuzz.cpp b/libc/fuzzing/string/strcmp_fuzz.cpp index 14c6e4d..6ba8440 100644 --- a/libc/fuzzing/string/strcmp_fuzz.cpp +++ b/libc/fuzzing/string/strcmp_fuzz.cpp @@ -10,7 +10,6 @@ /// //===----------------------------------------------------------------------===// #include "src/string/strcmp.h" -#include <algorithm> #include <stdint.h> extern "C" int LLVMFuzzerTestTwoInputs(const uint8_t *data1, size_t size1, @@ -25,15 +24,17 @@ extern "C" int LLVMFuzzerTestTwoInputs(const uint8_t *data1, size_t size1, const char *s1 = reinterpret_cast<const char *>(data1); const char *s2 = reinterpret_cast<const char *>(data2); - const size_t minimum_size = std::min(size1, size2); + const size_t minimum_size = size1 < size2 ? size1 : size2; // Iterate through until either the minimum size is hit, // a character is the null terminator, or the first set // of differed bytes between s1 and s2 are found. // No bytes following a null byte should be compared. size_t i; - for (i = 0; i < minimum_size && s1[i] && s1[i] == s2[i]; ++i) - ; + for (i = 0; i < minimum_size; ++i) { + if (!s1[i] || s1[i] != s2[i]) + break; + } int expected_result = s1[i] - s2[i]; int actual_result = __llvm_libc::strcmp(s1, s2); |