Skip to content

Commit ea835af

Browse files
committed
refactoring fuzzer and sql_doc structures
1 parent 9986bec commit ea835af

File tree

3 files changed

+37
-78
lines changed

3 files changed

+37
-78
lines changed

fuzz/fuzz_targets/sql_docs.rs

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,7 @@ use libfuzzer_sys::fuzz_target;
44
use std::str;
55

66
use sql_docs::SqlDoc;
7-
use std::{fs, path::PathBuf};
87

9-
fuzz_target!(|data: &[u8]| {
10-
const MAX_FILES: usize = 16;
11-
const MAX_FILE_BYTES: usize = 8 * 1024;
12-
13-
if data.is_empty() {
14-
return;
15-
}
16-
17-
let tmp = match tempfile::tempdir() {
18-
Ok(t) => t,
19-
Err(_) => return,
20-
};
21-
let root = tmp.path();
22-
23-
let mut i = 0usize;
24-
let file_count = (data[i] as usize % MAX_FILES).max(1);
25-
i += 1;
26-
27-
for file_idx in 0..file_count {
28-
if i >= data.len() {
29-
break;
30-
}
31-
32-
let name_len = (data[i] as usize % 24).max(1);
33-
i += 1;
34-
if i + name_len > data.len() {
35-
break;
36-
}
37-
38-
let name_bytes = &data[i..i + name_len];
39-
i += name_len;
40-
41-
let mut name = String::with_capacity(name_len);
42-
for &b in name_bytes {
43-
name.push(match b {
44-
b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' => b as char,
45-
_ => '_',
46-
});
47-
}
48-
49-
let ext = if (file_idx + data[0] as usize) % 3 == 0 { "sql" } else { "txt" };
50-
let path = root.join(format!("{name}_{file_idx}.{ext}"));
51-
52-
if i + 2 > data.len() {
53-
break;
54-
}
55-
let raw_len = u16::from_le_bytes([data[i], data[i + 1]]) as usize;
56-
i += 2;
57-
58-
let content_len = raw_len % MAX_FILE_BYTES;
59-
if i + content_len > data.len() {
60-
break;
61-
}
62-
63-
let content = &data[i..i + content_len];
64-
i += content_len;
65-
66-
let text = String::from_utf8_lossy(content);
67-
let _ = fs::write(&path, text.as_bytes());
68-
}
69-
let res = SqlDoc::from_dir(root).build();
70-
if let Err(e) = res {
71-
let _ = e.to_string();
72-
let _ = std::error::Error::source(&e);
73-
}
8+
fuzz_target!(|data: String| {
9+
7410
});

src/docs.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Module for parsing sql and comments and returning `table` and `column`
22
//! information, including comments
33
use core::fmt;
4+
use std::path::PathBuf;
45

56
use sqlparser::ast::{Ident, ObjectName, ObjectNamePart, Spanned, Statement};
67

@@ -65,6 +66,7 @@ pub struct TableDoc {
6566
name: String,
6667
doc: Option<String>,
6768
columns: Vec<ColumnDoc>,
69+
file: Option<PathBuf>,
6870
}
6971

7072
impl TableDoc {
@@ -223,6 +225,25 @@ impl SqlFileDoc {
223225
pub fn tables(&self) -> &[TableDoc] {
224226
&self.tables
225227
}
228+
/// Returns the number fo tables in the `SqlFileDoc`
229+
#[must_use]
230+
pub fn number_of_tables(&self) -> usize {
231+
self.tables().len()
232+
}
233+
}
234+
235+
impl From<SqlFileDoc> for Vec<TableDoc> {
236+
fn from(value: SqlFileDoc) -> Self {
237+
value.tables
238+
}
239+
}
240+
241+
impl IntoIterator for SqlFileDoc {
242+
type Item = TableDoc;
243+
type IntoIter = <Vec<TableDoc> as IntoIterator>::IntoIter;
244+
fn into_iter(self) -> Self::IntoIter {
245+
self.tables.into_iter()
246+
}
226247
}
227248

228249
/// Helper function that will parse the table's schema and table name.

src/sql_doc.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ pub struct SqlDoc {
2121

2222
/// Builder structure for the [`SqlDoc`]
2323
#[derive(Debug, Eq, PartialEq)]
24-
pub struct SqlDocBuilder {
24+
pub struct SqlDocBuilder<'a> {
2525
/// The source for implementing the [`SqlDoc`] to be built
26-
source: SqlFileDocSource,
26+
source: SqlFileDocSource<'a>,
2727
/// The list of Paths to be ignored for parsing purposes.
2828
deny: Vec<String>,
2929
/// Used to indicate maintaining the `[(PathBuf, SqlFileDoc)]`
@@ -45,9 +45,10 @@ pub enum MultiFlatten {
4545

4646
/// Enum for specifying a file doc source as a `directory` or a specific `file`
4747
#[derive(Debug, Eq, PartialEq)]
48-
enum SqlFileDocSource {
48+
enum SqlFileDocSource<'a>{
4949
Dir(PathBuf),
5050
File(PathBuf),
51+
FromString(&'a str),
5152
}
5253

5354
impl SqlDoc {
@@ -57,7 +58,7 @@ impl SqlDoc {
5758
Self { tables, files }
5859
}
5960
/// Method for generating builder from a directory.
60-
pub fn from_dir<P: AsRef<Path>>(root: P) -> SqlDocBuilder {
61+
pub fn from_dir<P: AsRef<Path> + ?Sized>(root: &P) -> SqlDocBuilder<'_> {
6162
SqlDocBuilder {
6263
source: SqlFileDocSource::Dir(root.as_ref().to_path_buf()),
6364
deny: Vec::new(),
@@ -66,7 +67,7 @@ impl SqlDoc {
6667
}
6768
}
6869
/// Method for generating builder from a [`Path`] of a single file
69-
pub fn from_path<P: AsRef<Path>>(path: P) -> SqlDocBuilder {
70+
pub fn from_path<P: AsRef<Path> + ?Sized>(path: &P) -> SqlDocBuilder<'_> {
7071
SqlDocBuilder {
7172
source: SqlFileDocSource::File(path.as_ref().to_path_buf()),
7273
deny: Vec::new(),
@@ -144,7 +145,7 @@ impl SqlDoc {
144145
}
145146
}
146147

147-
impl SqlDocBuilder {
148+
impl SqlDocBuilder<'_> {
148149
/// Method for adding an item to the deny list
149150
///
150151
/// # Parameters
@@ -181,7 +182,6 @@ impl SqlDocBuilder {
181182
self.multiline_flat = MultiFlatten::NoFlat;
182183
self
183184
}
184-
185185
/// Builds the [`SqlDoc`]
186186
///
187187
/// # Errors
@@ -193,18 +193,20 @@ impl SqlDocBuilder {
193193
let gen_files = generate_docs_from_file(file)?;
194194
let (path, sql_doc) = gen_files;
195195
vec![(path, sql_doc)]
196-
}
196+
},
197+
SqlFileDocSource::FromString(content) => {todo!()}
197198
};
198-
let mut tables = Vec::new();
199+
let num_of_tables = docs.iter().map(|(_,s)| s.number_of_tables()).sum();
200+
let mut tables = Vec::with_capacity(num_of_tables);
199201
let mut sql_doc = if self.retain_files {
200202
let files = docs;
201203
for (_, sql_doc) in &files {
202-
tables.extend(sql_doc.tables().iter().cloned());
204+
tables.extend(sql_doc.clone());
203205
}
204206
SqlDoc { tables, files: Some(files) }
205207
} else {
206-
for (_, sql_doc) in &docs {
207-
tables.extend(sql_doc.tables().iter().cloned());
208+
for (_, sql_doc) in docs {
209+
tables.extend(sql_doc);
208210
}
209211
SqlDoc { tables, files: None }
210212
};

0 commit comments

Comments
 (0)