mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-19 05:50:57 +01:00
Bound SBS input bytes, wire counts and stored warnings (#237)
* formats: bound SBS input, wires and stored warnings * formats: format the SBS size check
This commit is contained in:
1 parent
6f5812f8ef
commit
703d3b1cf4
4 files changed
+50
-5
No files matched your search
@@ -9,6 +9,12 @@ pub(super) const UPDATER_VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
pub const SBS_EXT: &str = "sbs";
|
||||
pub(super) const MAX_WARNINGS: usize = 600;
|
||||
|
||||
/// Largest SBS document read (audit: MaterialCraft #2).
|
||||
pub(super) const MAX_SBS_BYTES: u64 = 256 << 20;
|
||||
|
||||
/// Most wires one SBS may carry, across all graphs (audit: MaterialCraft #2).
|
||||
pub(super) const MAX_WIRES: usize = 1_000_000;
|
||||
pub(super) const SNIFF_BYTES: usize = 4096;
|
||||
pub(super) const SELF_DEPENDENCY: &str = "?himself";
|
||||
pub(super) const FIRST_UID: u64 = 1_000_000_000;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::{
|
||||
constant::{MAX_GRAPHS, MAX_XML_NODES, SNIFF_BYTES},
|
||||
constant::{MAX_GRAPHS, MAX_SBS_BYTES, MAX_XML_NODES, SNIFF_BYTES},
|
||||
placeholder::report_placeholders,
|
||||
sbs_read::SbsRead,
|
||||
sbs_reader::SbsReader,
|
||||
@@ -12,6 +12,9 @@ use roxmltree::Node as XmlNode;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn read_sbs(bytes: &[u8], name: &str, linked: &dyn Fn(&str) -> Option<Vec<u8>>) -> Result<SbsRead> {
|
||||
if bytes.len() as u64 > MAX_SBS_BYTES {
|
||||
return Err(FormatError::BadPackage(format!("the SBS file is {} bytes, over the {MAX_SBS_BYTES} byte limit", bytes.len())));
|
||||
}
|
||||
let text = std::str::from_utf8(bytes).map_err(|e| FormatError::BadPackage(format!("the SBS file is not UTF-8 text: {e}")))?;
|
||||
let text = text.trim_start_matches('\u{feff}');
|
||||
let options = roxmltree::ParsingOptions { allow_dtd: false, nodes_limit: MAX_XML_NODES };
|
||||
@@ -30,8 +33,15 @@ pub fn read_sbs(bytes: &[u8], name: &str, linked: &dyn Fn(&str) -> Option<Vec<u8
|
||||
_ => "Untitled package".to_string(),
|
||||
};
|
||||
|
||||
let mut reader =
|
||||
SbsReader { text, warnings: &mut warnings, dependencies: HashMap::new(), graph_names: HashMap::new(), resource_names: HashMap::new(), node_count: 0 };
|
||||
let mut reader = SbsReader {
|
||||
text,
|
||||
warnings: &mut warnings,
|
||||
dependencies: HashMap::new(),
|
||||
graph_names: HashMap::new(),
|
||||
resource_names: HashMap::new(),
|
||||
node_count: 0,
|
||||
wire_count: 0,
|
||||
};
|
||||
for dependency in child_element(root, "dependencies").into_iter().flat_map(|d| children_named(d, "dependency")) {
|
||||
if let (Some(uid), Some(file)) = (child_value(dependency, "uid"), child_value(dependency, "filename")) {
|
||||
reader.dependencies.insert(uid.to_string(), file.to_string());
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::base_parameters::read_base_params;
|
||||
use super::component_usage;
|
||||
use super::connection::connect_all;
|
||||
use super::constant::MAX_NODES;
|
||||
use super::constant::{MAX_NODES, MAX_WIRES};
|
||||
use super::constant::{NODE_HALF_SIZE, SELF_DEPENDENCY};
|
||||
use super::embedded::decode_embedded;
|
||||
use super::filter_map::{BASE_PARAMS, FilterMap, HANDLED_PARAMS, filter_by_sbs_name};
|
||||
@@ -27,6 +27,7 @@ pub(super) struct SbsReader<'text, 'warnings> {
|
||||
pub(super) graph_names: HashMap<String, String>,
|
||||
pub(super) resource_names: HashMap<String, String>,
|
||||
pub(super) node_count: usize,
|
||||
pub(super) wire_count: usize,
|
||||
}
|
||||
|
||||
impl<'text> SbsReader<'text, '_> {
|
||||
@@ -341,6 +342,11 @@ impl<'text> SbsReader<'text, '_> {
|
||||
}
|
||||
};
|
||||
|
||||
if self.wire_count >= MAX_WIRES {
|
||||
self.warnings.add(format!("graph `{name}`: the {MAX_WIRES} wire limit was reached; later wires were dropped"));
|
||||
return wires;
|
||||
}
|
||||
self.wire_count += 1;
|
||||
wires.push(Connection { from: from.id, from_port, to: to.id, to_port });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ use std::collections::HashMap;
|
||||
pub(super) struct Warnings {
|
||||
pub(super) lines: Vec<(String, usize)>,
|
||||
pub(super) line_index: HashMap<String, usize>,
|
||||
/// Messages over the limit: they are counted but never stored (audit: MaterialCraft #2).
|
||||
dropped: usize,
|
||||
}
|
||||
|
||||
impl Warnings {
|
||||
@@ -14,13 +16,17 @@ impl Warnings {
|
||||
line.1 += 1;
|
||||
return;
|
||||
}
|
||||
if self.lines.len() >= MAX_WARNINGS {
|
||||
self.dropped += 1;
|
||||
return;
|
||||
}
|
||||
|
||||
self.line_index.insert(message.clone(), self.lines.len());
|
||||
self.lines.push((message, 1));
|
||||
}
|
||||
|
||||
pub(super) fn finish(self) -> Vec<String> {
|
||||
let total = self.lines.len();
|
||||
let total = self.lines.len() + self.dropped;
|
||||
let mut out: Vec<String> =
|
||||
self.lines.into_iter().take(MAX_WARNINGS).map(|(line, count)| if count > 1 { format!("{line} (×{count})") } else { line }).collect();
|
||||
if total > MAX_WARNINGS {
|
||||
@@ -29,3 +35,20 @@ impl Warnings {
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn warning_messages_are_capped_as_they_arrive() {
|
||||
let mut w = Warnings::default();
|
||||
for i in 0..MAX_WARNINGS + 5 {
|
||||
w.add(format!("warning {i}"));
|
||||
}
|
||||
assert_eq!(w.lines.len(), MAX_WARNINGS);
|
||||
let out = w.finish();
|
||||
assert_eq!(out.len(), MAX_WARNINGS + 1);
|
||||
assert_eq!(out.last().unwrap(), "…and 5 more warnings");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user