Skip to content

Commit

Permalink
Merge pull request #185 from boostorg/more_fuzz
Browse files Browse the repository at this point in the history
Fuzz `to_chars` for integers
  • Loading branch information
mborland authored Apr 19, 2024
2 parents 552b5b5 + 2ba5773 commit 92f714f
Show file tree
Hide file tree
Showing 5 changed files with 583 additions and 4 deletions.
60 changes: 60 additions & 0 deletions fuzzing/fuzz_to_chars_int.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2024 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/charconv.hpp>
#include <boost/core/detail/string_view.hpp>
#include <iostream>
#include <exception>

extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data, std::size_t size)
{
try
{
auto c_data = reinterpret_cast<const char*>(data);

for (int base = 2; base < 36; ++base)
{
char buffer[10]; // Small enough it should force overflows

char c_val {};
boost::charconv::from_chars(c_data, c_data + size, c_val, base);
boost::charconv::to_chars(buffer, buffer + sizeof(buffer), c_val, base);

int i_val {};
boost::charconv::from_chars(c_data, c_data + size, i_val, base);
boost::charconv::to_chars(buffer, buffer + sizeof(buffer), i_val, base);

long l_val {};
boost::charconv::from_chars(c_data, c_data + size, l_val, base);
boost::charconv::to_chars(buffer, buffer + sizeof(buffer), l_val, base);

long long ll_val {};
boost::charconv::from_chars(c_data, c_data + size, ll_val, base);
boost::charconv::to_chars(buffer, buffer + sizeof(buffer), ll_val, base);

unsigned char uc_val {};
boost::charconv::from_chars(c_data, c_data + size, uc_val, base);
boost::charconv::to_chars(buffer, buffer + sizeof(buffer), uc_val, base);

unsigned int ui_val {};
boost::charconv::from_chars(c_data, c_data + size, ui_val, base);
boost::charconv::to_chars(buffer, buffer + sizeof(buffer), ui_val, base);

unsigned long ul_val {};
boost::charconv::from_chars(c_data, c_data + size, ul_val, base);
boost::charconv::to_chars(buffer, buffer + sizeof(buffer), ul_val, base);

unsigned long long ull_val {};
boost::charconv::from_chars(c_data, c_data + size, ull_val, base);
boost::charconv::to_chars(buffer, buffer + sizeof(buffer), ull_val, base);
}
}
catch(...)
{
std::cerr << "Error with: " << data << std::endl;
std::terminate();
}

return 0;
}
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 92f714f

Please sign in to comment.