Kevin Lubick | bc9a1a8 | 2018-09-17 14:46:57 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "fuzz/Fuzz.h" |
| 10 | #include "fuzz/FuzzCommon.h" |
Kevin Lubick | bc9a1a8 | 2018-09-17 14:46:57 -0400 | [diff] [blame] | 11 | |
| 12 | // UBSAN reminds us that bool can only legally hold 0 or 1. |
| 13 | void Fuzz::next(bool* b) { |
| 14 | uint8_t n; |
| 15 | this->next(&n); |
| 16 | *b = (n & 1) == 1; |
| 17 | } |
| 18 | |
Hal Canary | 6d9a51a | 2018-11-19 13:15:21 -0500 | [diff] [blame] | 19 | void Fuzz::nextBytes(void* n, size_t size) { |
| 20 | if ((fNextByte + size) > fBytes->size()) { |
| 21 | sk_bzero(n, size); |
| 22 | memcpy(n, fBytes->bytes() + fNextByte, fBytes->size() - fNextByte); |
| 23 | fNextByte = fBytes->size(); |
| 24 | return; |
| 25 | } |
| 26 | memcpy(n, fBytes->bytes() + fNextByte, size); |
| 27 | fNextByte += size; |
| 28 | } |
| 29 | |
Kevin Lubick | bc9a1a8 | 2018-09-17 14:46:57 -0400 | [diff] [blame] | 30 | void Fuzz::next(SkRegion* region) { |
| 31 | // See FuzzCommon.h |
| 32 | FuzzNiceRegion(this, region, 10); |
| 33 | } |
| 34 | |
| 35 | void Fuzz::nextRange(float* f, float min, float max) { |
| 36 | this->next(f); |
| 37 | if (!std::isnormal(*f) && *f != 0.0f) { |
| 38 | // Don't deal with infinity or other strange floats. |
| 39 | *f = max; |
| 40 | } |
| 41 | *f = min + std::fmod(std::abs(*f), (max - min + 1)); |
| 42 | } |