From 6d0498df2445975d8cfff3cb8c4b1453bd5e00e6 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Tue, 14 Jun 2022 16:22:59 +0200 Subject: [PATCH] Fix the fuzz tests --- milli/fuzz/fuzz_targets/indexing.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/milli/fuzz/fuzz_targets/indexing.rs b/milli/fuzz/fuzz_targets/indexing.rs index b618aabad..5c3b79ed7 100644 --- a/milli/fuzz/fuzz_targets/indexing.rs +++ b/milli/fuzz/fuzz_targets/indexing.rs @@ -7,10 +7,10 @@ use anyhow::{bail, Result}; use arbitrary_json::ArbitraryValue; use heed::EnvOpenOptions; use libfuzzer_sys::fuzz_target; -use milli::documents::{DocumentBatchBuilder, DocumentBatchReader}; +use milli::documents::{DocumentsBatchBuilder, DocumentsBatchReader}; use milli::update::{IndexDocuments, IndexDocumentsConfig, IndexerConfig, Settings}; use milli::Index; -use serde_json::Value; +use serde_json::{Map, Value}; #[cfg(target_os = "linux")] #[global_allocator] @@ -19,21 +19,26 @@ static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; /// reads json from input and write an obkv batch to writer. pub fn read_json(input: impl Read, writer: impl Write + Seek) -> Result { let writer = BufWriter::new(writer); - let mut builder = DocumentBatchBuilder::new(writer)?; - builder.extend_from_json(input)?; + let mut builder = DocumentsBatchBuilder::new(writer); - if builder.len() == 0 { + let values: Vec> = serde_json::from_reader(input)?; + if builder.documents_count() == 0 { bail!("Empty payload"); } - let count = builder.finish()?; + for object in values { + builder.append_json_object(&object)?; + } - Ok(count) + let count = builder.documents_count(); + let vector = builder.into_inner()?; + + Ok(count as usize) } fn index_documents( index: &mut milli::Index, - documents: DocumentBatchReader>>, + documents: DocumentsBatchReader>>, ) -> Result<()> { let config = IndexerConfig::default(); let mut wtxn = index.write_txn()?; @@ -98,7 +103,7 @@ fuzz_target!(|batches: Vec>| { // We ignore all malformed documents if let Ok(_) = read_json(json.as_bytes(), &mut documents) { documents.rewind().unwrap(); - let documents = DocumentBatchReader::from_reader(documents).unwrap(); + let documents = DocumentsBatchReader::from_reader(documents).unwrap(); // A lot of errors can come out of milli and we don't know which ones are normal or not // so we are only going to look for the unexpected panics. let _ = index_documents(&mut index, documents);