Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fuzzer.cpp Improve Memory Safety and Optimize String Handling in Fuzzer #12816

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions projects/rapidjson/fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ limitations under the License.

#include <cstdint>
#include <cstddef>
#include <string>
#include <string_view>
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>

Expand All @@ -23,30 +23,31 @@ extern "C" {
#endif

template<unsigned parseFlags>
void fuzzWithFlags(const std::string &s)
{
/* Parse input to rapidjson::Document */
void fuzzWithFlags(const std::string_view &s) {
rapidjson::Document document;
rapidjson::ParseResult pr = document.Parse<parseFlags>(s.c_str());
if ( !pr ) {
rapidjson::ParseResult pr = document.Parse<parseFlags>(s.data());
Shivam7-1 marked this conversation as resolved.
Show resolved Hide resolved
if (!pr) {
return;
}

/* Convert from rapidjson::Document to string */
rapidjson::StringBuffer sb;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
document.Accept(writer);
std::string str = sb.GetString();
std::string_view result(sb.GetString(), sb.GetSize());

#ifdef MSAN
if ( str.size() ) {
__msan_check_mem_is_initialized(str.data(), str.size());
if (!result.empty()) {
__msan_check_mem_is_initialized(result.data(), result.size());
}
#endif
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
const std::string s(data, data + size);
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
if (size == 0) {
return 0;
}

std::string_view s(reinterpret_cast<const char*>(data), size);

fuzzWithFlags<rapidjson::kParseDefaultFlags>(s);
fuzzWithFlags<rapidjson::kParseFullPrecisionFlag>(s);
Expand Down
Loading