From 4fc45b5c60f168cb2ef46b6938e0211a574beb28 Mon Sep 17 00:00:00 2001 From: Emmanuel Thompson Date: Thu, 30 Apr 2020 09:53:10 -0400 Subject: [PATCH] detect/asn1: Update ASN1 struct lifetime - 'static is only realistic when allocating and leaking it over the FFI boundary --- rust/src/asn1/mod.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/rust/src/asn1/mod.rs b/rust/src/asn1/mod.rs index c4a6ed3641..6fe211afe2 100644 --- a/rust/src/asn1/mod.rs +++ b/rust/src/asn1/mod.rs @@ -25,7 +25,7 @@ use parse_rules::DetectAsn1Data; /// Container for parsed Asn1 objects #[derive(Debug)] -pub struct Asn1(Vec>); +pub struct Asn1<'a>(Vec>); /// Errors possible during decoding of Asn1 #[derive(Debug)] @@ -70,7 +70,7 @@ impl std::fmt::Display for Asn1CheckError { } } -impl Asn1 { +impl<'a> Asn1<'a> { /// Checks each BerObject contained in self with the provided detection /// data, returns the first successful match if one occurs fn check(&self, ad: &DetectAsn1Data) -> Result, Asn1CheckError> { @@ -161,7 +161,7 @@ impl Asn1 { None } - fn from_slice(input: &'static [u8], ad: &DetectAsn1Data) -> Result { + fn from_slice(input: &'a [u8], ad: &DetectAsn1Data) -> Result, Asn1DecodeError> { let mut results = Vec::new(); let mut rest = input; @@ -196,11 +196,11 @@ impl Asn1 { /// Decodes Asn1 objects from an input + length while applying the offset /// defined in the asn1 keyword options -fn asn1_decode( - buffer: &'static [u8], +fn asn1_decode<'a>( + buffer: &'a [u8], buffer_offset: u32, ad: &DetectAsn1Data, -) -> Result { +) -> Result, Asn1DecodeError> { // Get offset let offset = if let Some(absolute_offset) = ad.absolute_offset { absolute_offset @@ -243,19 +243,19 @@ fn asn1_decode( /// input must be a valid buffer of at least input_len bytes /// pointer must be freed using `rs_asn1_free` #[no_mangle] -pub unsafe extern "C" fn rs_asn1_decode( +pub extern "C" fn rs_asn1_decode( input: *const u8, input_len: u16, buffer_offset: u32, ad_ptr: *const DetectAsn1Data, -) -> *mut Asn1 { +) -> *mut Asn1<'static> { if input.is_null() || input_len == 0 || ad_ptr.is_null() { return std::ptr::null_mut(); } let slice = build_slice!(input, input_len as usize); - let ad = &*ad_ptr; + let ad = unsafe { &*ad_ptr }; let res = asn1_decode(slice, buffer_offset, ad); -- 2.47.2