]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect/asn1: Update ASN1 struct lifetime
authorEmmanuel Thompson <eet6646@gmail.com>
Thu, 30 Apr 2020 13:53:10 +0000 (09:53 -0400)
committerVictor Julien <victor@inliniac.net>
Wed, 8 Jul 2020 14:50:38 +0000 (16:50 +0200)
- 'static is only realistic when allocating and leaking it over the
FFI boundary

rust/src/asn1/mod.rs

index c4a6ed3641b5cd67f7f35db90bb2fb067858e847..6fe211afe24299e4ce9de0bd75b758df0730ac77 100644 (file)
@@ -25,7 +25,7 @@ use parse_rules::DetectAsn1Data;
 
 /// Container for parsed Asn1 objects
 #[derive(Debug)]
-pub struct Asn1(Vec<BerObject<'static>>);
+pub struct Asn1<'a>(Vec<BerObject<'a>>);
 
 /// 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<Option<Asn1Check>, Asn1CheckError> {
@@ -161,7 +161,7 @@ impl Asn1 {
         None
     }
 
-    fn from_slice(input: &'static [u8], ad: &DetectAsn1Data) -> Result<Asn1, Asn1DecodeError> {
+    fn from_slice(input: &'a [u8], ad: &DetectAsn1Data) -> Result<Asn1<'a>, 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<Asn1, Asn1DecodeError> {
+) -> Result<Asn1<'a>, 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);