Skip to content

Commit

Permalink
stop high memory use when fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldreik committed Apr 21, 2019
1 parent 5efb24d commit 99b2e08
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,12 @@ void sprintf_format(Double value, internal::buffer<char>& buf,
*format_ptr++ = type;
*format_ptr = '\0';

#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
if(spec.precision>100000) {
throw std::runtime_error("fuzz mode - avoiding large precision");
}
#endif

// Format using snprintf.
char* start = FMT_NULL;
for (;;) {
Expand Down
5 changes: 5 additions & 0 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,11 @@ class basic_memory_buffer : private Allocator, public internal::buffer<T> {

template <typename T, std::size_t SIZE, typename Allocator>
void basic_memory_buffer<T, SIZE, Allocator>::grow(std::size_t size) {
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
if(size>10000000) {
throw std::runtime_error("fuzz mode - won't grow that much");
}
#endif
std::size_t old_capacity = this->capacity();
std::size_t new_capacity = old_capacity + old_capacity / 2;
if (size > new_capacity) new_capacity = size;
Expand Down

0 comments on commit 99b2e08

Please sign in to comment.