| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/test/fuzzed_data_provider.h" | 5 #include "base/test/fuzzed_data_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 offset += 8; | 47 offset += 8; |
| 48 } | 48 } |
| 49 | 49 |
| 50 // Avoid division by 0, in the case |range + 1| results in overflow. | 50 // Avoid division by 0, in the case |range + 1| results in overflow. |
| 51 if (range == std::numeric_limits<uint32_t>::max()) | 51 if (range == std::numeric_limits<uint32_t>::max()) |
| 52 return result; | 52 return result; |
| 53 | 53 |
| 54 return min + result % (range + 1); | 54 return min + result % (range + 1); |
| 55 } | 55 } |
| 56 | 56 |
| 57 std::string FuzzedDataProvider::ConsumeRandomLengthString(size_t max_length) { |
| 58 std::string out; |
| 59 for (size_t i = 0; i < max_length && !remaining_data_.empty(); ++i) { |
| 60 char next = remaining_data_[0]; |
| 61 remaining_data_.remove_prefix(1); |
| 62 if (next == '\\' && !remaining_data_.empty()) { |
| 63 next = remaining_data_[0]; |
| 64 remaining_data_.remove_prefix(1); |
| 65 if (next != '\\') |
| 66 return out; |
| 67 } |
| 68 out += next; |
| 69 } |
| 70 return out; |
| 71 } |
| 72 |
| 57 int FuzzedDataProvider::ConsumeInt32InRange(int min, int max) { | 73 int FuzzedDataProvider::ConsumeInt32InRange(int min, int max) { |
| 58 CHECK_LE(min, max); | 74 CHECK_LE(min, max); |
| 59 | 75 |
| 60 uint32_t range = max - min; | 76 uint32_t range = max - min; |
| 61 return min + ConsumeUint32InRange(0, range); | 77 return min + ConsumeUint32InRange(0, range); |
| 62 } | 78 } |
| 63 | 79 |
| 64 bool FuzzedDataProvider::ConsumeBool() { | 80 bool FuzzedDataProvider::ConsumeBool() { |
| 65 return (ConsumeUint8() & 0x01) == 0x01; | 81 return (ConsumeUint8() & 0x01) == 0x01; |
| 66 } | 82 } |
| 67 | 83 |
| 68 uint8_t FuzzedDataProvider::ConsumeUint8() { | 84 uint8_t FuzzedDataProvider::ConsumeUint8() { |
| 69 return ConsumeUint32InRange(0, 0xFF); | 85 return ConsumeUint32InRange(0, 0xFF); |
| 70 } | 86 } |
| 71 | 87 |
| 72 uint16_t FuzzedDataProvider::ConsumeUint16() { | 88 uint16_t FuzzedDataProvider::ConsumeUint16() { |
| 73 return ConsumeUint32InRange(0, 0xFFFF); | 89 return ConsumeUint32InRange(0, 0xFFFF); |
| 74 } | 90 } |
| 75 | 91 |
| 76 } // namespace base | 92 } // namespace base |
| OLD | NEW |