-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is split out from #34311 to see which part of that PR is leading to clang-tidy failures.
- Loading branch information
1 parent
78adc4d
commit a70aeae
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <cstddef> | ||
#include <cstdint> | ||
#include <fstream> | ||
#include <iostream> | ||
#include <string.h> | ||
|
||
#include <setup_payload/Base38Decode.h> | ||
#include <setup_payload/Base38Encode.h> | ||
|
||
using namespace chip; | ||
|
||
/** | ||
* @file | ||
* This file describes a base38 roundtrip Fuzzer. | ||
* It starts by encoding the fuzzing value passed | ||
* in Base38. The value encoded will then be decoded. | ||
* The fuzzer verify that the decoded value is the same | ||
* as the one in input. | ||
*/ | ||
|
||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t * data, size_t len) | ||
{ | ||
size_t outputSizeNeeded = base38EncodedLength(len); | ||
const size_t kMaxOutputSize = 512; | ||
|
||
if (outputSizeNeeded > kMaxOutputSize) | ||
{ | ||
return 0; | ||
} | ||
|
||
ByteSpan span(data, len); | ||
char encodedBuf[kMaxOutputSize]; | ||
MutableCharSpan encodedSpan(encodedBuf); | ||
CHIP_ERROR encodingError = base38Encode(span, encodedSpan); | ||
|
||
if (encodingError != CHIP_NO_ERROR) | ||
{ | ||
__builtin_trap(); | ||
} | ||
|
||
std::string base38EncodedString(encodedSpan.data(), encodedSpan.size()); | ||
|
||
std::vector<uint8_t> decodedData; | ||
CHIP_ERROR decodingError = base38Decode(base38EncodedString, decodedData); | ||
|
||
if (decodingError == CHIP_NO_ERROR) | ||
{ | ||
if (decodedData.size() != len) | ||
{ | ||
__builtin_trap(); | ||
} | ||
|
||
if (memcmp(data, decodedData.data(), len) != 0) | ||
{ | ||
__builtin_trap(); | ||
} | ||
} | ||
else | ||
{ | ||
__builtin_trap(); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <cstddef> | ||
#include <cstdint> | ||
#include <iostream> | ||
|
||
#include <setup_payload/Base38Decode.h> | ||
|
||
using namespace chip; | ||
|
||
/** | ||
* @file | ||
* This file describes a Fuzzer for decoding base38 encoded strings. | ||
*/ | ||
|
||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t * data, size_t len) | ||
{ | ||
std::string base38EncodedString(reinterpret_cast<const char *>(data), len); | ||
std::vector<uint8_t> decodedData; | ||
|
||
// Ignoring return value, because in general the data is garbage and won't decode properly. | ||
// We're just testing that the decoder does not crash on the fuzzer-generated inputs. | ||
chip::base38Decode(base38EncodedString, decodedData); | ||
|
||
return 0; | ||
} |