Skip to content

Commit

Permalink
rust: fix build with MSRV
Browse files Browse the repository at this point in the history
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")
  • Loading branch information
catenacyber authored and victorjulien committed Mar 20, 2024
1 parent a4d3e9b commit c2202b1
Showing 1 changed file with 1 addition and 30 deletions.
31 changes: 1 addition & 30 deletions rust/src/jsonbuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#![allow(clippy::missing_safety_doc)]

use std::collections::TryReserveError;
use std::ffi::CStr;
use std::os::raw::c_char;
use std::str::Utf8Error;
Expand All @@ -28,7 +27,6 @@ const INIT_SIZE: usize = 4096;
pub enum JsonError {
InvalidState,
Utf8Error(Utf8Error),
Memory,
}

impl std::error::Error for JsonError {}
Expand All @@ -38,17 +36,10 @@ impl std::fmt::Display for JsonError {
match self {
JsonError::InvalidState => write!(f, "invalid state"),
JsonError::Utf8Error(ref e) => e.fmt(f),
JsonError::Memory => write!(f, "memory error"),
}
}
}

impl From<TryReserveError> for JsonError {
fn from(_: TryReserveError) -> Self {
JsonError::Memory
}
}

impl From<Utf8Error> for JsonError {
fn from(e: Utf8Error) -> Self {
JsonError::Utf8Error(e)
Expand Down Expand Up @@ -448,7 +439,7 @@ impl JsonBuilder {
};
match std::str::from_utf8(val) {
Ok(s) => self.set_string(key, s),
Err(_) => self.set_string(key, &try_string_from_bytes(val)?),
Err(_) => self.set_string(key, &string_from_bytes(val)),
}
}

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

/// A Suricata specific function to create a string from bytes when UTF-8 decoding fails.
///
/// For bytes over 0x0f, we encode as hex like "\xf2".
fn try_string_from_bytes(input: &[u8]) -> Result<String, JsonError> {
let mut out = String::new();

// Allocate enough data to handle the worst case scenario of every
// byte needing to be presented as a byte.
out.try_reserve(input.len() * 4)?;

for b in input.iter() {
if *b < 128 {
out.push(*b as char);
} else {
out.push_str(&format!("\\x{:02x}", *b));
}
}
return Ok(out);
}

#[no_mangle]
pub extern "C" fn jb_new_object() -> *mut JsonBuilder {
let boxed = Box::new(JsonBuilder::new_object());
Expand Down

0 comments on commit c2202b1

Please sign in to comment.