Skip to content

Commit dd1c6e1

Browse files
committed
Add fuzzing crash reproducer
1 parent 7bad6b9 commit dd1c6e1

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

fuzzing/old_crashes/fuzz_snprintf/crash-da39a3ee5e6b4b0d3255bfef95601890afd80709

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0000000000000000000000000000000000000000

test/test_snprintf.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,38 @@ void test_bootstrap()
170170
}
171171
}
172172

173+
void test_fuzzer_crash()
174+
{
175+
auto c_data = reinterpret_cast<const char*>("");
176+
auto size = std::strlen(c_data);
177+
178+
const auto formats = std::array<boost::decimal::chars_format, 4>{boost::decimal::chars_format::general,
179+
boost::decimal::chars_format::fixed,
180+
boost::decimal::chars_format::scientific,
181+
boost::decimal::chars_format::hex};
182+
183+
const auto dec32_printf_formats = std::array<const char*, 4>{"%Hg", "%Hf", "%He", "%Ha"};
184+
const auto dec64_printf_formats = std::array<const char*, 4>{"%Dg", "%Df", "%De", "%Da"};
185+
const auto dec128_printf_formats = std::array<const char*, 4>{"%DDg", "%DDf", "%DDe", "%DDa"};
186+
187+
for (std::size_t i {}; i < 4; ++i)
188+
{
189+
char buffer[20]; // Small enough it should overflow sometimes
190+
191+
boost::decimal::decimal32 f_val {};
192+
boost::decimal::from_chars(c_data, c_data + size, f_val, formats[i]);
193+
boost::decimal::snprintf(buffer, sizeof(buffer), dec32_printf_formats[i], f_val);
194+
195+
boost::decimal::decimal64 val {};
196+
boost::decimal::from_chars(c_data, c_data + size, val, formats[i]);
197+
boost::decimal::snprintf(buffer, sizeof(buffer), dec64_printf_formats[i], val);
198+
199+
boost::decimal::decimal128 ld_val {};
200+
boost::decimal::from_chars(c_data, c_data + size, ld_val, formats[i]);
201+
boost::decimal::snprintf(buffer, sizeof(buffer), dec128_printf_formats[i], ld_val);
202+
}
203+
}
204+
173205
int main()
174206
{
175207
test_bootstrap<decimal32>();
@@ -181,5 +213,7 @@ int main()
181213
test_locales();
182214
#endif
183215

216+
test_fuzzer_crash();
217+
184218
return boost::report_errors();
185219
}

0 commit comments

Comments
 (0)