Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(473)

Side by Side Diff: net/base/fuzzed_data_provider.cc

Issue 1917503002: URLRequest fuzzer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fuzz
Patch Set: Response to comments Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/base/fuzzed_data_provider.h ('k') | net/data/http/http.dict » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "net/base/fuzzed_data_provider.h"
6
7 #include <algorithm>
8 #include <string>
9
10 #include "base/logging.h"
11
12 namespace net {
13
14 FuzzedDataProvider::FuzzedDataProvider(const uint8_t* data, size_t size)
15 : remaining_data_(reinterpret_cast<const char*>(data), size) {}
16
17 FuzzedDataProvider::~FuzzedDataProvider() {}
18
19 size_t FuzzedDataProvider::ConsumeBytes(char* dest, size_t bytes) {
20 size_t bytes_to_write = std::min(bytes, remaining_data_.length());
21 memcpy(dest, remaining_data_.data(), bytes_to_write);
22 remaining_data_ = remaining_data_.substr(bytes_to_write);
23 return bytes_to_write;
24 }
25
26 uint32_t FuzzedDataProvider::ConsumeBits(size_t num_bits) {
27 CHECK_NE(0u, num_bits);
28 CHECK_LE(num_bits, 32u);
29 uint32_t out = 0;
30 while (num_bits > 0) {
31 uint8_t new_bits = 0;
32 if (!remaining_data_.empty()) {
33 new_bits = remaining_data_.data()[remaining_data_.length() - 1];
34 remaining_data_ = remaining_data_.substr(0, remaining_data_.length() - 1);
35 }
36 size_t bits_to_add = 8;
37 if (num_bits > 8)
38 bits_to_add = num_bits;
39 new_bits &= new_bits & ((1 << bits_to_add) - 1);
40 out = (out << bits_to_add) | new_bits;
41 num_bits -= bits_to_add;
Charlie Harrison 2016/04/26 02:24:57 Drive-by: I think bits_to_add can be greater than
mmenke 2016/04/27 19:53:25 Oops, yea, you're right, good catch! I'd already
42 }
43
44 return out;
45 }
46
47 bool FuzzedDataProvider::ConsumeBool() {
48 return !ConsumeBits(1);
49 }
50
51 uint32_t FuzzedDataProvider::ConsumeValueInRange(uint32_t min, uint32_t max) {
52 CHECK_LE(min, max);
53
54 // This consumes bytes in multiples of 8, to try and get a more uniform
55 // distribution of values in the given range.
56
57 // Find the smallest multiple of 8 that will cover the range.
58 uint32_t range = max - min;
59 int needed_bits = 0;
60 while (range > 0) {
61 range >>= 8;
62 needed_bits++;
63 }
64
65 // Allow an empty range, unlike ConsumeBits.
66 if (needed_bits == 0)
67 return min;
68
69 return min + ConsumeBits(needed_bits) % (max - min);
70 }
71
72 } // namespace net
OLDNEW
« no previous file with comments | « net/base/fuzzed_data_provider.h ('k') | net/data/http/http.dict » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698