Skip to content

Commit c2202b1

Browse files
catenacybervictorjulien
authored andcommitted
rust: fix build with MSRV
Ticket: 6876 Do not backport try_string_from_bytes as it uses try_reserve And just use string_from_bytes instead Fixes: b9963b3 ("ssh: limit length for banner logs")
1 parent a4d3e9b commit c2202b1

File tree

1 file changed

+1
-30
lines changed

1 file changed

+1
-30
lines changed

rust/src/jsonbuilder.rs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
#![allow(clippy::missing_safety_doc)]
1919

20-
use std::collections::TryReserveError;
2120
use std::ffi::CStr;
2221
use std::os::raw::c_char;
2322
use std::str::Utf8Error;
@@ -28,7 +27,6 @@ const INIT_SIZE: usize = 4096;
2827
pub enum JsonError {
2928
InvalidState,
3029
Utf8Error(Utf8Error),
31-
Memory,
3230
}
3331

3432
impl std::error::Error for JsonError {}
@@ -38,17 +36,10 @@ impl std::fmt::Display for JsonError {
3836
match self {
3937
JsonError::InvalidState => write!(f, "invalid state"),
4038
JsonError::Utf8Error(ref e) => e.fmt(f),
41-
JsonError::Memory => write!(f, "memory error"),
4239
}
4340
}
4441
}
4542

46-
impl From<TryReserveError> for JsonError {
47-
fn from(_: TryReserveError) -> Self {
48-
JsonError::Memory
49-
}
50-
}
51-
5243
impl From<Utf8Error> for JsonError {
5344
fn from(e: Utf8Error) -> Self {
5445
JsonError::Utf8Error(e)
@@ -448,7 +439,7 @@ impl JsonBuilder {
448439
};
449440
match std::str::from_utf8(val) {
450441
Ok(s) => self.set_string(key, s),
451-
Err(_) => self.set_string(key, &try_string_from_bytes(val)?),
442+
Err(_) => self.set_string(key, &string_from_bytes(val)),
452443
}
453444
}
454445

@@ -595,26 +586,6 @@ fn string_from_bytes(input: &[u8]) -> String {
595586
return out;
596587
}
597588

598-
/// A Suricata specific function to create a string from bytes when UTF-8 decoding fails.
599-
///
600-
/// For bytes over 0x0f, we encode as hex like "\xf2".
601-
fn try_string_from_bytes(input: &[u8]) -> Result<String, JsonError> {
602-
let mut out = String::new();
603-
604-
// Allocate enough data to handle the worst case scenario of every
605-
// byte needing to be presented as a byte.
606-
out.try_reserve(input.len() * 4)?;
607-
608-
for b in input.iter() {
609-
if *b < 128 {
610-
out.push(*b as char);
611-
} else {
612-
out.push_str(&format!("\\x{:02x}", *b));
613-
}
614-
}
615-
return Ok(out);
616-
}
617-
618589
#[no_mangle]
619590
pub extern "C" fn jb_new_object() -> *mut JsonBuilder {
620591
let boxed = Box::new(JsonBuilder::new_object());

0 commit comments

Comments
 (0)