Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
fuzz_csv_utility.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 https://github.com/lrleon/Aleph-w
6
7 This file is part of Aleph-w library
8
9 Copyright (c) 2002-2026 Leandro Rabindranath Leon
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28*/
29
35#include <algorithm>
36#include <cstddef>
37#include <cstdint>
38#include <cstdlib>
39#include <sstream>
40#include <string>
41
42#include <parse-csv.H>
43
51extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
52{
53 size = std::min(size, size_t{512});
54 const char delimiters[] = {',', ';', '\t'};
55 const char delimiter = delimiters[size == 0 ? 0 : data[0] % 3];
56 const char *bytes = size == 0 ? "" : reinterpret_cast<const char *>(data);
57 const std::string input(bytes, size);
58
59 std::istringstream arbitrary(input);
60 (void) Aleph::csv_read_row(arbitrary, delimiter);
61
63 size_t cursor = size == 0 ? 0 : 1;
64 const size_t field_count = size < 2 ? 1 : 1 + data[1] % 8;
65 if (size >= 2)
66 cursor = 2;
67
68 for (size_t i = 0; i < field_count; ++i)
69 {
70 const size_t length = cursor < size ? data[cursor++] % 33 : 0;
71 const size_t available = std::min(length, size - cursor);
72 fields.append(std::string(bytes + cursor, available));
73 cursor += available;
74 }
75
76 std::ostringstream output;
77 Aleph::csv_write_row(output, fields, delimiter);
78 std::istringstream written(output.str());
79 const auto parsed = Aleph::csv_read_row(written, delimiter);
80 if (parsed.size() != fields.size())
81 std::abort();
82 for (size_t i = 0; i < fields.size(); ++i)
83 if (parsed(i) != fields(i))
84 std::abort();
85
86 return 0;
87}
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
Parse arbitrary CSV and round-trip generated fields through the writer.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
void csv_write_row(std::ostream &out, const Array< std::string > &row, char delimiter=',', const std::string &line_ending="\n")
Write a CSV row to an output stream.
Definition parse-csv.H:377
Array< std::string > csv_read_row(std::istream &in, char delimiter=',')
Read a single CSV row from an input stream.
Definition parse-csv.H:159
Comprehensive CSV (Comma-Separated Values) parsing and manipulation utilities.
ofstream output
Definition writeHeap.C:215