Skip to content

Commit

Permalink
Merge pull request #503 from cppalliance/overflow
Browse files Browse the repository at this point in the history
Add fuzzing to snprintf and fix overflow
  • Loading branch information
mborland authored Apr 18, 2024
2 parents ac3edde + 3e6412b commit 74b13a8
Show file tree
Hide file tree
Showing 11 changed files with 1,129 additions and 38 deletions.
8 changes: 4 additions & 4 deletions doc/decimal/cstdio.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ The following functions analogous to those from <cstdio> are provided:
namespace boost {
namespace decimal {
template <typename Dec>
int snprintf(char* buffer, std::size_t buf_size, const char* format, Dec value) noexcept;
template <typename... Dec>
int snprintf(char* buffer, std::size_t buf_size, const char* format, Dec... value) noexcept;
template <typename Dec>
int sprintf(char* buffer, const char* format, Dec value) noexcept;
template <typename... Dec>
int sprintf(char* buffer, const char* format, Dec... value) noexcept;
} //namespace decimal
} //namespace boost
Expand Down
50 changes: 50 additions & 0 deletions fuzzing/fuzz_snprintf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2024 Matt Borland
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/decimal.hpp>
#include <iostream>
#include <exception>
#include <string>
#include <array>

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

const auto formats = std::array<boost::decimal::chars_format, 4>{boost::decimal::chars_format::general,
boost::decimal::chars_format::fixed,
boost::decimal::chars_format::scientific,
boost::decimal::chars_format::hex};

const auto dec32_printf_formats = std::array<const char*, 4>{"%Hg", "%Hf", "%He", "%Ha"};
const auto dec64_printf_formats = std::array<const char*, 4>{"%Dg", "%Df", "%De", "%Da"};
const auto dec128_printf_formats = std::array<const char*, 4>{"%DDg", "%DDf", "%DDe", "%DDa"};

for (std::size_t i {}; i < 4; ++i)
{
char buffer[20]; // Small enough it should overflow sometimes

boost::decimal::decimal32 f_val {};
boost::decimal::from_chars(c_data, c_data + size, f_val, formats[i]);
boost::decimal::snprintf(buffer, sizeof(buffer), dec32_printf_formats[i], f_val);

boost::decimal::decimal64 val {};
boost::decimal::from_chars(c_data, c_data + size, val, formats[i]);
boost::decimal::snprintf(buffer, sizeof(buffer), dec64_printf_formats[i], val);

boost::decimal::decimal128 ld_val {};
boost::decimal::from_chars(c_data, c_data + size, ld_val, formats[i]);
boost::decimal::snprintf(buffer, sizeof(buffer), dec128_printf_formats[i], ld_val);
}
}
catch(...)
{
std::cerr << "Error with: " << data << std::endl;
std::terminate();
}

return 0;
}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-2679829776778;
09KKK
6778;
85e-q
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 74b13a8

Please sign in to comment.