1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53

base / time / time_fuzzer.cc [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <fuzzer/FuzzedDataProvider.h>
#include <stddef.h>
#include <stdint.h>

#include <string>
#include <tuple>

#include "base/test/icu_test_util.h"
#include "base/time/time.h"

namespace {

void FuzzStringConversions(const uint8_t* data, size_t size) {
  FuzzedDataProvider provider(data, size);
  std::string str = provider.ConsumeRemainingBytesAsString();
  base::Time dummy;
  std::ignore = base::Time::FromString(str.c_str(), &dummy);
  std::ignore = base::Time::FromUTCString(str.c_str(), &dummy);
}

void FuzzExplodedConversions(const uint8_t* data, size_t size) {
  FuzzedDataProvider provider(data, size);
  base::Time::Exploded exploded = {
      .year = provider.ConsumeIntegral<int>(),
      .month = provider.ConsumeIntegral<int>(),
      .day_of_week = provider.ConsumeIntegral<int>(),
      .day_of_month = provider.ConsumeIntegral<int>(),
      .hour = provider.ConsumeIntegral<int>(),
      .minute = provider.ConsumeIntegral<int>(),
      .second = provider.ConsumeIntegral<int>(),
      .millisecond = provider.ConsumeIntegral<int>(),
  };

  base::Time dummy;
  std::ignore = base::Time::FromUTCExploded(exploded, &dummy);
  std::ignore = base::Time::FromLocalExploded(exploded, &dummy);
}

}  // namespace

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  // Fuzz with a constant time zone to make reproduction of bugs easier.
  base::test::ScopedRestoreDefaultTimezone test_tz("America/Los_Angeles");

  FuzzStringConversions(data, size);
  FuzzExplodedConversions(data, size);

  return 0;
}