[NFC][Py Reformat] Reformat python files in llvm
[llvm-project.git] / libc / fuzzing / stdlib / strtointeger_fuzz.cpp
blob197bee01d3a3ae5609e9d03dc4b133a05202d933
1 //===-- strtointeger_fuzz.cpp ---------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// Fuzzing test for llvm-libc atof implementation.
10 ///
11 //===----------------------------------------------------------------------===//
12 #include "src/stdlib/atoi.h"
13 #include "src/stdlib/atol.h"
14 #include "src/stdlib/atoll.h"
15 #include "src/stdlib/strtol.h"
16 #include "src/stdlib/strtoll.h"
17 #include "src/stdlib/strtoul.h"
18 #include "src/stdlib/strtoull.h"
19 #include <stddef.h>
20 #include <stdint.h>
22 // This takes the randomized bytes in data and interprets the first byte as the
23 // base for the string to integer conversion and the rest of them as a string to
24 // be passed to the string to integer conversion.
25 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
26 size_t container_size = 0;
27 if (size == 0) {
28 container_size = 1;
29 } else {
30 container_size = size;
32 uint8_t *container = new uint8_t[container_size];
33 if (!container)
34 __builtin_trap();
36 int base = 0;
37 if (size > 0) {
38 base = data[0] % 36;
39 base = base + ((base == 0) ? 0 : 1);
41 for (size_t i = 1; i < size; ++i) {
42 container[i - 1] = data[i];
45 container[container_size - 1] = '\0'; // Add null terminator to container.
47 const char *str_ptr = reinterpret_cast<const char *>(container);
49 char *out_ptr = nullptr;
51 auto volatile atoi_output = __llvm_libc::atoi(str_ptr);
52 auto volatile atol_output = __llvm_libc::atol(str_ptr);
53 auto volatile atoll_output = __llvm_libc::atoll(str_ptr);
54 auto volatile strtol_output = __llvm_libc::strtol(str_ptr, &out_ptr, base);
55 if (str_ptr + container_size - 1 < out_ptr)
56 __builtin_trap();
57 auto volatile strtoll_output = __llvm_libc::strtoll(str_ptr, &out_ptr, base);
58 if (str_ptr + container_size - 1 < out_ptr)
59 __builtin_trap();
60 auto volatile strtoul_output = __llvm_libc::strtoul(str_ptr, &out_ptr, base);
61 if (str_ptr + container_size - 1 < out_ptr)
62 __builtin_trap();
63 auto volatile strtoull_output =
64 __llvm_libc::strtoull(str_ptr, &out_ptr, base);
65 if (str_ptr + container_size - 1 < out_ptr)
66 __builtin_trap();
68 // If atoi is non-zero and the base is at least 10
69 if (atoi_output != 0 && base >= 10) {
70 // Then all of the other functions should output non-zero values as well.
71 // This is a trivial check meant to silence the "unused variable" warnings.
72 if (atol_output == 0 || atoll_output == 0 || strtol_output == 0 ||
73 strtoll_output == 0 || strtoul_output == 0 || strtoull_output == 0) {
74 __builtin_trap();
78 delete[] container;
79 return 0;