Fuzz.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. #include "fuzz/Fuzz.h"
  8. #include "fuzz/FuzzCommon.h"
  9. // UBSAN reminds us that bool can only legally hold 0 or 1.
  10. void Fuzz::next(bool* b) {
  11. uint8_t n;
  12. this->next(&n);
  13. *b = (n & 1) == 1;
  14. }
  15. void Fuzz::next(SkImageFilter::CropRect* cropRect) {
  16. SkRect rect;
  17. uint8_t flags;
  18. this->next(&rect);
  19. this->nextRange(&flags, 0, 0xF);
  20. *cropRect = SkImageFilter::CropRect(rect, flags);
  21. }
  22. void Fuzz::nextBytes(void* n, size_t size) {
  23. if ((fNextByte + size) > fBytes->size()) {
  24. sk_bzero(n, size);
  25. memcpy(n, fBytes->bytes() + fNextByte, fBytes->size() - fNextByte);
  26. fNextByte = fBytes->size();
  27. return;
  28. }
  29. memcpy(n, fBytes->bytes() + fNextByte, size);
  30. fNextByte += size;
  31. }
  32. void Fuzz::next(SkRegion* region) {
  33. // See FuzzCommon.h
  34. FuzzNiceRegion(this, region, 10);
  35. }
  36. void Fuzz::nextRange(float* f, float min, float max) {
  37. this->next(f);
  38. if (!std::isnormal(*f) && *f != 0.0f) {
  39. // Don't deal with infinity or other strange floats.
  40. *f = max;
  41. }
  42. *f = min + std::fmod(std::abs(*f), (max - min + 1));
  43. }