json-archive/fuzz/fuzz_targets/fuzz_random_bytes.rs
nobody b65103c9f7
refactor: decompose archive read/write into composable building blocks
Delete archive_context.rs and archive_ops.rs (1200+ lines of duplicated
logic). Replace with four focused modules:

1. open_archive() - opens a file, detects compression, returns raw bytes
2. read_archive() - parses bytes into validated observations
3. CompressionWriter - writes bytes with any compression format
4. WriteStrategy - given a list of files, determines input archive,
 output archive, output format, and which of four write modes to use:
  - Create: new archive, no input
  - Append: uncompressed input, seek to end
  - AtomicSwap: compressed input, rewrite via temp file
  - CopyOnWrite: different input/output paths, transcode between formats

Previously you could not specify output format. Appending always
preserved the input format, creating compressed archives didn't work.
Now all four cases work with any supported compression format.

Atomic swap now writes to temp file, then renames. Crash-safe.

Trade-off: This approach prioritizes code clarity over syscall efficiency.
  The archive file may be opened and read multiple times during a single
  operation (once for format detection, once for reading state, once for
  copying content). A more optimized implementation could reuse file
  handles, but the current approach makes each step's purpose obvious.
2025-12-01 21:01:27 -08:00

23 lines
No EOL
907 B
Rust

#![no_main]
use libfuzzer_sys::fuzz_target;
use json_archive::{read_archive, ReadMode};
use std::io::{BufReader, Write};
use tempfile::NamedTempFile;
fuzz_target!(|data: &[u8]| {
// Write the random bytes to a temporary file
if let Ok(mut temp_file) = NamedTempFile::new() {
if temp_file.write_all(data).is_ok() {
// Try to read the file with both validation modes
for mode in [ReadMode::FullValidation, ReadMode::AppendSeek] {
if let Ok(file) = std::fs::File::open(temp_file.path()) {
let reader = BufReader::new(file);
// The read operation should never panic, regardless of input
// It should either succeed or return an error gracefully
let _ = read_archive(reader, &temp_file.path().display().to_string(), mode);
}
}
}
}
});