Copilot commented on code in PR #54912:
URL: https://github.com/apache/arrow/pull/54912#discussion_r4679003875
##########
cpp/src/arrow/csv/generate_fuzz_corpus.cc:
##########
@@ -152,6 +159,99 @@ Result<RecordBatchVector> Batches() {
return batches;
}
+namespace {
+
+// Default fuzz header values, matching the default CSV reader options
+// (see ParseOptions, ReadOptions and ConvertOptions in csv/options.h).
+internal::FuzzCsvHeader MakeDefaultFuzzCsvHeader() {
+ internal::FuzzCsvHeader header{};
+ internal::SetFuzzCsvMagic(header);
+ // --- ParseOptions defaults ---
+ header.parse_1.delimiter_id = 0; // ','
+ header.parse_1.quoting = 1;
+ header.parse_1.quote_char_id = 0; // '"'
+ header.parse_1.double_quote = 1;
+ header.parse_1.escaping = 0;
+ header.parse_1.escape_char_id = 0; // '\\'
+ header.parse_2.newlines_in_values = 0;
+ header.parse_2.ignore_empty_lines = 1;
+ header.parse_2.pad_short_rows = 0;
+ header.parse_2.ignore_extra_columns = 0;
+ // --- ReadOptions defaults ---
+ // Block size and threading are not part of the header: they do not affect
+ // the CSV bytes and the fuzz target varies them itself.
+ header.read.skip_rows = 0;
+ header.read.autogenerate_column_names = 0;
+ // --- ConvertOptions defaults ---
+ header.convert_1.strings_can_be_null = 0;
+ header.convert_1.quoted_strings_can_be_null = 1;
+ header.convert_1.auto_dict_encode = 0;
+ header.convert_1.auto_dict_max_cardinality_id = 0; // 50
+ header.convert_1.decimal_point_is_comma = 0; // '.'
+ header.convert_1.check_utf8 = 1;
+ return header;
+}
+
+// Index of `delimiter` in the candidate delimiters of the fuzz header
+uint8_t FuzzDelimiterId(std::string_view delimiter) {
+ auto it = std::find(internal::kFuzzCsvDelimiters.begin(),
+ internal::kFuzzCsvDelimiters.end(), delimiter);
+ return static_cast<uint8_t>(it - internal::kFuzzCsvDelimiters.begin());
+}
+// Build the fuzz header for a set of write options: the parse and convert
+// fields mirror the dialect that the writer will produce, and the read-side
+// fields keep their defaults (block size and threading are varied by the
+// fuzz target itself).
+internal::FuzzCsvHeader MakeFuzzCsvHeaderFromWriteOptions(
+ const WriteOptions& o) {
+ internal::FuzzCsvHeader header = MakeDefaultFuzzCsvHeader();
+ header.parse_1.delimiter_id = FuzzDelimiterId(std::string(1, o.delimiter));
+ if (!o.include_header) {
+ // No header row was written, so the reader must autogenerate the
+ // column names it will read back.
+ header.read.autogenerate_column_names = 1;
+ }
+ if (!o.null_string.empty()) {
+ // The reader must parse the written null spelling as nulls.
+ header.convert_1.strings_can_be_null = 1;
+ }
+ return header;
+}
+
+using WriteAndHeaderOptions = std::pair<WriteOptions, internal::FuzzCsvHeader>;
+
+// Generate several dialect variants of each batch, matching the option
+// rotations performed by the fuzz target (see csv/fuzz.cc). For each
+// variant, also compute the fuzz header encoding the reader options that
+// match the writing dialect.
+std::vector<WriteAndHeaderOptions> SampleVariants() {
+ std::vector<WriteAndHeaderOptions> variants;
+ auto append_variant = [&](std::function<void(WriteOptions&)> write_fn) {
+ auto write_options = WriteOptions::Defaults();
+ write_fn(write_options);
+ // An invalid set of write options is a bug in this generator, not
+ // something to skip silently.
+ ARROW_CHECK_OK(write_options.Validate());
+ variants.emplace_back(std::move(write_options),
+ MakeFuzzCsvHeaderFromWriteOptions(write_options));
Review Comment:
`variants.emplace_back(std::move(write_options),
MakeFuzzCsvHeaderFromWriteOptions(write_options))` can read from a moved-from
`write_options` because argument evaluation order is not guaranteed. Compute
the header before moving (e.g., create `header` from `write_options`, then
`emplace_back(std::move(write_options), header)`).
##########
cpp/src/arrow/csv/fuzz.cc:
##########
@@ -15,106 +15,220 @@
// specific language governing permissions and limitations
// under the License.
+#include <array>
#include <cstdint>
-#include <functional>
#include <memory>
-#include <optional>
+#include <span>
+#include <sstream>
#include "arrow/buffer.h"
+#include "arrow/compare.h"
+#include "arrow/csv/fuzz_header_internal.h"
#include "arrow/csv/reader.h"
#include "arrow/io/interfaces.h"
#include "arrow/io/memory.h"
#include "arrow/status.h"
#include "arrow/table.h"
#include "arrow/util/fuzz_internal.h"
-#include "arrow/util/macros.h"
+#include "arrow/util/logging.h"
#include "arrow/util/thread_pool.h"
+#include "arrow/util/value_parsing.h"
namespace arrow::csv {
using ::arrow::internal::GetCpuThreadPool;
using ::arrow::io::InputStream;
-Status FuzzCsvReader(const uint8_t* data, int64_t size) {
- // Since the Fuzz-allocated data is not owned, any task that outlives the
TableReader
- // may try to read memory that has been deallocated. Hence we wait for all
pending
- // tasks to end before leaving.
- struct TaskGuard {
- ~TaskGuard() { GetCpuThreadPool()->WaitForIdle(); }
- };
+namespace {
+
+struct FuzzCsvOptions {
+ ReadOptions read;
+ ParseOptions parse;
+ ConvertOptions convert;
+};
+
+FuzzCsvOptions MakeFuzzCsvOptions(const internal::FuzzCsvHeader& header) {
+ FuzzCsvOptions opts;
+
+ // --- ParseOptions ---
+ // The "id" fields store an index in the corresponding candidate arrays,
+ // modulo the array length (see csv/fuzz_header_internal.h).
+ opts.parse.delimiter_string =
+ std::string(internal::kFuzzCsvDelimiters[header.parse_1.delimiter_id %
+
internal::kFuzzCsvDelimiters.size()]);
+ opts.parse.quoting = header.parse_1.quoting;
+ opts.parse.quote_char =
+ internal::kFuzzCsvQuoteChars[header.parse_1.quote_char_id %
+ internal::kFuzzCsvQuoteChars.size()];
+ opts.parse.double_quote = header.parse_1.double_quote;
+ opts.parse.escaping = header.parse_1.escaping;
+ opts.parse.escape_char =
+ internal::kFuzzCsvEscapeChars[header.parse_1.escape_char_id %
+ internal::kFuzzCsvEscapeChars.size()];
+ opts.parse.newlines_in_values = header.parse_2.newlines_in_values;
+ opts.parse.ignore_empty_lines = header.parse_2.ignore_empty_lines;
+ opts.parse.pad_short_rows = header.parse_2.pad_short_rows;
+ opts.parse.ignore_extra_columns = header.parse_2.ignore_extra_columns;
+
+ // --- ReadOptions ---
+ // Block size and threading are deliberately not encoded in the header:
+ // they are read-side knobs that do not affect the CSV bytes being read,
+ // and the target varies them itself in ReadAllWays() below.
+ opts.read.skip_rows = header.read.skip_rows;
+ opts.read.autogenerate_column_names = header.read.autogenerate_column_names;
Review Comment:
`opts.parse` and `opts.read` are populated without first initializing from
`ParseOptions::Defaults()` / `ReadOptions::Defaults()`. This makes behavior
depend on the structs' default constructors and can unintentionally leave newly
added fields at non-default values in the future. Recommend starting with
`Defaults()` for both and then overriding the fields driven by the header
(similar to how `ConvertOptions::Defaults()` is used).
##########
cpp/build-support/fuzzing/pack_corpus.py:
##########
@@ -36,9 +36,9 @@ def process_dir(corpus_dir, zip_output):
data = f.read()
arcname = hashlib.sha1(data).hexdigest()
if arcname in seen_hashes:
- raise ValueError(
- f"Duplicate hash: {arcname} (in file {child}), "
- f"already seen in file {seen_hashes[arcname]}")
+ print(f" WARNING: {child} has the same content as
{seen_hashes[arcname]}, "
+ "skipping it")
Review Comment:
This warning is printed to stdout; for build tooling it is often preferable
to write warnings to stderr so stdout remains machine-consumable. Consider
`print(..., file=sys.stderr)` (and importing `sys`) for the warning path.
##########
cpp/src/arrow/csv/generate_fuzz_corpus.cc:
##########
@@ -152,6 +159,99 @@ Result<RecordBatchVector> Batches() {
return batches;
}
+namespace {
+
+// Default fuzz header values, matching the default CSV reader options
+// (see ParseOptions, ReadOptions and ConvertOptions in csv/options.h).
+internal::FuzzCsvHeader MakeDefaultFuzzCsvHeader() {
+ internal::FuzzCsvHeader header{};
+ internal::SetFuzzCsvMagic(header);
+ // --- ParseOptions defaults ---
+ header.parse_1.delimiter_id = 0; // ','
+ header.parse_1.quoting = 1;
+ header.parse_1.quote_char_id = 0; // '"'
+ header.parse_1.double_quote = 1;
+ header.parse_1.escaping = 0;
+ header.parse_1.escape_char_id = 0; // '\\'
+ header.parse_2.newlines_in_values = 0;
+ header.parse_2.ignore_empty_lines = 1;
+ header.parse_2.pad_short_rows = 0;
+ header.parse_2.ignore_extra_columns = 0;
+ // --- ReadOptions defaults ---
+ // Block size and threading are not part of the header: they do not affect
+ // the CSV bytes and the fuzz target varies them itself.
+ header.read.skip_rows = 0;
+ header.read.autogenerate_column_names = 0;
+ // --- ConvertOptions defaults ---
+ header.convert_1.strings_can_be_null = 0;
+ header.convert_1.quoted_strings_can_be_null = 1;
+ header.convert_1.auto_dict_encode = 0;
+ header.convert_1.auto_dict_max_cardinality_id = 0; // 50
+ header.convert_1.decimal_point_is_comma = 0; // '.'
+ header.convert_1.check_utf8 = 1;
+ return header;
+}
+
+// Index of `delimiter` in the candidate delimiters of the fuzz header
+uint8_t FuzzDelimiterId(std::string_view delimiter) {
+ auto it = std::find(internal::kFuzzCsvDelimiters.begin(),
+ internal::kFuzzCsvDelimiters.end(), delimiter);
Review Comment:
If `delimiter` is not present in `kFuzzCsvDelimiters`, this returns an
out-of-range index which later maps (via modulo) to a different delimiter than
intended, silently producing a mismatched header. Consider asserting/checking
`it != end()` (generator bug) or explicitly mapping to a known fallback, so
future added variants cannot accidentally desync the header from the written
dialect.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]