Lines Matching +full:max +full:- +full:size
1 //===- FuzzedDataProvider.h - Utility header for fuzz targets ---*- C++ -* ===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 //===----------------------------------------------------------------------===//
31 // https://github.com/google/fuzzing/blob/master/docs/split-inputs.md#fuzzed-data-provider
34 // |data| is an array of length |size| that the FuzzedDataProvider wraps to
36 FuzzedDataProvider(const uint8_t *data, size_t size)
37 : data_ptr_(data), remaining_bytes_(size) {}
45 // separate buffer (i.e. ASan would catch any under-/overflow) and the memory
53 // terminated C-string. Otherwise, prefer the methods returning std::vector.
61 template <typename T> T ConsumeIntegralInRange(T min, T max);
65 template <typename T> T ConsumeFloatingPointInRange(T min, T max);
76 template <typename T, size_t size> T PickValueInArray(const T (&array)[size]);
77 template <typename T, size_t size>
78 T PickValueInArray(const std::array<T, size> &array);
96 std::vector<T> ConsumeBytes(size_t size, size_t num_bytes);
115 // of the resulting vector. Useful, when a mutable null-terminated C-string is
135 // null-terminated C string. If fewer than |num_bytes| of data remain, returns
192 // Returns a number in the range [Type's min, Type's max]. The value might
197 std::numeric_limits<T>::max());
200 // Returns a number in the range [min, max] by consuming bytes from the
203 // be less than or equal to |max|.
205 T FuzzedDataProvider::ConsumeIntegralInRange(T min, T max) {
209 if (min > max)
213 uint64_t range = static_cast<uint64_t>(max) - static_cast<uint64_t>(min);
225 --remaining_bytes_;
231 if (range != std::numeric_limits<decltype(range)>::max())
237 // Returns a floating point value in the range [Type's lowest, Type's max] by
242 std::numeric_limits<T>::max());
247 // |min| must be less than or equal to |max|.
249 T FuzzedDataProvider::ConsumeFloatingPointInRange(T min, T max) {
250 if (min > max)
256 if (max > zero && min < zero && max > min + std::numeric_limits<T>::max()) {
257 // The diff |max - min| would overflow the given floating point type. Use
260 range = (max / 2.0) - (min / 2.0);
265 range = max - min;
284 result /= static_cast<T>(std::numeric_limits<IntegralType>::max());
302 // Returns a copy of the value selected from the given fixed-size |array|.
303 template <typename T, size_t size>
304 T FuzzedDataProvider::PickValueInArray(const T (&array)[size]) {
305 static_assert(size > 0, "The array must be non empty.");
306 return array[ConsumeIntegralInRange<size_t>(0, size - 1)];
309 template <typename T, size_t size>
310 T FuzzedDataProvider::PickValueInArray(const std::array<T, size> &array) {
311 static_assert(size > 0, "The array must be non empty.");
312 return array[ConsumeIntegralInRange<size_t>(0, size - 1)];
318 if (!list.size())
321 return *(list.begin() + ConsumeIntegralInRange<size_t>(0, list.size() - 1));
349 remaining_bytes_ -= num_bytes;
353 std::vector<T> FuzzedDataProvider::ConsumeBytes(size_t size, size_t num_bytes) {
356 // The point of using the size-based constructor below is to increase the
362 std::vector<T> result(size);
363 if (size == 0) {
388 // Avoid using implementation-defined unsigned to signed conversions.
390 if (value <= std::numeric_limits<TS>::max()) {
394 return TS_min + static_cast<TS>(value - TS_min);