From 63ca8212f646fa251a8ff1489700e2ccab69d82f Mon Sep 17 00:00:00 2001 From: Arthur Baars Date: Fri, 30 Oct 2020 19:41:26 +0100 Subject: [PATCH] Limit string sizes to 1MB --- extractor/src/extractor.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/extractor/src/extractor.rs b/extractor/src/extractor.rs index f36f012760a..02871f24e0d 100644 --- a/extractor/src/extractor.rs +++ b/extractor/src/extractor.rs @@ -627,12 +627,37 @@ enum Arg { String(String), } +const MAX_STRLEN: usize = 1048576; + impl fmt::Display for Arg { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Arg::Label(x) => write!(f, "{}", x), Arg::Int(x) => write!(f, "{}", x), - Arg::String(x) => write!(f, "\"{}\"", x.replace("\"", "\"\"")), + Arg::String(x) => write!( + f, + "\"{}\"", + limit_string(x, MAX_STRLEN).replace("\"", "\"\"") + ), } } } + +fn limit_string(string: &String, max_size: usize) -> &str { + if string.len() <= max_size { + return string; + } + let p = string.as_ptr(); + let mut index = max_size; + while index > 0 && unsafe { (*p.offset(index as isize) & 0b11000000) == 0b10000000 } { + index -= 1; + } + &string[0..index] +} + +#[test] +fn limit_string_test() { + assert_eq!("hello", limit_string(&"hello world".to_owned(), 5)); + assert_eq!("hi ☹", limit_string(&"hi ☹☹".to_owned(), 6)); + assert_eq!("hi ", limit_string(&"hi ☹☹".to_owned(), 5)); +}