From 09ea4126140982c51a52362083eece8d5ad08f57 Mon Sep 17 00:00:00 2001 From: Juliana Fajardini Date: Tue, 10 Aug 2021 15:57:48 +0100 Subject: [PATCH 01/24] util-debug: fix unchecked ConfGetBool call --- src/util-debug.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/util-debug.h b/src/util-debug.h index 8d92d8bf8931..9c93b5ce5150 100644 --- a/src/util-debug.h +++ b/src/util-debug.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2010 Open Information Security Foundation +/* Copyright (C) 2007-2021 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -537,17 +537,17 @@ void SCLogErr(int x, const char *file, const char *func, const int line, /** \brief Fatal error IF we're starting up, and configured to consider * errors to be fatal errors */ #if !defined(__clang_analyzer__) -#define FatalErrorOnInit(x, ...) do { \ - SC_ATOMIC_EXTERN(unsigned int, engine_stage); \ - int init_errors_fatal = 0; \ - ConfGetBool("engine.init-failure-fatal", &init_errors_fatal); \ - if (init_errors_fatal && (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT))\ - { \ - SCLogError(x, __VA_ARGS__); \ - exit(EXIT_FAILURE); \ - } \ - SCLogWarning(x, __VA_ARGS__); \ -} while(0) +#define FatalErrorOnInit(x, ...) \ + do { \ + SC_ATOMIC_EXTERN(unsigned int, engine_stage); \ + int init_errors_fatal = 0; \ + (void)ConfGetBool("engine.init-failure-fatal", &init_errors_fatal); \ + if (init_errors_fatal && (SC_ATOMIC_GET(engine_stage) == SURICATA_INIT)) { \ + SCLogError(x, __VA_ARGS__); \ + exit(EXIT_FAILURE); \ + } \ + SCLogWarning(x, __VA_ARGS__); \ + } while (0) /* make it simpler for scan-build */ #else #define FatalErrorOnInit(x, ...) FatalError(x, __VA_ARGS__) From fbade2584896e1512c63c9bae6cc170e0d83f170 Mon Sep 17 00:00:00 2001 From: Juliana Fajardini Date: Tue, 10 Aug 2021 16:03:50 +0100 Subject: [PATCH 02/24] util-napatech: fix ConfGetBool unchecked call --- src/util-napatech.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util-napatech.c b/src/util-napatech.c index 9bf7b16576dc..6f0c19c4bf70 100644 --- a/src/util-napatech.c +++ b/src/util-napatech.c @@ -878,7 +878,7 @@ int NapatechGetStreamConfig(NapatechStreamConfig stream_config[]) } } else { - ConfGetBool("threading.set-cpu-affinity", &set_cpu_affinity); + (void)ConfGetBool("threading.set-cpu-affinity", &set_cpu_affinity); if (NapatechIsAutoConfigEnabled() && (set_cpu_affinity == 1)) { start = 0; end = CountWorkerThreads() - 1; From 7198355324fa199d10f299f05aa90bf113a667f5 Mon Sep 17 00:00:00 2001 From: Juliana Fajardini Date: Tue, 10 Aug 2021 16:29:31 +0100 Subject: [PATCH 03/24] util-napatech: fix typos, update copyright year --- src/util-napatech.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/util-napatech.c b/src/util-napatech.c index 6f0c19c4bf70..72c968ef665a 100644 --- a/src/util-napatech.c +++ b/src/util-napatech.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2017 Open Information Security Foundation +/* Copyright (C) 2017-2021 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -467,10 +467,10 @@ static uint32_t UpdateStreamStats(ThreadVars *tv, /** * \brief Statistics processing loop * - * Instantiated on the stats thread. Periodically retrieives + * Instantiated on the stats thread. Periodically retrieves * statistics from the Napatech card and updates the packet counters * - * \param arg Pointer that is caste into a TheardVars structure + * \param arg Pointer that is cast into a TheardVars structure */ static void *NapatechStatsLoop(void *arg) { From 4839088359da0fa2b6c865bdeb1df095cb07e3c9 Mon Sep 17 00:00:00 2001 From: Juliana Fajardini Date: Tue, 10 Aug 2021 16:31:17 +0100 Subject: [PATCH 04/24] stream-tcp: fix ConfGetBool unchecked call --- src/stream-tcp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stream-tcp.c b/src/stream-tcp.c index 29ed3c9f69e6..b4bc097a3a20 100644 --- a/src/stream-tcp.c +++ b/src/stream-tcp.c @@ -419,14 +419,14 @@ void StreamTcpInitConfig(bool quiet) } int imidstream; - ConfGetBool("stream.midstream", &imidstream); + (void)ConfGetBool("stream.midstream", &imidstream); stream_config.midstream = imidstream != 0; if (!quiet) { SCLogConfig("stream \"midstream\" session pickups: %s", stream_config.midstream ? "enabled" : "disabled"); } - ConfGetBool("stream.async-oneside", &stream_config.async_oneside); + (void)ConfGetBool("stream.async-oneside", &stream_config.async_oneside); if (!quiet) { SCLogConfig("stream \"async-oneside\": %s", stream_config.async_oneside ? "enabled" : "disabled"); From 2e0d76e6e7ee9067a177447c4d160c4b6ea941dd Mon Sep 17 00:00:00 2001 From: Juliana Fajardini Date: Tue, 10 Aug 2021 17:17:19 +0100 Subject: [PATCH 05/24] stream-tcp: fix typos, update copyright year --- src/stream-tcp.c | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/stream-tcp.c b/src/stream-tcp.c index b4bc097a3a20..1239cd0ba519 100644 --- a/src/stream-tcp.c +++ b/src/stream-tcp.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2016 Open Information Security Foundation +/* Copyright (C) 2007-2021 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -555,7 +555,7 @@ void StreamTcpInitConfig(bool quiet) int randomize = 0; if ((ConfGetBool("stream.reassembly.randomize-chunk-size", &randomize)) == 0) { /* randomize by default if value not set - * In ut mode we disable, to get predictible test results */ + * In ut mode we disable, to get predictable test results */ if (!(RunmodeIsUnittests())) randomize = 1; } @@ -898,7 +898,7 @@ static int StreamTcpPacketIsRetransmission(TcpStream *stream, Packet *p) * packets while the session state is None which means a newly * initialized structure, or a fully closed session. * - * \param tv Thread Variable containig input/output queue, cpu affinity + * \param tv Thread Variable containing input/output queue, cpu affinity * \param p Packet which has to be handled in this TCP state. * \param stt Strean Thread module registered to handle the stream handling * @@ -1408,7 +1408,7 @@ static inline bool StateSynSentValidateTimestamp(TcpSession *ssn, Packet *p) * SYN, SYN/ACK, RST packets and correspondingly changes the connection * state. * - * \param tv Thread Variable containig input/output queue, cpu affinity + * \param tv Thread Variable containing input/output queue, cpu affinity * \param p Packet which has to be handled in this TCP state. * \param stt Strean Thread module registered to handle the stream handling */ @@ -1649,8 +1649,8 @@ static int StreamTcpPacketStateSynSent(ThreadVars *tv, Packet *p, } else if (p->tcph->th_flags & TH_ACK) { /* Handle the asynchronous stream, when we receive a SYN packet - and now istead of receving a SYN/ACK we receive a ACK from the - same host, which sent the SYN, this suggests the ASNYC streams.*/ + and now instead of receiving a SYN/ACK we receive a ACK from the + same host, which sent the SYN, this suggests the ASYNC streams.*/ if (stream_config.async_oneside == FALSE) return 0; @@ -1726,7 +1726,7 @@ static int StreamTcpPacketStateSynSent(ThreadVars *tv, Packet *p, * SYN, SYN/ACK, ACK, FIN, RST packets and correspondingly changes * the connection state. * - * \param tv Thread Variable containig input/output queue, cpu affinity + * \param tv Thread Variable containing input/output queue, cpu affinity * \param p Packet which has to be handled in this TCP state. * \param stt Strean Thread module registered to handle the stream handling * @@ -2156,7 +2156,7 @@ static int StreamTcpPacketStateSynRecv(ThreadVars *tv, Packet *p, * * Timestamp has already been checked at this point. * - * \param tv Thread Variable containig input/output queue, cpu affinity etc. + * \param tv Thread Variable containing input/output queue, cpu affinity etc. * \param ssn Pointer to the current TCP session * \param p Packet which has to be handled in this TCP state. * \param stt Strean Thread module registered to handle the stream handling @@ -2343,7 +2343,7 @@ static int HandleEstablishedPacketToServer(ThreadVars *tv, TcpSession *ssn, Pack * * Timestamp has already been checked at this point. * - * \param tv Thread Variable containig input/output queue, cpu affinity etc. + * \param tv Thread Variable containing input/output queue, cpu affinity etc. * \param ssn Pointer to the current TCP session * \param p Packet which has to be handled in this TCP state. * \param stt Strean Thread module registered to handle the stream handling @@ -2576,7 +2576,7 @@ static bool StreamTcpPacketIsOutdatedAck(TcpSession *ssn, Packet *p) * state. The function handles the data inside packets and call * StreamTcpReassembleHandleSegment(tv, ) to handle the reassembling. * - * \param tv Thread Variable containig input/output queue, cpu affinity etc. + * \param tv Thread Variable containing input/output queue, cpu affinity etc. * \param p Packet which has to be handled in this TCP state. * \param stt Strean Thread module registered to handle the stream handling */ @@ -2780,7 +2780,7 @@ static int StreamTcpPacketStateEstablished(ThreadVars *tv, Packet *p, * \brief Function to handle the FIN packets for states TCP_SYN_RECV and * TCP_ESTABLISHED and changes to another TCP state as required. * - * \param tv Thread Variable containig input/output queue, cpu affinity + * \param tv Thread Variable containing input/output queue, cpu affinity * \param p Packet which has to be handled in this TCP state. * \param stt Strean Thread module registered to handle the stream handling * @@ -2897,7 +2897,7 @@ static int StreamTcpHandleFin(ThreadVars *tv, StreamTcpThread *stt, * ACK, FIN, RST packets and correspondingly changes the connection * state. * - * \param tv Thread Variable containig input/output queue, cpu affinity + * \param tv Thread Variable containing input/output queue, cpu affinity * \param p Packet which has to be handled in this TCP state. * \param stt Strean Thread module registered to handle the stream handling * @@ -3342,7 +3342,7 @@ static int StreamTcpPacketStateFinWait1(ThreadVars *tv, Packet *p, * ACK, RST, FIN packets and correspondingly changes the connection * state. * - * \param tv Thread Variable containig input/output queue, cpu affinity + * \param tv Thread Variable containing input/output queue, cpu affinity * \param p Packet which has to be handled in this TCP state. * \param stt Strean Thread module registered to handle the stream handling */ @@ -3645,7 +3645,7 @@ static int StreamTcpPacketStateFinWait2(ThreadVars *tv, Packet *p, * the connection goes to TCP_TIME_WAIT state. The state has been * reached as both end application has been closed. * - * \param tv Thread Variable containig input/output queue, cpu affinity + * \param tv Thread Variable containing input/output queue, cpu affinity * \param p Packet which has to be handled in this TCP state. * \param stt Strean Thread module registered to handle the stream handling */ @@ -3809,7 +3809,7 @@ static int StreamTcpPacketStateClosing(ThreadVars *tv, Packet *p, * packet from server the connection goes to TCP_LAST_ACK state. * The state is possible only for server host. * - * \param tv Thread Variable containig input/output queue, cpu affinity + * \param tv Thread Variable containing input/output queue, cpu affinity * \param p Packet which has to be handled in this TCP state. * \param stt Strean Thread module registered to handle the stream handling */ @@ -4112,7 +4112,7 @@ static int StreamTcpPacketStateCloseWait(ThreadVars *tv, Packet *p, * the connection goes to TCP_CLOSED state and stream memory is * returned back to pool. The state is possible only for server host. * - * \param tv Thread Variable containig input/output queue, cpu affinity + * \param tv Thread Variable containing input/output queue, cpu affinity * \param p Packet which has to be handled in this TCP state. * \param stt Strean Thread module registered to handle the stream handling */ @@ -4237,7 +4237,7 @@ static int StreamTcpPacketStateLastAck(ThreadVars *tv, Packet *p, * the connection goes to TCP_CLOSED state and stream memory is * returned back to pool. * - * \param tv Thread Variable containig input/output queue, cpu affinity + * \param tv Thread Variable containing input/output queue, cpu affinity * \param p Packet which has to be handled in this TCP state. * \param stt Strean Thread module registered to handle the stream handling */ @@ -5633,7 +5633,7 @@ static int StreamTcpValidateTimestamp (TcpSession *ssn, Packet *p) } if (receiver_stream->os_policy == OS_POLICY_HPUX11) { - /* HPUX11 igoners the timestamp of out of order packets */ + /* HPUX11 ignores the timestamp of out of order packets */ if (!SEQ_EQ(sender_stream->next_seq, TCP_GET_SEQ(p))) check_ts = 0; } @@ -5648,7 +5648,7 @@ static int StreamTcpValidateTimestamp (TcpSession *ssn, Packet *p) /* Old Linux and windows allowed packet with 0 timestamp. */ break; default: - /* other OS simply drop the pakcet with 0 timestamp, when + /* other OS simply drop the packet with 0 timestamp, when * 3whs has valid timestamp*/ goto invalid; } @@ -5777,7 +5777,7 @@ static int StreamTcpHandleTimestamp (TcpSession *ssn, Packet *p) } if (receiver_stream->os_policy == OS_POLICY_HPUX11) { - /*HPUX11 igoners the timestamp of out of order packets*/ + /*HPUX11 ignores the timestamp of out of order packets*/ if (!SEQ_EQ(sender_stream->next_seq, TCP_GET_SEQ(p))) check_ts = 0; } @@ -5792,7 +5792,7 @@ static int StreamTcpHandleTimestamp (TcpSession *ssn, Packet *p) /* Old Linux and windows allowed packet with 0 timestamp. */ break; default: - /* other OS simply drop the pakcet with 0 timestamp, when + /* other OS simply drop the packet with 0 timestamp, when * 3whs has valid timestamp*/ goto invalid; } @@ -6059,7 +6059,7 @@ Packet *StreamTcpPseudoSetup(Packet *parent, uint8_t *pkt, uint32_t len) else p->root = parent; - /* copy packet and set lenght, proto */ + /* copy packet and set length, proto */ p->proto = parent->proto; p->datalink = parent->datalink; From 613f9b2f5ae45409553b9010e9ea5a85d48c2974 Mon Sep 17 00:00:00 2001 From: Juliana Fajardini Date: Tue, 10 Aug 2021 17:27:32 +0100 Subject: [PATCH 06/24] stream-tcp-reassemble: fix ConfGetBool unc'kd call --- src/stream-tcp-reassemble.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stream-tcp-reassemble.c b/src/stream-tcp-reassemble.c index b09c49634d0b..ab31d629ed3b 100644 --- a/src/stream-tcp-reassemble.c +++ b/src/stream-tcp-reassemble.c @@ -378,7 +378,7 @@ static int StreamTcpReassemblyConfig(bool quiet) stream_config.prealloc_segments = segment_prealloc; int overlap_diff_data = 0; - ConfGetBool("stream.reassembly.check-overlap-different-data", &overlap_diff_data); + (void)ConfGetBool("stream.reassembly.check-overlap-different-data", &overlap_diff_data); if (overlap_diff_data) { StreamTcpReassembleConfigEnableOverlapCheck(); } From ff976df70414d7d16ec5cb60cd3d08fa82c9c258 Mon Sep 17 00:00:00 2001 From: Juliana Fajardini Date: Tue, 10 Aug 2021 17:28:55 +0100 Subject: [PATCH 07/24] stream-tcp-reassemble: fix typo, updt copyright yr --- src/stream-tcp-reassemble.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/stream-tcp-reassemble.c b/src/stream-tcp-reassemble.c index ab31d629ed3b..fae1a4619dc5 100644 --- a/src/stream-tcp-reassemble.c +++ b/src/stream-tcp-reassemble.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2020 Open Information Security Foundation +/* Copyright (C) 2007-2021 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -137,7 +137,7 @@ uint64_t StreamTcpReassembleMemuseGlobalCounter(void) /** * \brief Function to Check the reassembly memory usage counter against the - * allowed max memory usgae for TCP segments. + * allowed max memory usage for TCP segments. * * \param size Size of the TCP segment and its payload length memory allocated * \retval 1 if in bounds @@ -456,7 +456,7 @@ TcpReassemblyThreadCtx *StreamTcpReassembleInitThreadCtx(ThreadVars *tv) PoolThreadSize(segment_thread_pool), ra_ctx->segment_thread_pool_id); } else { - /* grow segment_thread_pool until we have a element for our thread id */ + /* grow segment_thread_pool until we have an element for our thread id */ ra_ctx->segment_thread_pool_id = PoolThreadExpand(segment_thread_pool); SCLogDebug("pool size %d, thread segment_thread_pool_id %d", PoolThreadSize(segment_thread_pool), From 7ca4b135681db9f0e6477cc083bce6d8b50edbe4 Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Thu, 22 Jul 2021 18:54:16 +0200 Subject: [PATCH 08/24] qa: use time on fuzz targets being run on corpus --- .github/workflows/builds.yml | 1 + qa/run-ossfuzz-corpus.sh | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/builds.yml b/.github/workflows/builds.yml index 9555ae20797c..902a4bec8735 100644 --- a/.github/workflows/builds.yml +++ b/.github/workflows/builds.yml @@ -741,6 +741,7 @@ jobs: exuberant-ctags \ unzip \ curl \ + time \ wget - uses: actions/checkout@v2 - uses: actions/download-artifact@v2 diff --git a/qa/run-ossfuzz-corpus.sh b/qa/run-ossfuzz-corpus.sh index c2ac05b3b695..81959c35a2ad 100755 --- a/qa/run-ossfuzz-corpus.sh +++ b/qa/run-ossfuzz-corpus.sh @@ -9,5 +9,5 @@ do rm -rf corpus_$target unzip -q public.zip -d corpus_$target #run target on corpus. - ./src/$target corpus_$target + /usr/bin/time -v ./src/$target corpus_$target done From cbd03c7ea4c5a74d724b10bf65dbbc2082e061d6 Mon Sep 17 00:00:00 2001 From: KevinWang Date: Fri, 6 Aug 2021 19:39:04 +0800 Subject: [PATCH 09/24] output/redis: Fix possible segv --- src/util-log-redis.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/util-log-redis.c b/src/util-log-redis.c index 544bef154578..6814b142ab88 100644 --- a/src/util-log-redis.c +++ b/src/util-log-redis.c @@ -210,6 +210,7 @@ static int SCConfLogReopenAsyncRedis(LogFileCtx *log_ctx) if (ctx->ev_base != NULL) { event_base_free(ctx->ev_base); + ctx->ev_base = NULL; } if (ctx->async == NULL) { From 53413f2d7af513f631fcae316a5ea2142d94e977 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Mon, 26 Jul 2021 13:25:17 -0600 Subject: [PATCH 10/24] rust: remove all usage of transmute All cases of our transmute can be replaced with more idiomatic solutions and do no require the power of transmute. When returning an object to C for life-time management, use Box::into_raw to convert the boxed object to pointer and use Box::from_raw to convert back. For cases where we're just returning a pointer to Rust managed data, use a cast. --- rust/src/applayertemplate/template.rs | 10 ++++------ rust/src/dcerpc/dcerpc.rs | 7 +++---- rust/src/dcerpc/dcerpc_udp.rs | 8 +++----- rust/src/dcerpc/detect.rs | 8 ++++---- rust/src/dhcp/dhcp.rs | 12 ++++-------- rust/src/dhcp/logger.rs | 4 ++-- rust/src/dns/detect.rs | 4 ++-- rust/src/dns/dns.rs | 9 ++++----- rust/src/http2/detect.rs | 9 ++++----- rust/src/http2/http2.rs | 10 ++++------ rust/src/ike/ike.rs | 9 ++++----- rust/src/krb/krb5.rs | 7 +++---- rust/src/mqtt/mqtt.rs | 9 ++++----- rust/src/nfs/nfs.rs | 9 ++++----- rust/src/ntp/ntp.rs | 7 +++---- rust/src/rdp/rdp.rs | 7 +++---- rust/src/rfb/rfb.rs | 9 ++++----- rust/src/sip/sip.rs | 6 +++--- rust/src/smb/smb.rs | 10 ++++------ rust/src/snmp/snmp.rs | 12 +++++------- rust/src/ssh/ssh.rs | 8 +++----- rust/src/tftp/tftp.rs | 7 +++---- 22 files changed, 77 insertions(+), 104 deletions(-) diff --git a/rust/src/applayertemplate/template.rs b/rust/src/applayertemplate/template.rs index 50af3526eab3..36f6d3b79055 100644 --- a/rust/src/applayertemplate/template.rs +++ b/rust/src/applayertemplate/template.rs @@ -17,7 +17,6 @@ use std; use crate::core::{self, ALPROTO_UNKNOWN, AppProto, Flow, IPPROTO_TCP}; -use std::mem::transmute; use crate::applayer::{self, *}; use std::ffi::CString; use nom; @@ -298,13 +297,12 @@ pub extern "C" fn rs_template_probing_parser( pub extern "C" fn rs_template_state_new(_orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto) -> *mut std::os::raw::c_void { let state = TemplateState::new(); let boxed = Box::new(state); - return unsafe { transmute(boxed) }; + return Box::into_raw(boxed) as *mut std::os::raw::c_void; } #[no_mangle] pub extern "C" fn rs_template_state_free(state: *mut std::os::raw::c_void) { - // Just unbox... - let _drop: Box = unsafe { transmute(state) }; + std::mem::drop(unsafe { Box::from_raw(state as *mut TemplateState) }); } #[no_mangle] @@ -390,7 +388,7 @@ pub extern "C" fn rs_template_state_get_tx( let state = cast_pointer!(state, TemplateState); match state.get_tx(tx_id) { Some(tx) => { - return unsafe { transmute(tx) }; + return tx as *const _ as *mut _; } None => { return std::ptr::null_mut(); @@ -456,7 +454,7 @@ pub extern "C" fn rs_template_state_get_tx_iterator( let state = cast_pointer!(state, TemplateState); match state.tx_iterator(min_tx_id, istate) { Some((tx, out_tx_id, has_next)) => { - let c_tx = unsafe { transmute(tx) }; + let c_tx = tx as *const _ as *mut _; let ires = applayer::AppLayerGetTxIterTuple::with_values( c_tx, out_tx_id, diff --git a/rust/src/dcerpc/dcerpc.rs b/rust/src/dcerpc/dcerpc.rs index 51743c8cc901..d7b54f0f7522 100644 --- a/rust/src/dcerpc/dcerpc.rs +++ b/rust/src/dcerpc/dcerpc.rs @@ -15,7 +15,6 @@ * 02110-1301, USA. */ -use std::mem::transmute; use crate::applayer::*; use crate::core::{self, *}; use crate::dcerpc::parser; @@ -1193,12 +1192,12 @@ pub extern "C" fn rs_dcerpc_parse_response( pub extern "C" fn rs_dcerpc_state_new(_orig_state: *mut std::os::raw::c_void, _orig_proto: core::AppProto) -> *mut std::os::raw::c_void { let state = DCERPCState::new(); let boxed = Box::new(state); - return unsafe { transmute(boxed)}; + return Box::into_raw(boxed) as *mut _; } #[no_mangle] pub extern "C" fn rs_dcerpc_state_free(state: *mut std::os::raw::c_void) { - let _state: Box = unsafe { transmute(state) }; + std::mem::drop(unsafe { Box::from_raw(state as *mut DCERPCState)} ); } #[no_mangle] @@ -1258,7 +1257,7 @@ pub extern "C" fn rs_dcerpc_get_tx( ) -> *mut std::os::raw::c_void { let dce_state = cast_pointer!(vtx, DCERPCState); match dce_state.get_tx(tx_id) { - Some(tx) => unsafe { transmute(tx) }, + Some(tx) => tx as *const _ as *mut _, None => std::ptr::null_mut(), } } diff --git a/rust/src/dcerpc/dcerpc_udp.rs b/rust/src/dcerpc/dcerpc_udp.rs index c3671aaccb5f..f6f5ed961f7a 100644 --- a/rust/src/dcerpc/dcerpc_udp.rs +++ b/rust/src/dcerpc/dcerpc_udp.rs @@ -15,8 +15,6 @@ * 02110-1301, USA. */ -use std::mem::transmute; - use crate::applayer::*; use crate::core; use crate::dcerpc::dcerpc::{ @@ -221,14 +219,14 @@ pub extern "C" fn rs_dcerpc_udp_parse( #[no_mangle] pub extern "C" fn rs_dcerpc_udp_state_free(state: *mut std::os::raw::c_void) { - let _drop: Box = unsafe { transmute(state) }; + std::mem::drop(unsafe { Box::from_raw(state as *mut DCERPCUDPState) }); } #[no_mangle] pub extern "C" fn rs_dcerpc_udp_state_new(_orig_state: *mut std::os::raw::c_void, _orig_proto: core::AppProto) -> *mut std::os::raw::c_void { let state = DCERPCUDPState::new(); let boxed = Box::new(state); - return unsafe { transmute(boxed) }; + return Box::into_raw(boxed) as *mut _; } #[no_mangle] @@ -276,7 +274,7 @@ pub extern "C" fn rs_dcerpc_udp_get_tx( let dce_state = cast_pointer!(state, DCERPCUDPState); match dce_state.get_tx(tx_id) { Some(tx) => { - return unsafe{ transmute(tx) }; + return tx as *const _ as *mut _; }, None => { return std::ptr::null_mut(); diff --git a/rust/src/dcerpc/detect.rs b/rust/src/dcerpc/detect.rs index 49b3492c6c6b..5be74782a6c1 100644 --- a/rust/src/dcerpc/detect.rs +++ b/rust/src/dcerpc/detect.rs @@ -281,7 +281,7 @@ pub unsafe extern "C" fn rs_dcerpc_iface_parse(carg: *const c_char) -> *mut c_vo }; match parse_iface_data(&arg) { - Ok(detect) => std::mem::transmute(Box::new(detect)), + Ok(detect) => Box::into_raw(Box::new(detect)) as *mut _, Err(_) => std::ptr::null_mut(), } } @@ -289,7 +289,7 @@ pub unsafe extern "C" fn rs_dcerpc_iface_parse(carg: *const c_char) -> *mut c_vo #[no_mangle] pub unsafe extern "C" fn rs_dcerpc_iface_free(ptr: *mut c_void) { if ptr != std::ptr::null_mut() { - let _: Box = std::mem::transmute(ptr); + std::mem::drop(Box::from_raw(ptr as *mut DCEIfaceData)); } } @@ -328,7 +328,7 @@ pub unsafe extern "C" fn rs_dcerpc_opnum_parse(carg: *const c_char) -> *mut c_vo }; match parse_opnum_data(&arg) { - Ok(detect) => std::mem::transmute(Box::new(detect)), + Ok(detect) => Box::into_raw(Box::new(detect)) as *mut _, Err(_) => std::ptr::null_mut(), } } @@ -336,7 +336,7 @@ pub unsafe extern "C" fn rs_dcerpc_opnum_parse(carg: *const c_char) -> *mut c_vo #[no_mangle] pub unsafe extern "C" fn rs_dcerpc_opnum_free(ptr: *mut c_void) { if ptr != std::ptr::null_mut() { - let _: Box = std::mem::transmute(ptr); + std::mem::drop(Box::from_raw(ptr as *mut DCEOpnumData)); } } diff --git a/rust/src/dhcp/dhcp.rs b/rust/src/dhcp/dhcp.rs index d90a281eb10f..a9ae1f42c2e1 100644 --- a/rust/src/dhcp/dhcp.rs +++ b/rust/src/dhcp/dhcp.rs @@ -22,7 +22,6 @@ use crate::core::{sc_detect_engine_state_free, sc_app_layer_decoder_events_free_ use crate::dhcp::parser::*; use std; use std::ffi::{CStr,CString}; -use std::mem::transmute; static mut ALPROTO_DHCP: AppProto = ALPROTO_UNKNOWN; @@ -256,7 +255,7 @@ pub extern "C" fn rs_dhcp_state_get_tx(state: *mut std::os::raw::c_void, let state = cast_pointer!(state, DHCPState); match state.get_tx(tx_id) { Some(tx) => { - return unsafe { transmute(tx) }; + return tx as *const _ as *mut _; } None => { return std::ptr::null_mut(); @@ -299,15 +298,12 @@ pub extern "C" fn rs_dhcp_state_tx_free( pub extern "C" fn rs_dhcp_state_new(_orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto) -> *mut std::os::raw::c_void { let state = DHCPState::new(); let boxed = Box::new(state); - return unsafe { - transmute(boxed) - }; + return Box::into_raw(boxed) as *mut _; } #[no_mangle] pub extern "C" fn rs_dhcp_state_free(state: *mut std::os::raw::c_void) { - // Just unbox... - let _drop: Box = unsafe { transmute(state) }; + std::mem::drop(unsafe { Box::from_raw(state as *mut DHCPState) }); } #[no_mangle] @@ -379,7 +375,7 @@ pub extern "C" fn rs_dhcp_state_get_tx_iterator( let state = cast_pointer!(state, DHCPState); match state.get_tx_iterator(min_tx_id, istate) { Some((tx, out_tx_id, has_next)) => { - let c_tx = unsafe { transmute(tx) }; + let c_tx = tx as *const _ as *mut _; let ires = applayer::AppLayerGetTxIterTuple::with_values( c_tx, out_tx_id, has_next); return ires; diff --git a/rust/src/dhcp/logger.rs b/rust/src/dhcp/logger.rs index 135d523278c1..1b4b4a977c55 100644 --- a/rust/src/dhcp/logger.rs +++ b/rust/src/dhcp/logger.rs @@ -259,12 +259,12 @@ fn format_addr_hex(input: &Vec) -> String { pub extern "C" fn rs_dhcp_logger_new(conf: *const c_void) -> *mut std::os::raw::c_void { let conf = ConfNode::wrap(conf); let boxed = Box::new(DHCPLogger::new(conf)); - return unsafe{std::mem::transmute(boxed)}; + return Box::into_raw(boxed) as *mut _; } #[no_mangle] pub extern "C" fn rs_dhcp_logger_free(logger: *mut std::os::raw::c_void) { - let _: Box = unsafe{std::mem::transmute(logger)}; + std::mem::drop(unsafe { Box::from_raw(logger as *mut DHCPLogger) }); } #[no_mangle] diff --git a/rust/src/dns/detect.rs b/rust/src/dns/detect.rs index bbf3c71c7522..3cf626e0172d 100644 --- a/rust/src/dns/detect.rs +++ b/rust/src/dns/detect.rs @@ -108,7 +108,7 @@ pub unsafe extern "C" fn rs_detect_dns_opcode_parse(carg: *const c_char) -> *mut }; match parse_opcode(&arg) { - Ok(detect) => std::mem::transmute(Box::new(detect)), + Ok(detect) => Box::into_raw(Box::new(detect)) as *mut _, Err(_) => std::ptr::null_mut(), } } @@ -116,7 +116,7 @@ pub unsafe extern "C" fn rs_detect_dns_opcode_parse(carg: *const c_char) -> *mut #[no_mangle] pub unsafe extern "C" fn rs_dns_detect_opcode_free(ptr: *mut c_void) { if ptr != std::ptr::null_mut() { - let _: Box = std::mem::transmute(ptr); + std::mem::drop(Box::from_raw(ptr as *mut DetectDnsOpcode)); } } diff --git a/rust/src/dns/dns.rs b/rust/src/dns/dns.rs index e0f0f8a9c36e..76c441d4997e 100644 --- a/rust/src/dns/dns.rs +++ b/rust/src/dns/dns.rs @@ -19,7 +19,6 @@ extern crate nom; use std; use std::ffi::CString; -use std::mem::transmute; use std::collections::HashMap; use std::collections::VecDeque; @@ -720,7 +719,7 @@ pub fn probe_tcp(input: &[u8]) -> (bool, bool, bool) { pub extern "C" fn rs_dns_state_new(_orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto) -> *mut std::os::raw::c_void { let state = DNSState::new(); let boxed = Box::new(state); - return unsafe{transmute(boxed)}; + return Box::into_raw(boxed) as *mut _; } /// Returns *mut DNSState @@ -728,7 +727,7 @@ pub extern "C" fn rs_dns_state_new(_orig_state: *mut std::os::raw::c_void, _orig pub extern "C" fn rs_dns_state_tcp_new() -> *mut std::os::raw::c_void { let state = DNSState::new_tcp(); let boxed = Box::new(state); - return unsafe{transmute(boxed)}; + return Box::into_raw(boxed) as *mut _; } /// Params: @@ -736,7 +735,7 @@ pub extern "C" fn rs_dns_state_tcp_new() -> *mut std::os::raw::c_void { #[no_mangle] pub extern "C" fn rs_dns_state_free(state: *mut std::os::raw::c_void) { // Just unbox... - let _drop: Box = unsafe{transmute(state)}; + std::mem::drop(unsafe { Box::from_raw(state as *mut DNSState) }); } #[no_mangle] @@ -855,7 +854,7 @@ pub extern "C" fn rs_dns_state_get_tx(state: *mut std::os::raw::c_void, let state = cast_pointer!(state, DNSState); match state.get_tx(tx_id) { Some(tx) => { - return unsafe{transmute(tx)}; + return tx as *const _ as *mut _; } None => { return std::ptr::null_mut(); diff --git a/rust/src/http2/detect.rs b/rust/src/http2/detect.rs index 40f4ec7e71bd..3248b897cbf6 100644 --- a/rust/src/http2/detect.rs +++ b/rust/src/http2/detect.rs @@ -21,7 +21,6 @@ use super::http2::{ use super::parser; use crate::core::STREAM_TOSERVER; use std::ffi::CStr; -use std::mem::transmute; use std::str::FromStr; fn http2_tx_has_frametype( @@ -234,7 +233,7 @@ pub unsafe extern "C" fn rs_http2_detect_settingsctx_parse( if let Ok(s) = ft_name.to_str() { if let Ok((_, ctx)) = parser::http2_parse_settingsctx(s) { let boxed = Box::new(ctx); - return transmute(boxed); //unsafe + return Box::into_raw(boxed) as *mut _; } } return std::ptr::null_mut(); @@ -243,7 +242,7 @@ pub unsafe extern "C" fn rs_http2_detect_settingsctx_parse( #[no_mangle] pub unsafe extern "C" fn rs_http2_detect_settingsctx_free(ctx: *mut std::os::raw::c_void) { // Just unbox... - let _ctx: Box = transmute(ctx); + std::mem::drop(Box::from_raw(ctx as *mut parser::DetectHTTP2settingsSigCtx)); } fn http2_detect_settings_match( @@ -329,7 +328,7 @@ pub unsafe extern "C" fn rs_detect_u64_parse( if let Ok(s) = ft_name.to_str() { if let Ok((_, ctx)) = parser::detect_parse_u64(s) { let boxed = Box::new(ctx); - return transmute(boxed); //unsafe + return Box::into_raw(boxed) as *mut _; } } return std::ptr::null_mut(); @@ -338,7 +337,7 @@ pub unsafe extern "C" fn rs_detect_u64_parse( #[no_mangle] pub unsafe extern "C" fn rs_detect_u64_free(ctx: *mut std::os::raw::c_void) { // Just unbox... - let _ctx: Box = transmute(ctx); + std::mem::drop(Box::from_raw(ctx as *mut parser::DetectU64Data)); } fn http2_detect_sizeupdate_match( diff --git a/rust/src/http2/http2.rs b/rust/src/http2/http2.rs index efa1802e1a2a..c11e9d3ec64e 100644 --- a/rust/src/http2/http2.rs +++ b/rust/src/http2/http2.rs @@ -29,7 +29,6 @@ use std; use std::ffi::{CStr, CString}; use std::fmt; use std::io; -use std::mem::transmute; static mut ALPROTO_HTTP2: AppProto = ALPROTO_UNKNOWN; @@ -1004,7 +1003,7 @@ pub extern "C" fn rs_http2_state_new( ) -> *mut std::os::raw::c_void { let state = HTTP2State::new(); let boxed = Box::new(state); - let r = unsafe { transmute(boxed) }; + let r = Box::into_raw(boxed) as *mut _; if orig_state != std::ptr::null_mut() { //we could check ALPROTO_HTTP1 == orig_proto unsafe { @@ -1016,8 +1015,7 @@ pub extern "C" fn rs_http2_state_new( #[no_mangle] pub extern "C" fn rs_http2_state_free(state: *mut std::os::raw::c_void) { - // Just unbox... - let mut state: Box = unsafe { transmute(state) }; + let mut state: Box = unsafe { Box::from_raw(state as _) }; state.free(); } @@ -1059,7 +1057,7 @@ pub extern "C" fn rs_http2_state_get_tx( let state = cast_pointer!(state, HTTP2State); match state.get_tx(tx_id) { Some(tx) => { - return unsafe { transmute(tx) }; + return tx as *const _ as *mut _; } None => { return std::ptr::null_mut(); @@ -1163,7 +1161,7 @@ pub extern "C" fn rs_http2_state_get_tx_iterator( let state = cast_pointer!(state, HTTP2State); match state.tx_iterator(min_tx_id, istate) { Some((tx, out_tx_id, has_next)) => { - let c_tx = unsafe { transmute(tx) }; + let c_tx = tx as *const _ as *mut _; let ires = applayer::AppLayerGetTxIterTuple::with_values(c_tx, out_tx_id, has_next); return ires; } diff --git a/rust/src/ike/ike.rs b/rust/src/ike/ike.rs index bcd6ffcec6d6..0a9b4fc07d99 100644 --- a/rust/src/ike/ike.rs +++ b/rust/src/ike/ike.rs @@ -32,7 +32,6 @@ use nom; use std; use std::collections::HashSet; use std::ffi::{CStr, CString}; -use std::mem::transmute; #[repr(u32)] pub enum IkeEvent { @@ -346,13 +345,13 @@ pub extern "C" fn rs_ike_state_new( ) -> *mut std::os::raw::c_void { let state = IKEState::default(); let boxed = Box::new(state); - return unsafe { transmute(boxed) }; + return Box::into_raw(boxed) as *mut _; } #[no_mangle] pub extern "C" fn rs_ike_state_free(state: *mut std::os::raw::c_void) { // Just unbox... - let _drop: Box = unsafe { transmute(state) }; + std::mem::drop(unsafe { Box::from_raw(state as *mut IKEState) }); } #[no_mangle] @@ -389,7 +388,7 @@ pub extern "C" fn rs_ike_state_get_tx( let state = cast_pointer!(state, IKEState); match state.get_tx(tx_id) { Some(tx) => { - return unsafe { transmute(tx) }; + return tx as *const _ as *mut _; } None => { return std::ptr::null_mut(); @@ -516,7 +515,7 @@ pub extern "C" fn rs_ike_state_get_tx_iterator( let state = cast_pointer!(state, IKEState); match state.tx_iterator(min_tx_id, istate) { Some((tx, out_tx_id, has_next)) => { - let c_tx = unsafe { transmute(tx) }; + let c_tx = tx as *const _ as *mut _; let ires = applayer::AppLayerGetTxIterTuple::with_values(c_tx, out_tx_id, has_next); return ires; } diff --git a/rust/src/krb/krb5.rs b/rust/src/krb/krb5.rs index 389bbc7b3cdc..564266c0f815 100644 --- a/rust/src/krb/krb5.rs +++ b/rust/src/krb/krb5.rs @@ -276,15 +276,14 @@ pub fn test_weak_encryption(alg:EncryptionType) -> bool { pub extern "C" fn rs_krb5_state_new(_orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto) -> *mut std::os::raw::c_void { let state = KRB5State::new(); let boxed = Box::new(state); - return unsafe{std::mem::transmute(boxed)}; + return Box::into_raw(boxed) as *mut _; } /// Params: /// - state: *mut KRB5State as void pointer #[no_mangle] pub extern "C" fn rs_krb5_state_free(state: *mut std::os::raw::c_void) { - // Just unbox... - let mut state: Box = unsafe{std::mem::transmute(state)}; + let mut state: Box = unsafe{Box::from_raw(state as _)}; state.free(); } @@ -295,7 +294,7 @@ pub extern "C" fn rs_krb5_state_get_tx(state: *mut std::os::raw::c_void, { let state = cast_pointer!(state,KRB5State); match state.get_tx_by_id(tx_id) { - Some(tx) => unsafe{std::mem::transmute(tx)}, + Some(tx) => tx as *const _ as *mut _, None => std::ptr::null_mut(), } } diff --git a/rust/src/mqtt/mqtt.rs b/rust/src/mqtt/mqtt.rs index fab018eda200..1ced80ee34d9 100644 --- a/rust/src/mqtt/mqtt.rs +++ b/rust/src/mqtt/mqtt.rs @@ -26,7 +26,6 @@ use num_traits::FromPrimitive; use nom; use std; use std::ffi::{CStr,CString}; -use std::mem::transmute; // Used as a special pseudo packet identifier to denote the first CONNECT // packet in a connection. Note that there is no risk of collision with a @@ -581,12 +580,12 @@ pub extern "C" fn rs_mqtt_probing_parser( pub extern "C" fn rs_mqtt_state_new(_orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto) -> *mut std::os::raw::c_void { let state = MQTTState::new(); let boxed = Box::new(state); - return unsafe { transmute(boxed) }; + return Box::into_raw(boxed) as *mut _; } #[no_mangle] pub extern "C" fn rs_mqtt_state_free(state: *mut std::os::raw::c_void) { - let _drop: Box = unsafe { transmute(state) }; + std::mem::drop(unsafe { Box::from_raw(state as *mut MQTTState) }); } #[no_mangle] @@ -633,7 +632,7 @@ pub extern "C" fn rs_mqtt_state_get_tx( let state = cast_pointer!(state, MQTTState); match state.get_tx(tx_id) { Some(tx) => { - return unsafe { transmute(tx) }; + return tx as *const _ as *mut _; } None => { return std::ptr::null_mut(); @@ -775,7 +774,7 @@ pub extern "C" fn rs_mqtt_state_get_tx_iterator( let state = cast_pointer!(state, MQTTState); match state.tx_iterator(min_tx_id, istate) { Some((tx, out_tx_id, has_next)) => { - let c_tx = unsafe { transmute(tx) }; + let c_tx = tx as *const _ as *mut _; let ires = applayer::AppLayerGetTxIterTuple::with_values(c_tx, out_tx_id, has_next); return ires; } diff --git a/rust/src/nfs/nfs.rs b/rust/src/nfs/nfs.rs index 969ed29185a0..2c3f18f7c226 100644 --- a/rust/src/nfs/nfs.rs +++ b/rust/src/nfs/nfs.rs @@ -19,7 +19,6 @@ use std; use std::cmp; -use std::mem::transmute; use std::collections::{HashMap}; use std::ffi::{CStr, CString}; @@ -1377,7 +1376,7 @@ pub extern "C" fn rs_nfs_state_new(_orig_state: *mut std::os::raw::c_void, _orig let state = NFSState::new(); let boxed = Box::new(state); SCLogDebug!("allocating state"); - return unsafe{transmute(boxed)}; + return Box::into_raw(boxed) as *mut _; } /// Params: @@ -1386,7 +1385,7 @@ pub extern "C" fn rs_nfs_state_new(_orig_state: *mut std::os::raw::c_void, _orig pub extern "C" fn rs_nfs_state_free(state: *mut std::os::raw::c_void) { // Just unbox... SCLogDebug!("freeing state"); - let mut _nfs_state: Box = unsafe{transmute(state)}; + std::mem::drop(unsafe { Box::from_raw(state as *mut NFSState) }); } /// C binding parse a NFS TCP request. Returns 1 on success, -1 on failure. @@ -1511,7 +1510,7 @@ pub extern "C" fn rs_nfs_state_get_tx(state: *mut std::os::raw::c_void, let state = cast_pointer!(state, NFSState); match state.get_tx_by_id(tx_id) { Some(tx) => { - return unsafe{transmute(tx)}; + return tx as *const _ as *mut _; } None => { return std::ptr::null_mut(); @@ -1533,7 +1532,7 @@ pub extern "C" fn rs_nfs_state_get_tx_iterator( let state = cast_pointer!(state, NFSState); match state.get_tx_iterator(min_tx_id, istate) { Some((tx, out_tx_id, has_next)) => { - let c_tx = unsafe { transmute(tx) }; + let c_tx = tx as *const _ as *mut _; let ires = applayer::AppLayerGetTxIterTuple::with_values(c_tx, out_tx_id, has_next); return ires; } diff --git a/rust/src/ntp/ntp.rs b/rust/src/ntp/ntp.rs index 02e9fdca9bf6..7d6ce269d1f4 100644 --- a/rust/src/ntp/ntp.rs +++ b/rust/src/ntp/ntp.rs @@ -178,15 +178,14 @@ impl Drop for NTPTransaction { pub extern "C" fn rs_ntp_state_new(_orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto) -> *mut std::os::raw::c_void { let state = NTPState::new(); let boxed = Box::new(state); - return unsafe{std::mem::transmute(boxed)}; + return Box::into_raw(boxed) as *mut _; } /// Params: /// - state: *mut NTPState as void pointer #[no_mangle] pub extern "C" fn rs_ntp_state_free(state: *mut std::os::raw::c_void) { - // Just unbox... - let mut ntp_state: Box = unsafe{std::mem::transmute(state)}; + let mut ntp_state = unsafe{ Box::from_raw(state as *mut NTPState) }; ntp_state.free(); } @@ -229,7 +228,7 @@ pub extern "C" fn rs_ntp_state_get_tx(state: *mut std::os::raw::c_void, { let state = cast_pointer!(state,NTPState); match state.get_tx_by_id(tx_id) { - Some(tx) => unsafe{std::mem::transmute(tx)}, + Some(tx) => tx as *const _ as *mut _, None => std::ptr::null_mut(), } } diff --git a/rust/src/rdp/rdp.rs b/rust/src/rdp/rdp.rs index 55f2ddd3ecf5..6f578189fec3 100644 --- a/rust/src/rdp/rdp.rs +++ b/rust/src/rdp/rdp.rs @@ -24,7 +24,6 @@ use crate::core::{self, AppProto, DetectEngineState, Flow, ALPROTO_UNKNOWN, IPPR use crate::rdp::parser::*; use nom; use std; -use std::mem::transmute; use tls_parser::{parse_tls_plaintext, TlsMessage, TlsMessageHandshake, TlsRecordType}; static mut ALPROTO_RDP: AppProto = ALPROTO_UNKNOWN; @@ -86,7 +85,7 @@ pub extern "C" fn rs_rdp_state_get_tx( let state = cast_pointer!(state, RdpState); match state.get_tx(tx_id) { Some(tx) => { - return unsafe { transmute(tx) }; + return tx as *const _ as *mut _; } None => { return std::ptr::null_mut(); @@ -376,12 +375,12 @@ impl RdpState { pub extern "C" fn rs_rdp_state_new(_orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto) -> *mut std::os::raw::c_void { let state = RdpState::new(); let boxed = Box::new(state); - return unsafe { std::mem::transmute(boxed) }; + return Box::into_raw(boxed) as *mut _; } #[no_mangle] pub extern "C" fn rs_rdp_state_free(state: *mut std::os::raw::c_void) { - let _drop: Box = unsafe { std::mem::transmute(state) }; + std::mem::drop(unsafe { Box::from_raw(state as *mut RdpState) }); } #[no_mangle] diff --git a/rust/src/rfb/rfb.rs b/rust/src/rfb/rfb.rs index 3bbc33055145..1b0c5019da53 100644 --- a/rust/src/rfb/rfb.rs +++ b/rust/src/rfb/rfb.rs @@ -19,7 +19,6 @@ use std; use std::ffi::CString; -use std::mem::transmute; use crate::core::{self, ALPROTO_UNKNOWN, AppProto, Flow, IPPROTO_TCP}; use crate::applayer; use crate::applayer::*; @@ -521,13 +520,13 @@ export_tx_set_detect_state!( pub extern "C" fn rs_rfb_state_new(_orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto) -> *mut std::os::raw::c_void { let state = RFBState::new(); let boxed = Box::new(state); - return unsafe { transmute(boxed) }; + return Box::into_raw(boxed) as *mut _; } #[no_mangle] pub extern "C" fn rs_rfb_state_free(state: *mut std::os::raw::c_void) { // Just unbox... - let _drop: Box = unsafe { transmute(state) }; + std::mem::drop(unsafe { Box::from_raw(state as *mut RFBState) }); } #[no_mangle] @@ -577,7 +576,7 @@ pub extern "C" fn rs_rfb_state_get_tx( let state = cast_pointer!(state, RFBState); match state.get_tx(tx_id) { Some(tx) => { - return unsafe { transmute(tx) }; + return tx as *const _ as *mut _; } None => { return std::ptr::null_mut(); @@ -641,7 +640,7 @@ pub extern "C" fn rs_rfb_state_get_tx_iterator( let state = cast_pointer!(state, RFBState); match state.tx_iterator(min_tx_id, istate) { Some((tx, out_tx_id, has_next)) => { - let c_tx = unsafe { transmute(tx) }; + let c_tx = tx as *const _ as *mut _; let ires = applayer::AppLayerGetTxIterTuple::with_values( c_tx, out_tx_id, diff --git a/rust/src/sip/sip.rs b/rust/src/sip/sip.rs index 8874bdccef8e..4cc8bf8e21b6 100755 --- a/rust/src/sip/sip.rs +++ b/rust/src/sip/sip.rs @@ -172,12 +172,12 @@ impl Drop for SIPTransaction { pub extern "C" fn rs_sip_state_new(_orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto) -> *mut std::os::raw::c_void { let state = SIPState::new(); let boxed = Box::new(state); - return unsafe { std::mem::transmute(boxed) }; + return Box::into_raw(boxed) as *mut _; } #[no_mangle] pub extern "C" fn rs_sip_state_free(state: *mut std::os::raw::c_void) { - let mut state: Box = unsafe { std::mem::transmute(state) }; + let mut state = unsafe { Box::from_raw(state as *mut SIPState) }; state.free(); } @@ -188,7 +188,7 @@ pub extern "C" fn rs_sip_state_get_tx( ) -> *mut std::os::raw::c_void { let state = cast_pointer!(state, SIPState); match state.get_tx_by_id(tx_id) { - Some(tx) => unsafe { std::mem::transmute(tx) }, + Some(tx) => tx as *const _ as *mut _, None => std::ptr::null_mut(), } } diff --git a/rust/src/smb/smb.rs b/rust/src/smb/smb.rs index 4a0ff9adc84f..f2f091b616ff 100644 --- a/rust/src/smb/smb.rs +++ b/rust/src/smb/smb.rs @@ -26,7 +26,6 @@ // written by Victor Julien use std; -use std::mem::transmute; use std::str; use std::ffi::{self, CStr, CString}; @@ -1798,16 +1797,15 @@ pub extern "C" fn rs_smb_state_new(_orig_state: *mut std::os::raw::c_void, _orig let state = SMBState::new(); let boxed = Box::new(state); SCLogDebug!("allocating state"); - return unsafe{transmute(boxed)}; + return Box::into_raw(boxed) as *mut _; } /// Params: /// - state: *mut SMBState as void pointer #[no_mangle] pub extern "C" fn rs_smb_state_free(state: *mut std::os::raw::c_void) { - // Just unbox... SCLogDebug!("freeing state"); - let mut smb_state: Box = unsafe{transmute(state)}; + let mut smb_state = unsafe { Box::from_raw(state as *mut SMBState) }; smb_state.free(); } @@ -2021,7 +2019,7 @@ pub extern "C" fn rs_smb_state_get_tx(state: *mut ffi::c_void, let state = cast_pointer!(state, SMBState); match state.get_tx_by_id(tx_id) { Some(tx) => { - return unsafe{transmute(tx)}; + return tx as *const _ as *mut _; } None => { return std::ptr::null_mut(); @@ -2043,7 +2041,7 @@ pub extern "C" fn rs_smb_state_get_tx_iterator( let state = cast_pointer!(state, SMBState); match state.get_tx_iterator(min_tx_id, istate) { Some((tx, out_tx_id, has_next)) => { - let c_tx = unsafe { transmute(tx) }; + let c_tx = tx as *const _ as *mut _; let ires = applayer::AppLayerGetTxIterTuple::with_values(c_tx, out_tx_id, has_next); return ires; } diff --git a/rust/src/snmp/snmp.rs b/rust/src/snmp/snmp.rs index 88715ccb8122..3ad187593f05 100644 --- a/rust/src/snmp/snmp.rs +++ b/rust/src/snmp/snmp.rs @@ -23,7 +23,6 @@ use crate::core::{AppProto,Flow,ALPROTO_UNKNOWN,ALPROTO_FAILED,STREAM_TOSERVER,S use crate::applayer::{self, *}; use std; use std::ffi::{CStr,CString}; -use std::mem::transmute; use der_parser::ber::BerObjectContent; use der_parser::der::parse_der_sequence; @@ -300,15 +299,14 @@ impl<'a> Drop for SNMPTransaction<'a> { pub extern "C" fn rs_snmp_state_new(_orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto) -> *mut std::os::raw::c_void { let state = SNMPState::new(); let boxed = Box::new(state); - return unsafe{std::mem::transmute(boxed)}; + return Box::into_raw(boxed) as *mut _; } /// Params: /// - state: *mut SNMPState as void pointer #[no_mangle] pub extern "C" fn rs_snmp_state_free(state: *mut std::os::raw::c_void) { - // Just unbox... - let mut snmp_state: Box = unsafe{std::mem::transmute(state)}; + let mut snmp_state = unsafe{ Box::from_raw(state as *mut SNMPState) }; snmp_state.free(); } @@ -345,7 +343,7 @@ pub extern "C" fn rs_snmp_state_get_tx(state: *mut std::os::raw::c_void, { let state = cast_pointer!(state,SNMPState); match state.get_tx_by_id(tx_id) { - Some(tx) => unsafe{std::mem::transmute(tx)}, + Some(tx) => tx as *const _ as *mut _, None => std::ptr::null_mut(), } } @@ -463,7 +461,7 @@ pub extern "C" fn rs_snmp_state_get_tx_iterator( { match state.get_tx_iterator(min_tx_id, istate) { Some((tx, out_tx_id, has_next)) => { - let c_tx = unsafe { transmute(tx) }; + let c_tx = tx as *const _ as *mut _; let ires = applayer::AppLayerGetTxIterTuple::with_values(c_tx, out_tx_id, has_next); return ires; } @@ -485,7 +483,7 @@ pub extern "C" fn rs_snmp_get_tx_iterator(_ipproto: u8, let state = cast_pointer!(alstate,SNMPState); match state.get_tx_iterator(min_tx_id, istate) { Some((tx, out_tx_id, has_next)) => { - let c_tx = unsafe { transmute(tx) }; + let c_tx = tx as *const _ as *mut _; let ires = applayer::AppLayerGetTxIterTuple::with_values(c_tx, out_tx_id, has_next); return ires; } diff --git a/rust/src/ssh/ssh.rs b/rust/src/ssh/ssh.rs index 196e18e7e517..d79f9860f61f 100644 --- a/rust/src/ssh/ssh.rs +++ b/rust/src/ssh/ssh.rs @@ -20,7 +20,6 @@ use crate::applayer::*; use crate::core::STREAM_TOSERVER; use crate::core::{self, AppProto, Flow, ALPROTO_UNKNOWN, IPPROTO_TCP}; use std::ffi::{CStr, CString}; -use std::mem::transmute; use std::sync::atomic::{AtomicBool, Ordering}; static mut ALPROTO_SSH: AppProto = ALPROTO_UNKNOWN; @@ -431,13 +430,12 @@ pub extern "C" fn rs_ssh_state_get_event_info_by_id( pub extern "C" fn rs_ssh_state_new(_orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto) -> *mut std::os::raw::c_void { let state = SSHState::new(); let boxed = Box::new(state); - return unsafe { transmute(boxed) }; + return Box::into_raw(boxed) as *mut _; } #[no_mangle] pub extern "C" fn rs_ssh_state_free(state: *mut std::os::raw::c_void) { - // Just unbox... - let _drop: Box = unsafe { transmute(state) }; + std::mem::drop(unsafe { Box::from_raw(state as *mut SSHState) }); } #[no_mangle] @@ -480,7 +478,7 @@ pub extern "C" fn rs_ssh_state_get_tx( state: *mut std::os::raw::c_void, _tx_id: u64, ) -> *mut std::os::raw::c_void { let state = cast_pointer!(state, SSHState); - return unsafe { transmute(&state.transaction) }; + return &state.transaction as *const _ as *mut _; } #[no_mangle] diff --git a/rust/src/tftp/tftp.rs b/rust/src/tftp/tftp.rs index 361c8e15cbc8..977f95c7b4bb 100644 --- a/rust/src/tftp/tftp.rs +++ b/rust/src/tftp/tftp.rs @@ -21,7 +21,6 @@ extern crate nom; use std::str; use std; -use std::mem::transmute; use nom::*; use crate::applayer::AppLayerTxData; @@ -89,12 +88,12 @@ impl TFTPTransaction { pub extern "C" fn rs_tftp_state_alloc() -> *mut std::os::raw::c_void { let state = TFTPState { transactions : Vec::new(), tx_id: 0, }; let boxed = Box::new(state); - return unsafe{transmute(boxed)}; + return Box::into_raw(boxed) as *mut _; } #[no_mangle] pub extern "C" fn rs_tftp_state_free(state: *mut std::os::raw::c_void) { - let _state : Box = unsafe{transmute(state)}; + std::mem::drop(unsafe { Box::from_raw(state as *mut TFTPState) }); } #[no_mangle] @@ -107,7 +106,7 @@ pub extern "C" fn rs_tftp_state_tx_free(state: &mut TFTPState, pub extern "C" fn rs_tftp_get_tx(state: &mut TFTPState, tx_id: u64) -> *mut std::os::raw::c_void { match state.get_tx_by_id(tx_id) { - Some(tx) => unsafe{std::mem::transmute(tx)}, + Some(tx) => tx as *const _ as *mut _, None => std::ptr::null_mut(), } } From 363b5f99c36fda06f185e446ca28dff6da81fbcc Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Mon, 26 Jul 2021 15:53:44 -0600 Subject: [PATCH 11/24] rust: functions that reference raw pointers are unsafe Based on the Rust clippy lint that recommends that any public function that dereferences a raw pointer, mark all FFI functions that reference raw pointers with build_slice and cast_pointer as unsafe. This commits starts by removing the unsafe wrapper inside the build_slice and cast_pointer macros then marks all functions that use these macros as unsafe. Then fix all not_unsafe_ptr_arg_deref warnings from clippy. Fixes clippy lint: https://rust-lang.github.io/rust-clippy/master/index.html#not_unsafe_ptr_arg_deref --- rust/src/applayer.rs | 39 ++--- rust/src/applayertemplate/logger.rs | 2 +- rust/src/applayertemplate/template.rs | 60 ++++---- rust/src/asn1/mod.rs | 4 +- rust/src/dcerpc/dcerpc.rs | 32 ++--- rust/src/dcerpc/dcerpc_udp.rs | 26 ++-- rust/src/dhcp/dhcp.rs | 38 +++-- rust/src/dhcp/logger.rs | 8 +- rust/src/dns/dns.rs | 88 +++++------- rust/src/ftp/mod.rs | 12 +- rust/src/http2/detect.rs | 18 +-- rust/src/http2/http2.rs | 57 ++++---- rust/src/http2/logger.rs | 2 +- rust/src/ike/ike.rs | 48 +++---- rust/src/ike/logger.rs | 2 +- rust/src/krb/krb5.rs | 62 ++++---- rust/src/modbus/modbus.rs | 54 ++++--- rust/src/mqtt/detect.rs | 198 ++++++++++---------------- rust/src/mqtt/logger.rs | 2 +- rust/src/mqtt/mqtt.rs | 50 +++---- rust/src/nfs/nfs.rs | 116 +++++++-------- rust/src/ntp/ntp.rs | 34 ++--- rust/src/rdp/rdp.rs | 14 +- rust/src/rfb/logger.rs | 2 +- rust/src/rfb/rfb.rs | 16 +-- rust/src/sip/sip.rs | 42 +++--- rust/src/smb/detect.rs | 48 +++---- rust/src/smb/files.rs | 6 +- rust/src/smb/smb.rs | 76 +++++----- rust/src/snmp/detect.rs | 30 ++-- rust/src/snmp/snmp.rs | 44 +++--- rust/src/ssh/detect.rs | 80 ++++------- rust/src/ssh/logger.rs | 2 +- rust/src/ssh/ssh.rs | 36 +++-- rust/src/tftp/tftp.rs | 6 +- rust/src/x509/mod.rs | 6 +- 36 files changed, 594 insertions(+), 766 deletions(-) diff --git a/rust/src/applayer.rs b/rust/src/applayer.rs index f4a15b0c0b2b..58bb36723eea 100644 --- a/rust/src/applayer.rs +++ b/rust/src/applayer.rs @@ -238,7 +238,7 @@ pub struct RustParser { /// UNSAFE ! #[macro_export] macro_rules! build_slice { - ($buf:ident, $len:expr) => ( unsafe{ std::slice::from_raw_parts($buf, $len) } ); + ($buf:ident, $len:expr) => ( std::slice::from_raw_parts($buf, $len) ); } /// Cast pointer to a variable, as a mutable reference to an object @@ -246,32 +246,33 @@ macro_rules! build_slice { /// UNSAFE ! #[macro_export] macro_rules! cast_pointer { - ($ptr:ident, $ty:ty) => ( unsafe{ &mut *($ptr as *mut $ty) } ); + ($ptr:ident, $ty:ty) => ( &mut *($ptr as *mut $ty) ); } -pub type ParseFn = extern "C" fn (flow: *const Flow, +pub type ParseFn = unsafe extern "C" fn (flow: *const Flow, state: *mut c_void, pstate: *mut c_void, input: *const u8, input_len: u32, data: *const c_void, flags: u8) -> AppLayerResult; -pub type ProbeFn = extern "C" fn (flow: *const Flow, flags: u8, input:*const u8, input_len: u32, rdir: *mut u8) -> AppProto; +pub type ProbeFn = unsafe extern "C" fn (flow: *const Flow, flags: u8, input:*const u8, input_len: u32, rdir: *mut u8) -> AppProto; pub type StateAllocFn = extern "C" fn (*mut c_void, AppProto) -> *mut c_void; -pub type StateFreeFn = extern "C" fn (*mut c_void); -pub type StateTxFreeFn = extern "C" fn (*mut c_void, u64); -pub type StateGetTxFn = extern "C" fn (*mut c_void, u64) -> *mut c_void; -pub type StateGetTxCntFn = extern "C" fn (*mut c_void) -> u64; -pub type StateGetProgressFn = extern "C" fn (*mut c_void, u8) -> c_int; -pub type GetDetectStateFn = extern "C" fn (*mut c_void) -> *mut DetectEngineState; -pub type SetDetectStateFn = extern "C" fn (*mut c_void, &mut DetectEngineState) -> c_int; -pub type GetEventInfoFn = extern "C" fn (*const c_char, *mut c_int, *mut AppLayerEventType) -> c_int; -pub type GetEventInfoByIdFn = extern "C" fn (c_int, *mut *const c_char, *mut AppLayerEventType) -> i8; -pub type GetEventsFn = extern "C" fn (*mut c_void) -> *mut AppLayerDecoderEvents; +pub type StateFreeFn = unsafe extern "C" fn (*mut c_void); +pub type StateTxFreeFn = unsafe extern "C" fn (*mut c_void, u64); +pub type StateGetTxFn = unsafe extern "C" fn (*mut c_void, u64) -> *mut c_void; +pub type StateGetTxCntFn = unsafe extern "C" fn (*mut c_void) -> u64; +pub type StateGetProgressFn = unsafe extern "C" fn (*mut c_void, u8) -> c_int; +pub type GetDetectStateFn = unsafe extern "C" fn (*mut c_void) -> *mut DetectEngineState; +pub type SetDetectStateFn = unsafe extern "C" fn (*mut c_void, &mut DetectEngineState) -> c_int; +pub type GetEventInfoFn = unsafe extern "C" fn (*const c_char, *mut c_int, *mut AppLayerEventType) -> c_int; +pub type GetEventInfoByIdFn = unsafe extern "C" fn (c_int, *mut *const c_char, *mut AppLayerEventType) -> i8; +pub type GetEventsFn = unsafe extern "C" fn (*mut c_void) -> *mut AppLayerDecoderEvents; pub type LocalStorageNewFn = extern "C" fn () -> *mut c_void; pub type LocalStorageFreeFn = extern "C" fn (*mut c_void); -pub type GetFilesFn = extern "C" fn (*mut c_void, u8) -> *mut FileContainer; -pub type GetTxIteratorFn = extern "C" fn (ipproto: u8, alproto: AppProto, +pub type GetFilesFn = unsafe +extern "C" fn (*mut c_void, u8) -> *mut FileContainer; +pub type GetTxIteratorFn = unsafe extern "C" fn (ipproto: u8, alproto: AppProto, state: *mut c_void, min_tx_id: u64, max_tx_id: u64, @@ -320,7 +321,7 @@ pub const APP_LAYER_PARSER_BYPASS_READY : u8 = BIT_U8!(4); pub const APP_LAYER_PARSER_OPT_ACCEPT_GAPS: u32 = BIT_U32!(0); pub const APP_LAYER_PARSER_OPT_UNIDIR_TXS: u32 = BIT_U32!(1); -pub type AppLayerGetTxIteratorFn = extern "C" fn (ipproto: u8, +pub type AppLayerGetTxIteratorFn = unsafe extern "C" fn (ipproto: u8, alproto: AppProto, alstate: *mut c_void, min_tx_id: u64, @@ -384,7 +385,7 @@ impl LoggerFlags { macro_rules!export_tx_get_detect_state { ($name:ident, $type:ty) => ( #[no_mangle] - pub extern "C" fn $name(tx: *mut std::os::raw::c_void) + pub unsafe extern "C" fn $name(tx: *mut std::os::raw::c_void) -> *mut core::DetectEngineState { let tx = cast_pointer!(tx, $type); @@ -405,7 +406,7 @@ macro_rules!export_tx_get_detect_state { macro_rules!export_tx_set_detect_state { ($name:ident, $type:ty) => ( #[no_mangle] - pub extern "C" fn $name(tx: *mut std::os::raw::c_void, + pub unsafe extern "C" fn $name(tx: *mut std::os::raw::c_void, de_state: &mut core::DetectEngineState) -> std::os::raw::c_int { let tx = cast_pointer!(tx, $type); diff --git a/rust/src/applayertemplate/logger.rs b/rust/src/applayertemplate/logger.rs index 9c1278350f70..ef4b616f0e08 100644 --- a/rust/src/applayertemplate/logger.rs +++ b/rust/src/applayertemplate/logger.rs @@ -30,7 +30,7 @@ fn log_template(tx: &TemplateTransaction, js: &mut JsonBuilder) -> Result<(), Js } #[no_mangle] -pub extern "C" fn rs_template_logger_log(tx: *mut std::os::raw::c_void, js: &mut JsonBuilder) -> bool { +pub unsafe extern "C" fn rs_template_logger_log(tx: *mut std::os::raw::c_void, js: &mut JsonBuilder) -> bool { let tx = cast_pointer!(tx, TemplateTransaction); log_template(tx, js).is_ok() } diff --git a/rust/src/applayertemplate/template.rs b/rust/src/applayertemplate/template.rs index 36f6d3b79055..25482cc4d410 100644 --- a/rust/src/applayertemplate/template.rs +++ b/rust/src/applayertemplate/template.rs @@ -276,7 +276,7 @@ export_tx_set_detect_state!( /// C entry point for a probing parser. #[no_mangle] -pub extern "C" fn rs_template_probing_parser( +pub unsafe extern "C" fn rs_template_probing_parser( _flow: *const Flow, _direction: u8, input: *const u8, @@ -287,7 +287,7 @@ pub extern "C" fn rs_template_probing_parser( if input_len > 1 && input != std::ptr::null_mut() { let slice = build_slice!(input, input_len as usize); if probe(slice).is_ok() { - return unsafe { ALPROTO_TEMPLATE }; + return ALPROTO_TEMPLATE; } } return ALPROTO_UNKNOWN; @@ -301,12 +301,12 @@ pub extern "C" fn rs_template_state_new(_orig_state: *mut std::os::raw::c_void, } #[no_mangle] -pub extern "C" fn rs_template_state_free(state: *mut std::os::raw::c_void) { - std::mem::drop(unsafe { Box::from_raw(state as *mut TemplateState) }); +pub unsafe extern "C" fn rs_template_state_free(state: *mut std::os::raw::c_void) { + std::mem::drop(Box::from_raw(state as *mut TemplateState)); } #[no_mangle] -pub extern "C" fn rs_template_state_tx_free( +pub unsafe extern "C" fn rs_template_state_tx_free( state: *mut std::os::raw::c_void, tx_id: u64, ) { @@ -315,7 +315,7 @@ pub extern "C" fn rs_template_state_tx_free( } #[no_mangle] -pub extern "C" fn rs_template_parse_request( +pub unsafe extern "C" fn rs_template_parse_request( _flow: *const Flow, state: *mut std::os::raw::c_void, pstate: *mut std::os::raw::c_void, @@ -324,12 +324,10 @@ pub extern "C" fn rs_template_parse_request( _data: *const std::os::raw::c_void, _flags: u8, ) -> AppLayerResult { - let eof = unsafe { - if AppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TS) > 0 { - true - } else { - false - } + let eof = if AppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TS) > 0 { + true + } else { + false }; if eof { @@ -351,7 +349,7 @@ pub extern "C" fn rs_template_parse_request( } #[no_mangle] -pub extern "C" fn rs_template_parse_response( +pub unsafe extern "C" fn rs_template_parse_response( _flow: *const Flow, state: *mut std::os::raw::c_void, pstate: *mut std::os::raw::c_void, @@ -360,12 +358,10 @@ pub extern "C" fn rs_template_parse_response( _data: *const std::os::raw::c_void, _flags: u8, ) -> AppLayerResult { - let _eof = unsafe { - if AppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TC) > 0 { - true - } else { - false - } + let _eof = if AppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TC) > 0 { + true + } else { + false }; let state = cast_pointer!(state, TemplateState); @@ -381,7 +377,7 @@ pub extern "C" fn rs_template_parse_response( } #[no_mangle] -pub extern "C" fn rs_template_state_get_tx( +pub unsafe extern "C" fn rs_template_state_get_tx( state: *mut std::os::raw::c_void, tx_id: u64, ) -> *mut std::os::raw::c_void { @@ -397,7 +393,7 @@ pub extern "C" fn rs_template_state_get_tx( } #[no_mangle] -pub extern "C" fn rs_template_state_get_tx_count( +pub unsafe extern "C" fn rs_template_state_get_tx_count( state: *mut std::os::raw::c_void, ) -> u64 { let state = cast_pointer!(state, TemplateState); @@ -405,7 +401,7 @@ pub extern "C" fn rs_template_state_get_tx_count( } #[no_mangle] -pub extern "C" fn rs_template_tx_get_alstate_progress( +pub unsafe extern "C" fn rs_template_tx_get_alstate_progress( tx: *mut std::os::raw::c_void, _direction: u8, ) -> std::os::raw::c_int { @@ -419,7 +415,7 @@ pub extern "C" fn rs_template_tx_get_alstate_progress( } #[no_mangle] -pub extern "C" fn rs_template_state_get_events( +pub unsafe extern "C" fn rs_template_state_get_events( tx: *mut std::os::raw::c_void ) -> *mut core::AppLayerDecoderEvents { let tx = cast_pointer!(tx, TemplateTransaction); @@ -443,7 +439,7 @@ pub extern "C" fn rs_template_state_get_event_info_by_id(_event_id: std::os::raw return -1; } #[no_mangle] -pub extern "C" fn rs_template_state_get_tx_iterator( +pub unsafe extern "C" fn rs_template_state_get_tx_iterator( _ipproto: u8, _alproto: AppProto, state: *mut std::os::raw::c_void, @@ -473,7 +469,7 @@ pub extern "C" fn rs_template_state_get_tx_iterator( /// No required for parsing, but an example function for retrieving a /// pointer to the request buffer from C for detection. #[no_mangle] -pub extern "C" fn rs_template_get_request_buffer( +pub unsafe extern "C" fn rs_template_get_request_buffer( tx: *mut std::os::raw::c_void, buf: *mut *const u8, len: *mut u32, @@ -482,10 +478,8 @@ pub extern "C" fn rs_template_get_request_buffer( let tx = cast_pointer!(tx, TemplateTransaction); if let Some(ref request) = tx.request { if request.len() > 0 { - unsafe { - *len = request.len() as u32; - *buf = request.as_ptr(); - } + *len = request.len() as u32; + *buf = request.as_ptr(); return 1; } } @@ -494,7 +488,7 @@ pub extern "C" fn rs_template_get_request_buffer( /// Get the response buffer for a transaction from C. #[no_mangle] -pub extern "C" fn rs_template_get_response_buffer( +pub unsafe extern "C" fn rs_template_get_response_buffer( tx: *mut std::os::raw::c_void, buf: *mut *const u8, len: *mut u32, @@ -503,10 +497,8 @@ pub extern "C" fn rs_template_get_response_buffer( let tx = cast_pointer!(tx, TemplateTransaction); if let Some(ref response) = tx.response { if response.len() > 0 { - unsafe { - *len = response.len() as u32; - *buf = response.as_ptr(); - } + *len = response.len() as u32; + *buf = response.as_ptr(); return 1; } } diff --git a/rust/src/asn1/mod.rs b/rust/src/asn1/mod.rs index 6f8364898dc9..e4086b628fab 100644 --- a/rust/src/asn1/mod.rs +++ b/rust/src/asn1/mod.rs @@ -211,7 +211,7 @@ fn asn1_decode<'a>( /// input must be a valid buffer of at least input_len bytes /// pointer must be freed using `rs_asn1_free` #[no_mangle] -pub extern "C" fn rs_asn1_decode( +pub unsafe extern "C" fn rs_asn1_decode( input: *const u8, input_len: u16, buffer_offset: u32, ad_ptr: *const DetectAsn1Data, ) -> *mut Asn1<'static> { if input.is_null() || input_len == 0 || ad_ptr.is_null() { @@ -220,7 +220,7 @@ pub extern "C" fn rs_asn1_decode( let slice = build_slice!(input, input_len as usize); - let ad = unsafe { &*ad_ptr }; + let ad = &*ad_ptr ; let res = asn1_decode(slice, buffer_offset, ad); diff --git a/rust/src/dcerpc/dcerpc.rs b/rust/src/dcerpc/dcerpc.rs index d7b54f0f7522..97061bd920bd 100644 --- a/rust/src/dcerpc/dcerpc.rs +++ b/rust/src/dcerpc/dcerpc.rs @@ -1143,7 +1143,7 @@ pub extern "C" fn rs_parse_dcerpc_response_gap( } #[no_mangle] -pub extern "C" fn rs_dcerpc_parse_request( +pub unsafe extern "C" fn rs_dcerpc_parse_request( flow: *const core::Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, input_len: u32, _data: *const std::os::raw::c_void, flags: u8, ) -> AppLayerResult { @@ -1166,7 +1166,7 @@ pub extern "C" fn rs_dcerpc_parse_request( } #[no_mangle] -pub extern "C" fn rs_dcerpc_parse_response( +pub unsafe extern "C" fn rs_dcerpc_parse_response( flow: *const core::Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, input_len: u32, _data: *const std::os::raw::c_void, flags: u8, ) -> AppLayerResult { @@ -1201,14 +1201,14 @@ pub extern "C" fn rs_dcerpc_state_free(state: *mut std::os::raw::c_void) { } #[no_mangle] -pub extern "C" fn rs_dcerpc_state_transaction_free(state: *mut std::os::raw::c_void, tx_id: u64) { +pub unsafe extern "C" fn rs_dcerpc_state_transaction_free(state: *mut std::os::raw::c_void, tx_id: u64) { let dce_state = cast_pointer!(state, DCERPCState); SCLogDebug!("freeing tx {}", tx_id as u64); dce_state.free_tx(tx_id); } #[no_mangle] -pub extern "C" fn rs_dcerpc_state_trunc(state: *mut std::os::raw::c_void, direction: u8) { +pub unsafe extern "C" fn rs_dcerpc_state_trunc(state: *mut std::os::raw::c_void, direction: u8) { let dce_state = cast_pointer!(state, DCERPCState); if direction & core::STREAM_TOSERVER != 0 { dce_state.ts_ssn_trunc = true; @@ -1232,7 +1232,7 @@ pub extern "C" fn rs_dcerpc_state_trunc(state: *mut std::os::raw::c_void, direct } #[no_mangle] -pub extern "C" fn rs_dcerpc_get_tx_detect_state( +pub unsafe extern "C" fn rs_dcerpc_get_tx_detect_state( vtx: *mut std::os::raw::c_void, ) -> *mut core::DetectEngineState { let dce_tx = cast_pointer!(vtx, DCERPCTransaction); @@ -1243,7 +1243,7 @@ pub extern "C" fn rs_dcerpc_get_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_dcerpc_set_tx_detect_state( +pub unsafe extern "C" fn rs_dcerpc_set_tx_detect_state( vtx: *mut std::os::raw::c_void, de_state: &mut core::DetectEngineState, ) -> std::os::raw::c_int { let dce_tx = cast_pointer!(vtx, DCERPCTransaction); @@ -1252,7 +1252,7 @@ pub extern "C" fn rs_dcerpc_set_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_dcerpc_get_tx( +pub unsafe extern "C" fn rs_dcerpc_get_tx( vtx: *mut std::os::raw::c_void, tx_id: u64, ) -> *mut std::os::raw::c_void { let dce_state = cast_pointer!(vtx, DCERPCState); @@ -1263,13 +1263,13 @@ pub extern "C" fn rs_dcerpc_get_tx( } #[no_mangle] -pub extern "C" fn rs_dcerpc_get_tx_cnt(vtx: *mut std::os::raw::c_void) -> u64 { +pub unsafe extern "C" fn rs_dcerpc_get_tx_cnt(vtx: *mut std::os::raw::c_void) -> u64 { let dce_state = cast_pointer!(vtx, DCERPCState); dce_state.tx_id } #[no_mangle] -pub extern "C" fn rs_dcerpc_get_alstate_progress(tx: *mut std::os::raw::c_void, direction: u8 +pub unsafe extern "C" fn rs_dcerpc_get_alstate_progress(tx: *mut std::os::raw::c_void, direction: u8 )-> std::os::raw::c_int { let tx = cast_pointer!(tx, DCERPCTransaction); if direction == core::STREAM_TOSERVER && tx.req_done { @@ -1284,7 +1284,7 @@ pub extern "C" fn rs_dcerpc_get_alstate_progress(tx: *mut std::os::raw::c_void, } #[no_mangle] -pub extern "C" fn rs_dcerpc_get_tx_data( +pub unsafe extern "C" fn rs_dcerpc_get_tx_data( tx: *mut std::os::raw::c_void) -> *mut AppLayerTxData { @@ -1326,16 +1326,14 @@ fn probe(input: &[u8]) -> (bool, bool) { } } -pub extern "C" fn rs_dcerpc_probe_tcp(_f: *const core::Flow, direction: u8, input: *const u8, +pub unsafe extern "C" fn rs_dcerpc_probe_tcp(_f: *const core::Flow, direction: u8, input: *const u8, len: u32, rdir: *mut u8) -> AppProto { SCLogDebug!("Probing packet for DCERPC"); if len == 0 { return core::ALPROTO_UNKNOWN; } - let slice: &[u8] = unsafe { - std::slice::from_raw_parts(input as *mut u8, len as usize) - }; + let slice: &[u8] = std::slice::from_raw_parts(input as *mut u8, len as usize); //is_incomplete is checked by caller let (is_dcerpc, is_request, ) = probe(slice); if is_dcerpc { @@ -1345,11 +1343,11 @@ pub extern "C" fn rs_dcerpc_probe_tcp(_f: *const core::Flow, direction: u8, inpu core::STREAM_TOCLIENT }; if direction & (core::STREAM_TOSERVER|core::STREAM_TOCLIENT) != dir { - unsafe { *rdir = dir }; + *rdir = dir; } - return unsafe { ALPROTO_DCERPC }; + return ALPROTO_DCERPC; } - return unsafe { core::ALPROTO_FAILED }; + return core::ALPROTO_FAILED; } fn register_pattern_probe() -> i8 { diff --git a/rust/src/dcerpc/dcerpc_udp.rs b/rust/src/dcerpc/dcerpc_udp.rs index f6f5ed961f7a..44990ee484c9 100644 --- a/rust/src/dcerpc/dcerpc_udp.rs +++ b/rust/src/dcerpc/dcerpc_udp.rs @@ -205,7 +205,7 @@ impl DCERPCUDPState { } #[no_mangle] -pub extern "C" fn rs_dcerpc_udp_parse( +pub unsafe extern "C" fn rs_dcerpc_udp_parse( _flow: *const core::Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, input_len: u32, _data: *const std::os::raw::c_void, _flags: u8, ) -> AppLayerResult { @@ -230,7 +230,7 @@ pub extern "C" fn rs_dcerpc_udp_state_new(_orig_state: *mut std::os::raw::c_void } #[no_mangle] -pub extern "C" fn rs_dcerpc_udp_state_transaction_free( +pub unsafe extern "C" fn rs_dcerpc_udp_state_transaction_free( state: *mut std::os::raw::c_void, tx_id: u64, ) { let dce_state = cast_pointer!(state, DCERPCUDPState); @@ -239,7 +239,7 @@ pub extern "C" fn rs_dcerpc_udp_state_transaction_free( } #[no_mangle] -pub extern "C" fn rs_dcerpc_udp_get_tx_detect_state( +pub unsafe extern "C" fn rs_dcerpc_udp_get_tx_detect_state( vtx: *mut std::os::raw::c_void, ) -> *mut core::DetectEngineState { let dce_state = cast_pointer!(vtx, DCERPCTransaction); @@ -250,7 +250,7 @@ pub extern "C" fn rs_dcerpc_udp_get_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_dcerpc_udp_set_tx_detect_state( +pub unsafe extern "C" fn rs_dcerpc_udp_set_tx_detect_state( vtx: *mut std::os::raw::c_void, de_state: &mut core::DetectEngineState, ) -> std::os::raw::c_int { let dce_state = cast_pointer!(vtx, DCERPCTransaction); @@ -259,7 +259,7 @@ pub extern "C" fn rs_dcerpc_udp_set_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_dcerpc_udp_get_tx_data( +pub unsafe extern "C" fn rs_dcerpc_udp_get_tx_data( tx: *mut std::os::raw::c_void) -> *mut AppLayerTxData { @@ -268,7 +268,7 @@ pub extern "C" fn rs_dcerpc_udp_get_tx_data( } #[no_mangle] -pub extern "C" fn rs_dcerpc_udp_get_tx( +pub unsafe extern "C" fn rs_dcerpc_udp_get_tx( state: *mut std::os::raw::c_void, tx_id: u64, ) -> *mut std::os::raw::c_void { let dce_state = cast_pointer!(state, DCERPCUDPState); @@ -283,7 +283,7 @@ pub extern "C" fn rs_dcerpc_udp_get_tx( } #[no_mangle] -pub extern "C" fn rs_dcerpc_udp_get_tx_cnt(vtx: *mut std::os::raw::c_void) -> u64 { +pub unsafe extern "C" fn rs_dcerpc_udp_get_tx_cnt(vtx: *mut std::os::raw::c_void) -> u64 { let dce_state = cast_pointer!(vtx, DCERPCUDPState); dce_state.tx_id } @@ -303,16 +303,14 @@ fn probe(input: &[u8]) -> (bool, bool) { } } -pub extern "C" fn rs_dcerpc_probe_udp(_f: *const core::Flow, direction: u8, input: *const u8, +pub unsafe extern "C" fn rs_dcerpc_probe_udp(_f: *const core::Flow, direction: u8, input: *const u8, len: u32, rdir: *mut u8) -> core::AppProto { SCLogDebug!("Probing the packet for DCERPC/UDP"); if len == 0 { return core::ALPROTO_UNKNOWN; } - let slice: &[u8] = unsafe { - std::slice::from_raw_parts(input as *mut u8, len as usize) - }; + let slice: &[u8] = std::slice::from_raw_parts(input as *mut u8, len as usize); //is_incomplete is checked by caller let (is_dcerpc, is_request) = probe(slice); if is_dcerpc { @@ -322,11 +320,11 @@ pub extern "C" fn rs_dcerpc_probe_udp(_f: *const core::Flow, direction: u8, inpu core::STREAM_TOCLIENT }; if direction & (core::STREAM_TOSERVER|core::STREAM_TOCLIENT) != dir { - unsafe { *rdir = dir }; + *rdir = dir; } - return unsafe {ALPROTO_DCERPC}; + return ALPROTO_DCERPC; } - return unsafe { core::ALPROTO_FAILED }; + return core::ALPROTO_FAILED; } fn register_pattern_probe() -> i8 { diff --git a/rust/src/dhcp/dhcp.rs b/rust/src/dhcp/dhcp.rs index a9ae1f42c2e1..73b1679ed771 100644 --- a/rust/src/dhcp/dhcp.rs +++ b/rust/src/dhcp/dhcp.rs @@ -221,7 +221,7 @@ impl DHCPState { } #[no_mangle] -pub extern "C" fn rs_dhcp_probing_parser(_flow: *const Flow, +pub unsafe extern "C" fn rs_dhcp_probing_parser(_flow: *const Flow, _direction: u8, input: *const u8, input_len: u32, @@ -234,7 +234,7 @@ pub extern "C" fn rs_dhcp_probing_parser(_flow: *const Flow, let slice = build_slice!(input, input_len as usize); match parse_header(slice) { Ok((_, _)) => { - return unsafe { ALPROTO_DHCP }; + return ALPROTO_DHCP; } _ => { return ALPROTO_UNKNOWN; @@ -250,7 +250,7 @@ pub extern "C" fn rs_dhcp_tx_get_alstate_progress(_tx: *mut std::os::raw::c_void } #[no_mangle] -pub extern "C" fn rs_dhcp_state_get_tx(state: *mut std::os::raw::c_void, +pub unsafe extern "C" fn rs_dhcp_state_get_tx(state: *mut std::os::raw::c_void, tx_id: u64) -> *mut std::os::raw::c_void { let state = cast_pointer!(state, DHCPState); match state.get_tx(tx_id) { @@ -264,13 +264,13 @@ pub extern "C" fn rs_dhcp_state_get_tx(state: *mut std::os::raw::c_void, } #[no_mangle] -pub extern "C" fn rs_dhcp_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { +pub unsafe extern "C" fn rs_dhcp_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { let state = cast_pointer!(state, DHCPState); return state.tx_id; } #[no_mangle] -pub extern "C" fn rs_dhcp_parse(_flow: *const core::Flow, +pub unsafe extern "C" fn rs_dhcp_parse(_flow: *const core::Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, @@ -286,7 +286,7 @@ pub extern "C" fn rs_dhcp_parse(_flow: *const core::Flow, } #[no_mangle] -pub extern "C" fn rs_dhcp_state_tx_free( +pub unsafe extern "C" fn rs_dhcp_state_tx_free( state: *mut std::os::raw::c_void, tx_id: u64) { @@ -302,12 +302,12 @@ pub extern "C" fn rs_dhcp_state_new(_orig_state: *mut std::os::raw::c_void, _ori } #[no_mangle] -pub extern "C" fn rs_dhcp_state_free(state: *mut std::os::raw::c_void) { - std::mem::drop(unsafe { Box::from_raw(state as *mut DHCPState) }); +pub unsafe extern "C" fn rs_dhcp_state_free(state: *mut std::os::raw::c_void) { + std::mem::drop(Box::from_raw(state as *mut DHCPState)); } #[no_mangle] -pub extern "C" fn rs_dhcp_state_get_event_info_by_id(event_id: std::os::raw::c_int, +pub unsafe extern "C" fn rs_dhcp_state_get_event_info_by_id(event_id: std::os::raw::c_int, event_name: *mut *const std::os::raw::c_char, event_type: *mut core::AppLayerEventType) -> i8 @@ -317,17 +317,15 @@ pub extern "C" fn rs_dhcp_state_get_event_info_by_id(event_id: std::os::raw::c_i DHCPEvent::TruncatedOptions => { "truncated_options\0" }, DHCPEvent::MalformedOptions => { "malformed_options\0" }, }; - unsafe{ - *event_name = estr.as_ptr() as *const std::os::raw::c_char; - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - }; + *event_name = estr.as_ptr() as *const std::os::raw::c_char; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; 0 } else { -1 } } #[no_mangle] -pub extern "C" fn rs_dhcp_state_get_events(tx: *mut std::os::raw::c_void) +pub unsafe extern "C" fn rs_dhcp_state_get_events(tx: *mut std::os::raw::c_void) -> *mut core::AppLayerDecoderEvents { let tx = cast_pointer!(tx, DHCPTransaction); @@ -335,7 +333,7 @@ pub extern "C" fn rs_dhcp_state_get_events(tx: *mut std::os::raw::c_void) } #[no_mangle] -pub extern "C" fn rs_dhcp_state_get_event_info( +pub unsafe extern "C" fn rs_dhcp_state_get_event_info( event_name: *const std::os::raw::c_char, event_id: *mut std::os::raw::c_int, event_type: *mut core::AppLayerEventType) @@ -344,7 +342,7 @@ pub extern "C" fn rs_dhcp_state_get_event_info( if event_name == std::ptr::null() { return -1; } - let c_event_name: &CStr = unsafe { CStr::from_ptr(event_name) }; + let c_event_name: &CStr = CStr::from_ptr(event_name); let event = match c_event_name.to_str() { Ok(s) => { match s { @@ -355,15 +353,13 @@ pub extern "C" fn rs_dhcp_state_get_event_info( }, Err(_) => -1, // UTF-8 conversion failed }; - unsafe{ - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - *event_id = event as std::os::raw::c_int; - }; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; + *event_id = event as std::os::raw::c_int; 0 } #[no_mangle] -pub extern "C" fn rs_dhcp_state_get_tx_iterator( +pub unsafe extern "C" fn rs_dhcp_state_get_tx_iterator( _ipproto: u8, _alproto: AppProto, state: *mut std::os::raw::c_void, diff --git a/rust/src/dhcp/logger.rs b/rust/src/dhcp/logger.rs index 1b4b4a977c55..bed75ecdfdc5 100644 --- a/rust/src/dhcp/logger.rs +++ b/rust/src/dhcp/logger.rs @@ -263,12 +263,12 @@ pub extern "C" fn rs_dhcp_logger_new(conf: *const c_void) -> *mut std::os::raw:: } #[no_mangle] -pub extern "C" fn rs_dhcp_logger_free(logger: *mut std::os::raw::c_void) { - std::mem::drop(unsafe { Box::from_raw(logger as *mut DHCPLogger) }); +pub unsafe extern "C" fn rs_dhcp_logger_free(logger: *mut std::os::raw::c_void) { + std::mem::drop(Box::from_raw(logger as *mut DHCPLogger)); } #[no_mangle] -pub extern "C" fn rs_dhcp_logger_log(logger: *mut std::os::raw::c_void, +pub unsafe extern "C" fn rs_dhcp_logger_log(logger: *mut std::os::raw::c_void, tx: *mut std::os::raw::c_void, js: &mut JsonBuilder) -> bool { let logger = cast_pointer!(logger, DHCPLogger); @@ -277,7 +277,7 @@ pub extern "C" fn rs_dhcp_logger_log(logger: *mut std::os::raw::c_void, } #[no_mangle] -pub extern "C" fn rs_dhcp_logger_do_log(logger: *mut std::os::raw::c_void, +pub unsafe extern "C" fn rs_dhcp_logger_do_log(logger: *mut std::os::raw::c_void, tx: *mut std::os::raw::c_void) -> bool { let logger = cast_pointer!(logger, DHCPLogger); diff --git a/rust/src/dns/dns.rs b/rust/src/dns/dns.rs index 76c441d4997e..4bb8d6de8fbd 100644 --- a/rust/src/dns/dns.rs +++ b/rust/src/dns/dns.rs @@ -155,23 +155,21 @@ impl DNSEvent { } #[no_mangle] -pub extern "C" fn rs_dns_state_get_event_info_by_id( +pub unsafe extern "C" fn rs_dns_state_get_event_info_by_id( event_id: std::os::raw::c_int, event_name: *mut *const std::os::raw::c_char, event_type: *mut core::AppLayerEventType, ) -> i8 { if let Some(e) = DNSEvent::from_id(event_id as u32) { - unsafe { - *event_name = e.to_cstring().as_ptr() as *const std::os::raw::c_char; - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - } + *event_name = e.to_cstring().as_ptr() as *const std::os::raw::c_char; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; return 0; } return -1; } #[no_mangle] -pub extern "C" fn rs_dns_state_get_event_info( +pub unsafe extern "C" fn rs_dns_state_get_event_info( event_name: *const std::os::raw::c_char, event_id: *mut std::os::raw::c_int, event_type: *mut core::AppLayerEventType @@ -180,13 +178,11 @@ pub extern "C" fn rs_dns_state_get_event_info( return -1; } - let event_name = unsafe { std::ffi::CStr::from_ptr(event_name) }; + let event_name = std::ffi::CStr::from_ptr(event_name); if let Ok(event_name) = event_name.to_str() { if let Some(event) = DNSEvent::from_string(event_name) { - unsafe { - *event_id = event as std::os::raw::c_int; - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - } + *event_id = event as std::os::raw::c_int; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; } else { // Unknown event... return -1; @@ -739,7 +735,7 @@ pub extern "C" fn rs_dns_state_free(state: *mut std::os::raw::c_void) { } #[no_mangle] -pub extern "C" fn rs_dns_state_tx_free(state: *mut std::os::raw::c_void, +pub unsafe extern "C" fn rs_dns_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { let state = cast_pointer!(state, DNSState); @@ -748,7 +744,7 @@ pub extern "C" fn rs_dns_state_tx_free(state: *mut std::os::raw::c_void, /// C binding parse a DNS request. Returns 1 on success, -1 on failure. #[no_mangle] -pub extern "C" fn rs_dns_parse_request(_flow: *const core::Flow, +pub unsafe extern "C" fn rs_dns_parse_request(_flow: *const core::Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, @@ -757,7 +753,7 @@ pub extern "C" fn rs_dns_parse_request(_flow: *const core::Flow, _flags: u8) -> AppLayerResult { let state = cast_pointer!(state, DNSState); - let buf = unsafe{std::slice::from_raw_parts(input, input_len as usize)}; + let buf = std::slice::from_raw_parts(input, input_len as usize); if state.parse_request(buf) { AppLayerResult::ok() } else { @@ -766,7 +762,7 @@ pub extern "C" fn rs_dns_parse_request(_flow: *const core::Flow, } #[no_mangle] -pub extern "C" fn rs_dns_parse_response(_flow: *const core::Flow, +pub unsafe extern "C" fn rs_dns_parse_response(_flow: *const core::Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, @@ -775,7 +771,7 @@ pub extern "C" fn rs_dns_parse_response(_flow: *const core::Flow, _flags: u8) -> AppLayerResult { let state = cast_pointer!(state, DNSState); - let buf = unsafe{std::slice::from_raw_parts(input, input_len as usize)}; + let buf = std::slice::from_raw_parts(input, input_len as usize); if state.parse_response(buf) { AppLayerResult::ok() } else { @@ -785,7 +781,7 @@ pub extern "C" fn rs_dns_parse_response(_flow: *const core::Flow, /// C binding parse a DNS request. Returns 1 on success, -1 on failure. #[no_mangle] -pub extern "C" fn rs_dns_parse_request_tcp(_flow: *const core::Flow, +pub unsafe extern "C" fn rs_dns_parse_request_tcp(_flow: *const core::Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, @@ -796,8 +792,7 @@ pub extern "C" fn rs_dns_parse_request_tcp(_flow: *const core::Flow, let state = cast_pointer!(state, DNSState); if input_len > 0 { if input != std::ptr::null_mut() { - let buf = unsafe{ - std::slice::from_raw_parts(input, input_len as usize)}; + let buf = std::slice::from_raw_parts(input, input_len as usize); return state.parse_request_tcp(buf); } state.request_gap(input_len); @@ -806,7 +801,7 @@ pub extern "C" fn rs_dns_parse_request_tcp(_flow: *const core::Flow, } #[no_mangle] -pub extern "C" fn rs_dns_parse_response_tcp(_flow: *const core::Flow, +pub unsafe extern "C" fn rs_dns_parse_response_tcp(_flow: *const core::Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, @@ -817,8 +812,7 @@ pub extern "C" fn rs_dns_parse_response_tcp(_flow: *const core::Flow, let state = cast_pointer!(state, DNSState); if input_len > 0 { if input != std::ptr::null_mut() { - let buf = unsafe{ - std::slice::from_raw_parts(input, input_len as usize)}; + let buf = std::slice::from_raw_parts(input, input_len as usize); return state.parse_response_tcp(buf); } state.response_gap(input_len); @@ -838,7 +832,7 @@ pub extern "C" fn rs_dns_tx_get_alstate_progress(_tx: *mut std::os::raw::c_void, } #[no_mangle] -pub extern "C" fn rs_dns_state_get_tx_count(state: *mut std::os::raw::c_void) +pub unsafe extern "C" fn rs_dns_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { let state = cast_pointer!(state, DNSState); @@ -847,7 +841,7 @@ pub extern "C" fn rs_dns_state_get_tx_count(state: *mut std::os::raw::c_void) } #[no_mangle] -pub extern "C" fn rs_dns_state_get_tx(state: *mut std::os::raw::c_void, +pub unsafe extern "C" fn rs_dns_state_get_tx(state: *mut std::os::raw::c_void, tx_id: u64) -> *mut std::os::raw::c_void { @@ -873,7 +867,7 @@ pub extern "C" fn rs_dns_tx_is_response(tx: &mut DNSTransaction) -> bool { } #[no_mangle] -pub extern "C" fn rs_dns_state_set_tx_detect_state( +pub unsafe extern "C" fn rs_dns_state_set_tx_detect_state( tx: *mut std::os::raw::c_void, de_state: &mut core::DetectEngineState) -> std::os::raw::c_int { @@ -883,7 +877,7 @@ pub extern "C" fn rs_dns_state_set_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_dns_state_get_tx_detect_state( +pub unsafe extern "C" fn rs_dns_state_get_tx_detect_state( tx: *mut std::os::raw::c_void) -> *mut core::DetectEngineState { @@ -899,7 +893,7 @@ pub extern "C" fn rs_dns_state_get_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_dns_state_get_events(tx: *mut std::os::raw::c_void) +pub unsafe extern "C" fn rs_dns_state_get_events(tx: *mut std::os::raw::c_void) -> *mut core::AppLayerDecoderEvents { let tx = cast_pointer!(tx, DNSTransaction); @@ -907,7 +901,7 @@ pub extern "C" fn rs_dns_state_get_events(tx: *mut std::os::raw::c_void) } #[no_mangle] -pub extern "C" fn rs_dns_state_get_tx_data( +pub unsafe extern "C" fn rs_dns_state_get_tx_data( tx: *mut std::os::raw::c_void) -> *mut AppLayerTxData { @@ -916,7 +910,7 @@ pub extern "C" fn rs_dns_state_get_tx_data( } #[no_mangle] -pub extern "C" fn rs_dns_tx_get_query_name(tx: &mut DNSTransaction, +pub unsafe extern "C" fn rs_dns_tx_get_query_name(tx: &mut DNSTransaction, i: u32, buf: *mut *const u8, len: *mut u32) @@ -926,10 +920,8 @@ pub extern "C" fn rs_dns_tx_get_query_name(tx: &mut DNSTransaction, if (i as usize) < request.queries.len() { let query = &request.queries[i as usize]; if query.name.len() > 0 { - unsafe { - *len = query.name.len() as u32; - *buf = query.name.as_ptr(); - } + *len = query.name.len() as u32; + *buf = query.name.as_ptr(); return 1; } } @@ -957,7 +949,7 @@ pub extern "C" fn rs_dns_tx_get_response_flags(tx: &mut DNSTransaction) } #[no_mangle] -pub extern "C" fn rs_dns_tx_get_query_rrtype(tx: &mut DNSTransaction, +pub unsafe extern "C" fn rs_dns_tx_get_query_rrtype(tx: &mut DNSTransaction, i: u16, rrtype: *mut u16) -> u8 @@ -966,9 +958,7 @@ pub extern "C" fn rs_dns_tx_get_query_rrtype(tx: &mut DNSTransaction, if (i as usize) < request.queries.len() { let query = &request.queries[i as usize]; if query.name.len() > 0 { - unsafe { - *rrtype = query.rrtype; - } + *rrtype = query.rrtype; return 1; } } @@ -977,7 +967,7 @@ pub extern "C" fn rs_dns_tx_get_query_rrtype(tx: &mut DNSTransaction, } #[no_mangle] -pub extern "C" fn rs_dns_probe( +pub unsafe extern "C" fn rs_dns_probe( _flow: *const core::Flow, _dir: u8, input: *const u8, @@ -987,9 +977,7 @@ pub extern "C" fn rs_dns_probe( if len == 0 || len < std::mem::size_of::() as u32 { return core::ALPROTO_UNKNOWN; } - let slice: &[u8] = unsafe { - std::slice::from_raw_parts(input as *mut u8, len as usize) - }; + let slice: &[u8] = std::slice::from_raw_parts(input as *mut u8, len as usize); let (is_dns, is_request, _) = probe(slice, slice.len()); if is_dns { let dir = if is_request { @@ -997,16 +985,14 @@ pub extern "C" fn rs_dns_probe( } else { core::STREAM_TOCLIENT }; - unsafe { - *rdir = dir; - return ALPROTO_DNS; - } + *rdir = dir; + return ALPROTO_DNS; } return 0; } #[no_mangle] -pub extern "C" fn rs_dns_probe_tcp( +pub unsafe extern "C" fn rs_dns_probe_tcp( _flow: *const core::Flow, direction: u8, input: *const u8, @@ -1016,9 +1002,7 @@ pub extern "C" fn rs_dns_probe_tcp( if len == 0 || len < std::mem::size_of::() as u32 + 2 { return core::ALPROTO_UNKNOWN; } - let slice: &[u8] = unsafe { - std::slice::from_raw_parts(input as *mut u8, len as usize) - }; + let slice: &[u8] = std::slice::from_raw_parts(input as *mut u8, len as usize); //is_incomplete is checked by caller let (is_dns, is_request, _) = probe_tcp(slice); if is_dns { @@ -1028,15 +1012,15 @@ pub extern "C" fn rs_dns_probe_tcp( core::STREAM_TOCLIENT }; if direction & (core::STREAM_TOSERVER|core::STREAM_TOCLIENT) != dir { - unsafe { *rdir = dir }; + *rdir = dir; } - return unsafe { ALPROTO_DNS }; + return ALPROTO_DNS; } return 0; } #[no_mangle] -pub extern "C" fn rs_dns_apply_tx_config( +pub unsafe extern "C" fn rs_dns_apply_tx_config( _state: *mut std::os::raw::c_void, _tx: *mut std::os::raw::c_void, _mode: std::os::raw::c_int, config: AppLayerTxConfig ) { diff --git a/rust/src/ftp/mod.rs b/rust/src/ftp/mod.rs index 53c3df7d764c..cc9cc5d60fa6 100644 --- a/rust/src/ftp/mod.rs +++ b/rust/src/ftp/mod.rs @@ -76,7 +76,7 @@ named!(pub ftp_pasv_response, #[no_mangle] -pub extern "C" fn rs_ftp_active_port(input: *const u8, len: u32) -> u16 { +pub unsafe extern "C" fn rs_ftp_active_port(input: *const u8, len: u32) -> u16 { let buf = build_slice!(input, len as usize); match ftp_active_port(buf) { Ok((_, dport)) => { @@ -94,8 +94,8 @@ pub extern "C" fn rs_ftp_active_port(input: *const u8, len: u32) -> u16 { #[no_mangle] -pub extern "C" fn rs_ftp_pasv_response(input: *const u8, len: u32) -> u16 { - let buf = unsafe{std::slice::from_raw_parts(input, len as usize)}; +pub unsafe extern "C" fn rs_ftp_pasv_response(input: *const u8, len: u32) -> u16 { + let buf = std::slice::from_raw_parts(input, len as usize); match ftp_pasv_response(buf) { Ok((_, dport)) => { return dport; @@ -140,7 +140,7 @@ named!(pub ftp_active_eprt, ); #[no_mangle] -pub extern "C" fn rs_ftp_active_eprt(input: *const u8, len: u32) -> u16 { +pub unsafe extern "C" fn rs_ftp_active_eprt(input: *const u8, len: u32) -> u16 { let buf = build_slice!(input, len as usize); match ftp_active_eprt(buf) { Ok((_, dport)) => { @@ -157,8 +157,8 @@ pub extern "C" fn rs_ftp_active_eprt(input: *const u8, len: u32) -> u16 { return 0; } #[no_mangle] -pub extern "C" fn rs_ftp_epsv_response(input: *const u8, len: u32) -> u16 { - let buf = unsafe{std::slice::from_raw_parts(input, len as usize)}; +pub unsafe extern "C" fn rs_ftp_epsv_response(input: *const u8, len: u32) -> u16 { + let buf = std::slice::from_raw_parts(input, len as usize); match ftp_epsv_response(buf) { Ok((_, dport)) => { return dport; diff --git a/rust/src/http2/detect.rs b/rust/src/http2/detect.rs index 3248b897cbf6..e017ec5b9031 100644 --- a/rust/src/http2/detect.rs +++ b/rust/src/http2/detect.rs @@ -43,7 +43,7 @@ fn http2_tx_has_frametype( } #[no_mangle] -pub extern "C" fn rs_http2_tx_has_frametype( +pub unsafe extern "C" fn rs_http2_tx_has_frametype( tx: *mut std::os::raw::c_void, direction: u8, value: u8, ) -> std::os::raw::c_int { let tx = cast_pointer!(tx, HTTP2Transaction); @@ -103,7 +103,7 @@ fn http2_tx_has_errorcode( } #[no_mangle] -pub extern "C" fn rs_http2_tx_has_errorcode( +pub unsafe extern "C" fn rs_http2_tx_has_errorcode( tx: *mut std::os::raw::c_void, direction: u8, code: u32, ) -> std::os::raw::c_int { let tx = cast_pointer!(tx, HTTP2Transaction); @@ -176,7 +176,7 @@ fn http2_tx_get_next_priority( } #[no_mangle] -pub extern "C" fn rs_http2_tx_get_next_priority( +pub unsafe extern "C" fn rs_http2_tx_get_next_priority( tx: *mut std::os::raw::c_void, direction: u8, nb: u32, ) -> std::os::raw::c_int { let tx = cast_pointer!(tx, HTTP2Transaction); @@ -218,7 +218,7 @@ fn http2_tx_get_next_window( } #[no_mangle] -pub extern "C" fn rs_http2_tx_get_next_window( +pub unsafe extern "C" fn rs_http2_tx_get_next_window( tx: *mut std::os::raw::c_void, direction: u8, nb: u32, ) -> std::os::raw::c_int { let tx = cast_pointer!(tx, HTTP2Transaction); @@ -312,7 +312,7 @@ fn http2_detect_settingsctx_match( } #[no_mangle] -pub extern "C" fn rs_http2_detect_settingsctx_match( +pub unsafe extern "C" fn rs_http2_detect_settingsctx_match( ctx: *const std::os::raw::c_void, tx: *mut std::os::raw::c_void, direction: u8, ) -> std::os::raw::c_int { let ctx = cast_pointer!(ctx, parser::DetectHTTP2settingsSigCtx); @@ -413,7 +413,7 @@ fn http2_detect_sizeupdatectx_match( } #[no_mangle] -pub extern "C" fn rs_http2_detect_sizeupdatectx_match( +pub unsafe extern "C" fn rs_http2_detect_sizeupdatectx_match( ctx: *const std::os::raw::c_void, tx: *mut std::os::raw::c_void, direction: u8, ) -> std::os::raw::c_int { let ctx = cast_pointer!(ctx, parser::DetectU64Data); @@ -704,7 +704,7 @@ fn http2_tx_set_header(state: &mut HTTP2State, name: &[u8], input: &[u8]) { } #[no_mangle] -pub extern "C" fn rs_http2_tx_set_method( +pub unsafe extern "C" fn rs_http2_tx_set_method( state: &mut HTTP2State, buffer: *const u8, buffer_len: u32, ) { let slice = build_slice!(buffer, buffer_len as usize); @@ -712,7 +712,7 @@ pub extern "C" fn rs_http2_tx_set_method( } #[no_mangle] -pub extern "C" fn rs_http2_tx_set_uri(state: &mut HTTP2State, buffer: *const u8, buffer_len: u32) { +pub unsafe extern "C" fn rs_http2_tx_set_uri(state: &mut HTTP2State, buffer: *const u8, buffer_len: u32) { let slice = build_slice!(buffer, buffer_len as usize); http2_tx_set_header(state, ":path".as_bytes(), slice) } @@ -853,7 +853,7 @@ fn http2_tx_set_settings(state: &mut HTTP2State, input: &[u8]) { } #[no_mangle] -pub extern "C" fn rs_http2_tx_add_header( +pub unsafe extern "C" fn rs_http2_tx_add_header( state: &mut HTTP2State, name: *const u8, name_len: u32, value: *const u8, value_len: u32, ) { let slice_name = build_slice!(name, name_len as usize); diff --git a/rust/src/http2/http2.rs b/rust/src/http2/http2.rs index c11e9d3ec64e..313ced7595c4 100644 --- a/rust/src/http2/http2.rs +++ b/rust/src/http2/http2.rs @@ -963,7 +963,7 @@ export_tx_data_get!(rs_http2_get_tx_data, HTTP2Transaction); /// C entry point for a probing parser. #[no_mangle] -pub extern "C" fn rs_http2_probing_parser_tc( +pub unsafe extern "C" fn rs_http2_probing_parser_tc( _flow: *const Flow, _direction: u8, input: *const u8, input_len: u32, _rdir: *mut u8, ) -> AppProto { if input != std::ptr::null_mut() { @@ -975,15 +975,15 @@ pub extern "C" fn rs_http2_probing_parser_tc( || header.flags & 0xFE != 0 || header.ftype != parser::HTTP2FrameType::SETTINGS as u8 { - return unsafe { ALPROTO_FAILED }; + return ALPROTO_FAILED; } - return unsafe { ALPROTO_HTTP2 }; + return ALPROTO_HTTP2; } Err(nom::Err::Incomplete(_)) => { return ALPROTO_UNKNOWN; } Err(_) => { - return unsafe { ALPROTO_FAILED }; + return ALPROTO_FAILED ; } } } @@ -997,7 +997,10 @@ extern "C" { ); } +// Suppress the unsafe warning here as creating a state for an app-layer +// is typically not unsafe. #[no_mangle] +#[allow(clippy::not_unsafe_ptr_arg_deref)] pub extern "C" fn rs_http2_state_new( orig_state: *mut std::os::raw::c_void, _orig_proto: AppProto, ) -> *mut std::os::raw::c_void { @@ -1014,44 +1017,44 @@ pub extern "C" fn rs_http2_state_new( } #[no_mangle] -pub extern "C" fn rs_http2_state_free(state: *mut std::os::raw::c_void) { - let mut state: Box = unsafe { Box::from_raw(state as _) }; +pub unsafe extern "C" fn rs_http2_state_free(state: *mut std::os::raw::c_void) { + let mut state: Box = Box::from_raw(state as _); state.free(); } #[no_mangle] -pub extern "C" fn rs_http2_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { +pub unsafe extern "C" fn rs_http2_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { let state = cast_pointer!(state, HTTP2State); state.free_tx(tx_id); } #[no_mangle] -pub extern "C" fn rs_http2_parse_ts( +pub unsafe extern "C" fn rs_http2_parse_ts( flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, input_len: u32, _data: *const std::os::raw::c_void, _flags: u8, ) -> AppLayerResult { let state = cast_pointer!(state, HTTP2State); let buf = build_slice!(input, input_len as usize); - state.files.flags_ts = unsafe { FileFlowToFlags(flow, STREAM_TOSERVER) }; + state.files.flags_ts = FileFlowToFlags(flow, STREAM_TOSERVER); state.files.flags_ts = state.files.flags_ts | FILE_USE_DETECT; return state.parse_ts(buf); } #[no_mangle] -pub extern "C" fn rs_http2_parse_tc( +pub unsafe extern "C" fn rs_http2_parse_tc( flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, input_len: u32, _data: *const std::os::raw::c_void, _flags: u8, ) -> AppLayerResult { let state = cast_pointer!(state, HTTP2State); let buf = build_slice!(input, input_len as usize); - state.files.flags_tc = unsafe { FileFlowToFlags(flow, STREAM_TOCLIENT) }; + state.files.flags_tc = FileFlowToFlags(flow, STREAM_TOCLIENT); state.files.flags_tc = state.files.flags_tc | FILE_USE_DETECT; return state.parse_tc(buf); } #[no_mangle] -pub extern "C" fn rs_http2_state_get_tx( +pub unsafe extern "C" fn rs_http2_state_get_tx( state: *mut std::os::raw::c_void, tx_id: u64, ) -> *mut std::os::raw::c_void { let state = cast_pointer!(state, HTTP2State); @@ -1066,26 +1069,26 @@ pub extern "C" fn rs_http2_state_get_tx( } #[no_mangle] -pub extern "C" fn rs_http2_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { +pub unsafe extern "C" fn rs_http2_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { let state = cast_pointer!(state, HTTP2State); return state.tx_id; } #[no_mangle] -pub extern "C" fn rs_http2_tx_get_state(tx: *mut std::os::raw::c_void) -> HTTP2TransactionState { +pub unsafe extern "C" fn rs_http2_tx_get_state(tx: *mut std::os::raw::c_void) -> HTTP2TransactionState { let tx = cast_pointer!(tx, HTTP2Transaction); return tx.state; } #[no_mangle] -pub extern "C" fn rs_http2_tx_get_alstate_progress( +pub unsafe extern "C" fn rs_http2_tx_get_alstate_progress( tx: *mut std::os::raw::c_void, _direction: u8, ) -> std::os::raw::c_int { return rs_http2_tx_get_state(tx) as i32; } #[no_mangle] -pub extern "C" fn rs_http2_state_get_events( +pub unsafe extern "C" fn rs_http2_state_get_events( tx: *mut std::os::raw::c_void, ) -> *mut core::AppLayerDecoderEvents { let tx = cast_pointer!(tx, HTTP2Transaction); @@ -1093,14 +1096,14 @@ pub extern "C" fn rs_http2_state_get_events( } #[no_mangle] -pub extern "C" fn rs_http2_state_get_event_info( +pub unsafe extern "C" fn rs_http2_state_get_event_info( event_name: *const std::os::raw::c_char, event_id: *mut std::os::raw::c_int, event_type: *mut core::AppLayerEventType, ) -> std::os::raw::c_int { if event_name == std::ptr::null() { return -1; } - let c_event_name: &CStr = unsafe { CStr::from_ptr(event_name) }; + let c_event_name: &CStr = CStr::from_ptr(event_name); let event = match c_event_name.to_str() { Ok(s) => { match s { @@ -1119,15 +1122,13 @@ pub extern "C" fn rs_http2_state_get_event_info( } Err(_) => -1, // UTF-8 conversion failed }; - unsafe { - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - *event_id = event as std::os::raw::c_int; - }; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; + *event_id = event as std::os::raw::c_int; 0 } #[no_mangle] -pub extern "C" fn rs_http2_state_get_event_info_by_id( +pub unsafe extern "C" fn rs_http2_state_get_event_info_by_id( event_id: std::os::raw::c_int, event_name: *mut *const std::os::raw::c_char, event_type: *mut core::AppLayerEventType, ) -> i8 { @@ -1144,17 +1145,15 @@ pub extern "C" fn rs_http2_state_get_event_info_by_id( HTTP2Event::InvalidHTTP1Settings => "invalid_http1_settings\0", HTTP2Event::FailedDecompression => "failed_decompression\0", }; - unsafe { - *event_name = estr.as_ptr() as *const std::os::raw::c_char; - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - }; + *event_name = estr.as_ptr() as *const std::os::raw::c_char; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; 0 } else { -1 } } #[no_mangle] -pub extern "C" fn rs_http2_state_get_tx_iterator( +pub unsafe extern "C" fn rs_http2_state_get_tx_iterator( _ipproto: u8, _alproto: AppProto, state: *mut std::os::raw::c_void, min_tx_id: u64, _max_tx_id: u64, istate: &mut u64, ) -> applayer::AppLayerGetTxIterTuple { @@ -1172,7 +1171,7 @@ pub extern "C" fn rs_http2_state_get_tx_iterator( } #[no_mangle] -pub extern "C" fn rs_http2_getfiles( +pub unsafe extern "C" fn rs_http2_getfiles( state: *mut std::os::raw::c_void, direction: u8, ) -> *mut FileContainer { let state = cast_pointer!(state, HTTP2State); diff --git a/rust/src/http2/logger.rs b/rust/src/http2/logger.rs index 7e15ffcaabd5..beb2874bd864 100644 --- a/rust/src/http2/logger.rs +++ b/rust/src/http2/logger.rs @@ -268,7 +268,7 @@ fn log_http2(tx: &HTTP2Transaction, js: &mut JsonBuilder) -> Result bool { +pub unsafe extern "C" fn rs_http2_log_json(tx: *mut std::os::raw::c_void, js: &mut JsonBuilder) -> bool { let tx = cast_pointer!(tx, HTTP2Transaction); if let Ok(x) = log_http2(tx, js) { return x; diff --git a/rust/src/ike/ike.rs b/rust/src/ike/ike.rs index 0a9b4fc07d99..1b1fb0bd8b35 100644 --- a/rust/src/ike/ike.rs +++ b/rust/src/ike/ike.rs @@ -322,21 +322,21 @@ export_tx_set_detect_state!(rs_ike_tx_set_detect_state, IKETransaction); /// C entry point for a probing parser. #[no_mangle] -pub extern "C" fn rs_ike_probing_parser( +pub unsafe extern "C" fn rs_ike_probing_parser( _flow: *const Flow, direction: u8, input: *const u8, input_len: u32, rdir: *mut u8, ) -> AppProto { if input_len < 28 { // at least the ISAKMP_HEADER must be there, not ALPROTO_UNKNOWN because over UDP - return unsafe { ALPROTO_FAILED }; + return ALPROTO_FAILED; } if input != std::ptr::null_mut() { let slice = build_slice!(input, input_len as usize); if probe(slice, direction, rdir) { - return unsafe { ALPROTO_IKE }; + return ALPROTO_IKE ; } } - return unsafe { ALPROTO_FAILED }; + return ALPROTO_FAILED; } #[no_mangle] @@ -349,19 +349,19 @@ pub extern "C" fn rs_ike_state_new( } #[no_mangle] -pub extern "C" fn rs_ike_state_free(state: *mut std::os::raw::c_void) { +pub unsafe extern "C" fn rs_ike_state_free(state: *mut std::os::raw::c_void) { // Just unbox... - std::mem::drop(unsafe { Box::from_raw(state as *mut IKEState) }); + std::mem::drop(Box::from_raw(state as *mut IKEState)); } #[no_mangle] -pub extern "C" fn rs_ike_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { +pub unsafe extern "C" fn rs_ike_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { let state = cast_pointer!(state, IKEState); state.free_tx(tx_id); } #[no_mangle] -pub extern "C" fn rs_ike_parse_request( +pub unsafe extern "C" fn rs_ike_parse_request( _flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, input_len: u32, _data: *const std::os::raw::c_void, _flags: u8, ) -> AppLayerResult { @@ -372,7 +372,7 @@ pub extern "C" fn rs_ike_parse_request( } #[no_mangle] -pub extern "C" fn rs_ike_parse_response( +pub unsafe extern "C" fn rs_ike_parse_response( _flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, input_len: u32, _data: *const std::os::raw::c_void, _flags: u8, ) -> AppLayerResult { @@ -382,7 +382,7 @@ pub extern "C" fn rs_ike_parse_response( } #[no_mangle] -pub extern "C" fn rs_ike_state_get_tx( +pub unsafe extern "C" fn rs_ike_state_get_tx( state: *mut std::os::raw::c_void, tx_id: u64, ) -> *mut std::os::raw::c_void { let state = cast_pointer!(state, IKEState); @@ -397,7 +397,7 @@ pub extern "C" fn rs_ike_state_get_tx( } #[no_mangle] -pub extern "C" fn rs_ike_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { +pub unsafe extern "C" fn rs_ike_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { let state = cast_pointer!(state, IKEState); return state.tx_id; } @@ -416,7 +416,7 @@ pub extern "C" fn rs_ike_tx_get_alstate_progress( } #[no_mangle] -pub extern "C" fn rs_ike_tx_get_logged( +pub unsafe extern "C" fn rs_ike_tx_get_logged( _state: *mut std::os::raw::c_void, tx: *mut std::os::raw::c_void, ) -> u32 { let tx = cast_pointer!(tx, IKETransaction); @@ -424,7 +424,7 @@ pub extern "C" fn rs_ike_tx_get_logged( } #[no_mangle] -pub extern "C" fn rs_ike_tx_set_logged( +pub unsafe extern "C" fn rs_ike_tx_set_logged( _state: *mut std::os::raw::c_void, tx: *mut std::os::raw::c_void, logged: u32, ) { let tx = cast_pointer!(tx, IKETransaction); @@ -432,7 +432,7 @@ pub extern "C" fn rs_ike_tx_set_logged( } #[no_mangle] -pub extern "C" fn rs_ike_state_get_events( +pub unsafe extern "C" fn rs_ike_state_get_events( tx: *mut std::os::raw::c_void, ) -> *mut core::AppLayerDecoderEvents { let tx = cast_pointer!(tx, IKETransaction); @@ -440,7 +440,7 @@ pub extern "C" fn rs_ike_state_get_events( } #[no_mangle] -pub extern "C" fn rs_ike_state_get_event_info_by_id( +pub unsafe extern "C" fn rs_ike_state_get_event_info_by_id( event_id: std::os::raw::c_int, event_name: *mut *const std::os::raw::c_char, event_type: *mut core::AppLayerEventType, ) -> i8 { @@ -459,10 +459,8 @@ pub extern "C" fn rs_ike_state_get_event_info_by_id( IkeEvent::PayloadExtraData => "payload_extra_data\0", IkeEvent::MultipleServerProposal => "multiple_server_proposal\0", }; - unsafe { - *event_name = estr.as_ptr() as *const std::os::raw::c_char; - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - }; + *event_name = estr.as_ptr() as *const std::os::raw::c_char; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; 0 } else { -1 @@ -470,14 +468,14 @@ pub extern "C" fn rs_ike_state_get_event_info_by_id( } #[no_mangle] -pub extern "C" fn rs_ike_state_get_event_info( +pub unsafe extern "C" fn rs_ike_state_get_event_info( event_name: *const std::os::raw::c_char, event_id: *mut std::os::raw::c_int, event_type: *mut core::AppLayerEventType, ) -> std::os::raw::c_int { if event_name == std::ptr::null() { return -1; } - let c_event_name: &CStr = unsafe { CStr::from_ptr(event_name) }; + let c_event_name: &CStr = CStr::from_ptr(event_name); let event = match c_event_name.to_str() { Ok(s) => { match s { @@ -498,17 +496,15 @@ pub extern "C" fn rs_ike_state_get_event_info( } Err(_) => -1, // UTF-8 conversion failed }; - unsafe { - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - *event_id = event as std::os::raw::c_int; - }; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; + *event_id = event as std::os::raw::c_int; 0 } static mut ALPROTO_IKE : AppProto = ALPROTO_UNKNOWN; #[no_mangle] -pub extern "C" fn rs_ike_state_get_tx_iterator( +pub unsafe extern "C" fn rs_ike_state_get_tx_iterator( _ipproto: u8, _alproto: AppProto, state: *mut std::os::raw::c_void, min_tx_id: u64, _max_tx_id: u64, istate: &mut u64, ) -> applayer::AppLayerGetTxIterTuple { diff --git a/rust/src/ike/logger.rs b/rust/src/ike/logger.rs index 390231229e3f..c6809e933099 100644 --- a/rust/src/ike/logger.rs +++ b/rust/src/ike/logger.rs @@ -224,7 +224,7 @@ fn log_ikev2(tx: &IKETransaction, jb: &mut JsonBuilder) -> Result<(), JsonError> } #[no_mangle] -pub extern "C" fn rs_ike_logger_log( +pub unsafe extern "C" fn rs_ike_logger_log( state: &mut IKEState, tx: *mut std::os::raw::c_void, flags: u32, js: &mut JsonBuilder, ) -> bool { let tx = cast_pointer!(tx, IKETransaction); diff --git a/rust/src/krb/krb5.rs b/rust/src/krb/krb5.rs index 564266c0f815..3f619bb29684 100644 --- a/rust/src/krb/krb5.rs +++ b/rust/src/krb/krb5.rs @@ -288,7 +288,7 @@ pub extern "C" fn rs_krb5_state_free(state: *mut std::os::raw::c_void) { } #[no_mangle] -pub extern "C" fn rs_krb5_state_get_tx(state: *mut std::os::raw::c_void, +pub unsafe extern "C" fn rs_krb5_state_get_tx(state: *mut std::os::raw::c_void, tx_id: u64) -> *mut std::os::raw::c_void { @@ -300,7 +300,7 @@ pub extern "C" fn rs_krb5_state_get_tx(state: *mut std::os::raw::c_void, } #[no_mangle] -pub extern "C" fn rs_krb5_state_get_tx_count(state: *mut std::os::raw::c_void) +pub unsafe extern "C" fn rs_krb5_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { let state = cast_pointer!(state,KRB5State); @@ -308,7 +308,7 @@ pub extern "C" fn rs_krb5_state_get_tx_count(state: *mut std::os::raw::c_void) } #[no_mangle] -pub extern "C" fn rs_krb5_state_tx_free(state: *mut std::os::raw::c_void, +pub unsafe extern "C" fn rs_krb5_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { let state = cast_pointer!(state,KRB5State); @@ -324,7 +324,7 @@ pub extern "C" fn rs_krb5_tx_get_alstate_progress(_tx: *mut std::os::raw::c_void } #[no_mangle] -pub extern "C" fn rs_krb5_state_set_tx_detect_state( +pub unsafe extern "C" fn rs_krb5_state_set_tx_detect_state( tx: *mut std::os::raw::c_void, de_state: &mut core::DetectEngineState) -> std::os::raw::c_int { @@ -334,7 +334,7 @@ pub extern "C" fn rs_krb5_state_set_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_krb5_state_get_tx_detect_state( +pub unsafe extern "C" fn rs_krb5_state_get_tx_detect_state( tx: *mut std::os::raw::c_void) -> *mut core::DetectEngineState { @@ -346,7 +346,7 @@ pub extern "C" fn rs_krb5_state_get_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_krb5_state_get_event_info_by_id(event_id: std::os::raw::c_int, +pub unsafe extern "C" fn rs_krb5_state_get_event_info_by_id(event_id: std::os::raw::c_int, event_name: *mut *const std::os::raw::c_char, event_type: *mut core::AppLayerEventType) -> i8 @@ -356,10 +356,8 @@ pub extern "C" fn rs_krb5_state_get_event_info_by_id(event_id: std::os::raw::c_i KRB5Event::MalformedData => { "malformed_data\0" }, KRB5Event::WeakEncryption => { "weak_encryption\0" }, }; - unsafe{ - *event_name = estr.as_ptr() as *const std::os::raw::c_char; - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - }; + *event_name = estr.as_ptr() as *const std::os::raw::c_char; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; 0 } else { -1 @@ -367,7 +365,7 @@ pub extern "C" fn rs_krb5_state_get_event_info_by_id(event_id: std::os::raw::c_i } #[no_mangle] -pub extern "C" fn rs_krb5_state_get_events(tx: *mut std::os::raw::c_void) +pub unsafe extern "C" fn rs_krb5_state_get_events(tx: *mut std::os::raw::c_void) -> *mut core::AppLayerDecoderEvents { let tx = cast_pointer!(tx, KRB5Transaction); @@ -375,13 +373,13 @@ pub extern "C" fn rs_krb5_state_get_events(tx: *mut std::os::raw::c_void) } #[no_mangle] -pub extern "C" fn rs_krb5_state_get_event_info(event_name: *const std::os::raw::c_char, +pub unsafe extern "C" fn rs_krb5_state_get_event_info(event_name: *const std::os::raw::c_char, event_id: *mut std::os::raw::c_int, event_type: *mut core::AppLayerEventType) -> std::os::raw::c_int { if event_name == std::ptr::null() { return -1; } - let c_event_name: &CStr = unsafe { CStr::from_ptr(event_name) }; + let c_event_name: &CStr = CStr::from_ptr(event_name); let event = match c_event_name.to_str() { Ok(s) => { match s { @@ -392,31 +390,29 @@ pub extern "C" fn rs_krb5_state_get_event_info(event_name: *const std::os::raw:: }, Err(_) => -1, // UTF-8 conversion failed }; - unsafe{ - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - *event_id = event as std::os::raw::c_int; - }; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; + *event_id = event as std::os::raw::c_int; 0 } static mut ALPROTO_KRB5 : AppProto = ALPROTO_UNKNOWN; #[no_mangle] -pub extern "C" fn rs_krb5_probing_parser(_flow: *const Flow, +pub unsafe extern "C" fn rs_krb5_probing_parser(_flow: *const Flow, _direction: u8, input:*const u8, input_len: u32, _rdir: *mut u8) -> AppProto { let slice = build_slice!(input,input_len as usize); - let alproto = unsafe{ ALPROTO_KRB5 }; - if slice.len() <= 10 { return unsafe{ALPROTO_FAILED}; } + let alproto = ALPROTO_KRB5; + if slice.len() <= 10 { return ALPROTO_FAILED; } match der_read_element_header(slice) { Ok((rem, ref hdr)) => { // Kerberos messages start with an APPLICATION header - if hdr.class != BerClass::Application { return unsafe{ALPROTO_FAILED}; } + if hdr.class != BerClass::Application { return ALPROTO_FAILED; } // Tag number should be <= 30 - if hdr.tag.0 > 30 { return unsafe{ALPROTO_FAILED}; } + if hdr.tag.0 > 30 { return ALPROTO_FAILED; } // Kerberos messages contain sequences - if rem.is_empty() || rem[0] != 0x30 { return unsafe{ALPROTO_FAILED}; } + if rem.is_empty() || rem[0] != 0x30 { return ALPROTO_FAILED; } // Check kerberos version if let Ok((rem,_hdr)) = der_read_element_header(rem) { if rem.len() > 5 { @@ -427,29 +423,29 @@ pub extern "C" fn rs_krb5_probing_parser(_flow: *const Flow, } } } - return unsafe{ALPROTO_FAILED}; + return ALPROTO_FAILED; }, Err(nom::Err::Incomplete(_)) => { return ALPROTO_UNKNOWN; }, Err(_) => { - return unsafe{ALPROTO_FAILED}; + return ALPROTO_FAILED; }, } } #[no_mangle] -pub extern "C" fn rs_krb5_probing_parser_tcp(_flow: *const Flow, +pub unsafe extern "C" fn rs_krb5_probing_parser_tcp(_flow: *const Flow, direction: u8, input:*const u8, input_len: u32, rdir: *mut u8) -> AppProto { let slice = build_slice!(input,input_len as usize); - if slice.len() <= 14 { return unsafe{ALPROTO_FAILED}; } + if slice.len() <= 14 { return ALPROTO_FAILED; } match be_u32(slice) as IResult<&[u8],u32> { Ok((rem, record_mark)) => { // protocol implementations forbid very large requests - if record_mark > 16384 { return unsafe{ALPROTO_FAILED}; } + if record_mark > 16384 { return ALPROTO_FAILED; } return rs_krb5_probing_parser(_flow, direction, rem.as_ptr(), rem.len() as u32, rdir); }, @@ -457,13 +453,13 @@ pub extern "C" fn rs_krb5_probing_parser_tcp(_flow: *const Flow, return ALPROTO_UNKNOWN; }, Err(_) => { - return unsafe{ALPROTO_FAILED}; + return ALPROTO_FAILED; }, } } #[no_mangle] -pub extern "C" fn rs_krb5_parse_request(_flow: *const core::Flow, +pub unsafe extern "C" fn rs_krb5_parse_request(_flow: *const core::Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, @@ -479,7 +475,7 @@ pub extern "C" fn rs_krb5_parse_request(_flow: *const core::Flow, } #[no_mangle] -pub extern "C" fn rs_krb5_parse_response(_flow: *const core::Flow, +pub unsafe extern "C" fn rs_krb5_parse_response(_flow: *const core::Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, @@ -495,7 +491,7 @@ pub extern "C" fn rs_krb5_parse_response(_flow: *const core::Flow, } #[no_mangle] -pub extern "C" fn rs_krb5_parse_request_tcp(_flow: *const core::Flow, +pub unsafe extern "C" fn rs_krb5_parse_request_tcp(_flow: *const core::Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, @@ -554,7 +550,7 @@ pub extern "C" fn rs_krb5_parse_request_tcp(_flow: *const core::Flow, } #[no_mangle] -pub extern "C" fn rs_krb5_parse_response_tcp(_flow: *const core::Flow, +pub unsafe extern "C" fn rs_krb5_parse_response_tcp(_flow: *const core::Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, diff --git a/rust/src/modbus/modbus.rs b/rust/src/modbus/modbus.rs index 7e9acdf988d9..e901e3c54146 100644 --- a/rust/src/modbus/modbus.rs +++ b/rust/src/modbus/modbus.rs @@ -349,18 +349,18 @@ pub extern "C" fn rs_modbus_state_free(state: *mut std::os::raw::c_void) { } #[no_mangle] -pub extern "C" fn rs_modbus_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { +pub unsafe extern "C" fn rs_modbus_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { let state = cast_pointer!(state, ModbusState); state.free_tx(tx_id); } #[no_mangle] -pub extern "C" fn rs_modbus_parse_request( +pub unsafe extern "C" fn rs_modbus_parse_request( _flow: *const core::Flow, state: *mut std::os::raw::c_void, pstate: *mut std::os::raw::c_void, input: *const u8, input_len: u32, _data: *const std::os::raw::c_void, _flags: u8, ) -> AppLayerResult { if input_len == 0 { - if unsafe { AppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TS) } > 0 { + if AppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TS) > 0 { return AppLayerResult::ok(); } else { return AppLayerResult::err(); @@ -368,40 +368,38 @@ pub extern "C" fn rs_modbus_parse_request( } let state = cast_pointer!(state, ModbusState); - let buf = unsafe { std::slice::from_raw_parts(input, input_len as usize) }; + let buf = std::slice::from_raw_parts(input, input_len as usize); state.parse(buf, Direction::ToServer) } #[no_mangle] -pub extern "C" fn rs_modbus_parse_response( +pub unsafe extern "C" fn rs_modbus_parse_response( _flow: *const core::Flow, state: *mut std::os::raw::c_void, pstate: *mut std::os::raw::c_void, input: *const u8, input_len: u32, _data: *const std::os::raw::c_void, _flags: u8, ) -> AppLayerResult { if input_len == 0 { - unsafe { - if AppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TC) > 0 { - return AppLayerResult::ok(); - } else { - return AppLayerResult::err(); - } + if AppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TC) > 0 { + return AppLayerResult::ok(); + } else { + return AppLayerResult::err(); } } let state = cast_pointer!(state, ModbusState); - let buf = unsafe { std::slice::from_raw_parts(input, input_len as usize) }; + let buf = std::slice::from_raw_parts(input, input_len as usize); state.parse(buf, Direction::ToClient) } #[no_mangle] -pub extern "C" fn rs_modbus_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { +pub unsafe extern "C" fn rs_modbus_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { let state = cast_pointer!(state, ModbusState); state.tx_id } #[no_mangle] -pub extern "C" fn rs_modbus_state_get_tx( +pub unsafe extern "C" fn rs_modbus_state_get_tx( state: *mut std::os::raw::c_void, tx_id: u64, ) -> *mut std::os::raw::c_void { let state = cast_pointer!(state, ModbusState); @@ -412,7 +410,7 @@ pub extern "C" fn rs_modbus_state_get_tx( } #[no_mangle] -pub extern "C" fn rs_modbus_tx_get_alstate_progress( +pub unsafe extern "C" fn rs_modbus_tx_get_alstate_progress( tx: *mut std::os::raw::c_void, _direction: u8, ) -> std::os::raw::c_int { let tx = cast_pointer!(tx, ModbusTransaction); @@ -420,7 +418,7 @@ pub extern "C" fn rs_modbus_tx_get_alstate_progress( } #[no_mangle] -pub extern "C" fn rs_modbus_state_get_events( +pub unsafe extern "C" fn rs_modbus_state_get_events( tx: *mut std::os::raw::c_void, ) -> *mut core::AppLayerDecoderEvents { let tx = cast_pointer!(tx, ModbusTransaction); @@ -428,7 +426,7 @@ pub extern "C" fn rs_modbus_state_get_events( } #[no_mangle] -pub extern "C" fn rs_modbus_state_get_event_info( +pub unsafe extern "C" fn rs_modbus_state_get_event_info( event_name: *const std::os::raw::c_char, event_id: *mut std::os::raw::c_int, event_type: *mut core::AppLayerEventType, ) -> std::os::raw::c_int { @@ -436,10 +434,10 @@ pub extern "C" fn rs_modbus_state_get_event_info( return -1; } - let event_name = unsafe { std::ffi::CStr::from_ptr(event_name) }; + let event_name = std::ffi::CStr::from_ptr(event_name); if let Ok(event_name) = event_name.to_str() { match ModbusEvent::from_str(event_name) { - Ok(event) => unsafe { + Ok(event) => { *event_id = event as std::os::raw::c_int; *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; 0 @@ -458,15 +456,13 @@ pub extern "C" fn rs_modbus_state_get_event_info( } #[no_mangle] -pub extern "C" fn rs_modbus_state_get_event_info_by_id( +pub unsafe extern "C" fn rs_modbus_state_get_event_info_by_id( event_id: std::os::raw::c_int, event_name: *mut *const std::os::raw::c_char, event_type: *mut core::AppLayerEventType, ) -> i8 { if let Some(e) = ModbusEvent::from_id(event_id as u32) { - unsafe { - *event_name = e.to_str().as_ptr() as *const std::os::raw::c_char; - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - } + *event_name = e.to_str().as_ptr() as *const std::os::raw::c_char; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; 0 } else { SCLogError!("event {} not present in modbus's enum map table.", event_id); @@ -475,7 +471,7 @@ pub extern "C" fn rs_modbus_state_get_event_info_by_id( } #[no_mangle] -pub extern "C" fn rs_modbus_state_get_tx_detect_state( +pub unsafe extern "C" fn rs_modbus_state_get_tx_detect_state( tx: *mut std::os::raw::c_void, ) -> *mut core::DetectEngineState { let tx = cast_pointer!(tx, ModbusTransaction); @@ -486,7 +482,7 @@ pub extern "C" fn rs_modbus_state_get_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_modbus_state_set_tx_detect_state( +pub unsafe extern "C" fn rs_modbus_state_set_tx_detect_state( tx: *mut std::os::raw::c_void, de_state: &mut core::DetectEngineState, ) -> std::os::raw::c_int { let tx = cast_pointer!(tx, ModbusTransaction); @@ -495,7 +491,7 @@ pub extern "C" fn rs_modbus_state_set_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_modbus_state_get_tx_data( +pub unsafe extern "C" fn rs_modbus_state_get_tx_data( tx: *mut std::os::raw::c_void, ) -> *mut AppLayerTxData { let tx = cast_pointer!(tx, ModbusTransaction); @@ -841,7 +837,7 @@ pub mod test { } #[no_mangle] - pub extern "C" fn rs_modbus_state_get_tx_request( + pub unsafe extern "C" fn rs_modbus_state_get_tx_request( state: *mut std::os::raw::c_void, tx_id: u64, ) -> ModbusMessage { let state = cast_pointer!(state, ModbusState); @@ -857,7 +853,7 @@ pub mod test { } #[no_mangle] - pub extern "C" fn rs_modbus_state_get_tx_response( + pub unsafe extern "C" fn rs_modbus_state_get_tx_response( state: *mut std::os::raw::c_void, tx_id: u64, ) -> ModbusMessage { let state = cast_pointer!(state, ModbusState); diff --git a/rust/src/mqtt/detect.rs b/rust/src/mqtt/detect.rs index fc8c297797c7..67460709c08a 100644 --- a/rust/src/mqtt/detect.rs +++ b/rust/src/mqtt/detect.rs @@ -67,15 +67,13 @@ pub extern "C" fn rs_mqtt_tx_has_type( } #[no_mangle] -pub extern "C" fn rs_mqtt_cstr_message_code( +pub unsafe extern "C" fn rs_mqtt_cstr_message_code( str: *const std::os::raw::c_char, ) -> std::os::raw::c_int { - unsafe { - let msgtype: &CStr = CStr::from_ptr(str); - if let Ok(s) = msgtype.to_str() { - if let Ok(x) = MQTTTypeCode::from_str(s) { - return x as i32; - } + let msgtype: &CStr = CStr::from_ptr(str); + if let Ok(s) = msgtype.to_str() { + if let Ok(x) = MQTTTypeCode::from_str(s) { + return x as i32; } } return -1; @@ -145,7 +143,7 @@ pub extern "C" fn rs_mqtt_tx_has_connect_flags( } #[no_mangle] -pub extern "C" fn rs_mqtt_tx_get_connect_clientid( +pub unsafe extern "C" fn rs_mqtt_tx_get_connect_clientid( tx: &MQTTTransaction, buffer: *mut *const u8, buffer_len: *mut u32, @@ -154,24 +152,20 @@ pub extern "C" fn rs_mqtt_tx_get_connect_clientid( if let MQTTOperation::CONNECT(ref cv) = msg.op { let p = &cv.client_id; if p.len() > 0 { - unsafe { - *buffer = p.as_ptr(); - *buffer_len = p.len() as u32; - } + *buffer = p.as_ptr(); + *buffer_len = p.len() as u32; return 1; } } } - unsafe { - *buffer = ptr::null(); - *buffer_len = 0; - } + *buffer = ptr::null(); + *buffer_len = 0; return 0; } #[no_mangle] -pub extern "C" fn rs_mqtt_tx_get_connect_username( +pub unsafe extern "C" fn rs_mqtt_tx_get_connect_username( tx: &MQTTTransaction, buffer: *mut *const u8, buffer_len: *mut u32, @@ -180,26 +174,22 @@ pub extern "C" fn rs_mqtt_tx_get_connect_username( if let MQTTOperation::CONNECT(ref cv) = msg.op { if let Some(p) = &cv.username { if p.len() > 0 { - unsafe { - *buffer = p.as_ptr(); - *buffer_len = p.len() as u32; - } + *buffer = p.as_ptr(); + *buffer_len = p.len() as u32; return 1; } } } } - unsafe { - *buffer = ptr::null(); - *buffer_len = 0; - } + *buffer = ptr::null(); + *buffer_len = 0; return 0; } #[no_mangle] -pub extern "C" fn rs_mqtt_tx_get_connect_password( +pub unsafe extern "C" fn rs_mqtt_tx_get_connect_password( tx: &MQTTTransaction, buffer: *mut *const u8, buffer_len: *mut u32, @@ -208,25 +198,21 @@ pub extern "C" fn rs_mqtt_tx_get_connect_password( if let MQTTOperation::CONNECT(ref cv) = msg.op { if let Some(p) = &cv.password { if p.len() > 0 { - unsafe { - *buffer = p.as_ptr(); - *buffer_len = p.len() as u32; - } + *buffer = p.as_ptr(); + *buffer_len = p.len() as u32; return 1; } } } } - unsafe { - *buffer = ptr::null(); - *buffer_len = 0; - } + *buffer = ptr::null(); + *buffer_len = 0; return 0; } #[no_mangle] -pub extern "C" fn rs_mqtt_tx_get_connect_willtopic( +pub unsafe extern "C" fn rs_mqtt_tx_get_connect_willtopic( tx: &MQTTTransaction, buffer: *mut *const u8, buffer_len: *mut u32, @@ -235,26 +221,22 @@ pub extern "C" fn rs_mqtt_tx_get_connect_willtopic( if let MQTTOperation::CONNECT(ref cv) = msg.op { if let Some(p) = &cv.will_topic { if p.len() > 0 { - unsafe { - *buffer = p.as_ptr(); - *buffer_len = p.len() as u32; - } + *buffer = p.as_ptr(); + *buffer_len = p.len() as u32; return 1; } } } } - unsafe { - *buffer = ptr::null(); - *buffer_len = 0; - } + *buffer = ptr::null(); + *buffer_len = 0; return 0; } #[no_mangle] -pub extern "C" fn rs_mqtt_tx_get_connect_willmessage( +pub unsafe extern "C" fn rs_mqtt_tx_get_connect_willmessage( tx: &MQTTTransaction, buffer: *mut *const u8, buffer_len: *mut u32, @@ -263,34 +245,28 @@ pub extern "C" fn rs_mqtt_tx_get_connect_willmessage( if let MQTTOperation::CONNECT(ref cv) = msg.op { if let Some(p) = &cv.will_message { if p.len() > 0 { - unsafe { - *buffer = p.as_ptr(); - *buffer_len = p.len() as u32; - } + *buffer = p.as_ptr(); + *buffer_len = p.len() as u32; return 1; } } } } - unsafe { - *buffer = ptr::null(); - *buffer_len = 0; - } + *buffer = ptr::null(); + *buffer_len = 0; return 0; } #[no_mangle] -pub extern "C" fn rs_mqtt_tx_get_connack_sessionpresent( +pub unsafe extern "C" fn rs_mqtt_tx_get_connack_sessionpresent( tx: &MQTTTransaction, session_present: *mut bool, ) -> u8 { for msg in tx.msg.iter() { if let MQTTOperation::CONNACK(ref ca) = msg.op { - unsafe { - *session_present = ca.session_present; - } + *session_present = ca.session_present; return 1; } } @@ -298,7 +274,7 @@ pub extern "C" fn rs_mqtt_tx_get_connack_sessionpresent( } #[no_mangle] -pub extern "C" fn rs_mqtt_tx_get_publish_topic( +pub unsafe extern "C" fn rs_mqtt_tx_get_publish_topic( tx: &MQTTTransaction, buffer: *mut *const u8, buffer_len: *mut u32, @@ -307,25 +283,21 @@ pub extern "C" fn rs_mqtt_tx_get_publish_topic( if let MQTTOperation::PUBLISH(ref pubv) = msg.op { let p = &pubv.topic; if p.len() > 0 { - unsafe { - *buffer = p.as_ptr(); - *buffer_len = p.len() as u32; - } + *buffer = p.as_ptr(); + *buffer_len = p.len() as u32; return 1; } } } - unsafe { - *buffer = ptr::null(); - *buffer_len = 0; - } + *buffer = ptr::null(); + *buffer_len = 0; return 0; } #[no_mangle] -pub extern "C" fn rs_mqtt_tx_get_publish_message( +pub unsafe extern "C" fn rs_mqtt_tx_get_publish_message( tx: &MQTTTransaction, buffer: *mut *const u8, buffer_len: *mut u32, @@ -334,25 +306,21 @@ pub extern "C" fn rs_mqtt_tx_get_publish_message( if let MQTTOperation::PUBLISH(ref pubv) = msg.op { let p = &pubv.message; if p.len() > 0 { - unsafe { - *buffer = p.as_ptr(); - *buffer_len = p.len() as u32; - } + *buffer = p.as_ptr(); + *buffer_len = p.len() as u32; return 1; } } } - unsafe { - *buffer = ptr::null(); - *buffer_len = 0; - } + *buffer = ptr::null(); + *buffer_len = 0; return 0; } #[no_mangle] -pub extern "C" fn rs_mqtt_tx_get_subscribe_topic(tx: &MQTTTransaction, +pub unsafe extern "C" fn rs_mqtt_tx_get_subscribe_topic(tx: &MQTTTransaction, i: u32, buf: *mut *const u8, len: *mut u32) @@ -364,10 +332,8 @@ pub extern "C" fn rs_mqtt_tx_get_subscribe_topic(tx: &MQTTTransaction, if (i as usize) < subv.topics.len() + offset { let topic = &subv.topics[(i as usize) - offset]; if topic.topic_name.len() > 0 { - unsafe { - *len = topic.topic_name.len() as u32; - *buf = topic.topic_name.as_ptr(); - } + *len = topic.topic_name.len() as u32; + *buf = topic.topic_name.as_ptr(); return 1; } } else { @@ -376,16 +342,14 @@ pub extern "C" fn rs_mqtt_tx_get_subscribe_topic(tx: &MQTTTransaction, } } - unsafe { - *buf = ptr::null(); - *len = 0; - } + *buf = ptr::null(); + *len = 0; return 0; } #[no_mangle] -pub extern "C" fn rs_mqtt_tx_get_unsubscribe_topic(tx: &MQTTTransaction, +pub unsafe extern "C" fn rs_mqtt_tx_get_unsubscribe_topic(tx: &MQTTTransaction, i: u32, buf: *mut *const u8, len: *mut u32) @@ -397,10 +361,8 @@ pub extern "C" fn rs_mqtt_tx_get_unsubscribe_topic(tx: &MQTTTransaction, if (i as usize) < unsubv.topics.len() + offset { let topic = &unsubv.topics[(i as usize) - offset]; if topic.len() > 0 { - unsafe { - *len = topic.len() as u32; - *buf = topic.as_ptr(); - } + *len = topic.len() as u32; + *buf = topic.as_ptr(); return 1; } } else { @@ -409,16 +371,14 @@ pub extern "C" fn rs_mqtt_tx_get_unsubscribe_topic(tx: &MQTTTransaction, } } - unsafe { - *buf = ptr::null(); - *len = 0; - } + *buf = ptr::null(); + *len = 0; return 0; } #[no_mangle] -pub extern "C" fn rs_mqtt_tx_get_reason_code( +pub unsafe extern "C" fn rs_mqtt_tx_get_reason_code( tx: &MQTTTransaction, result: *mut u8, ) -> u8 { @@ -429,29 +389,21 @@ pub extern "C" fn rs_mqtt_tx_get_reason_code( | MQTTOperation::PUBREC(ref v) | MQTTOperation::PUBCOMP(ref v) => { if let Some(rcode) = v.reason_code { - unsafe { - *result = rcode; - } + *result = rcode; return 1; } } MQTTOperation::AUTH(ref v) => { - unsafe { - *result = v.reason_code; - } + *result = v.reason_code; return 1; } MQTTOperation::CONNACK(ref v) => { - unsafe { - *result = v.return_code; - } + *result = v.return_code; return 1; } MQTTOperation::DISCONNECT(ref v) => { if let Some(rcode) = v.reason_code { - unsafe { - *result = rcode; - } + *result = rcode; return 1; } } @@ -520,23 +472,23 @@ mod test { }); let mut s: *const u8 = std::ptr::null_mut(); let mut slen: u32 = 0; - let mut r = rs_mqtt_tx_get_unsubscribe_topic(&t, 0, &mut s, &mut slen); + let mut r = unsafe{rs_mqtt_tx_get_unsubscribe_topic(&t, 0, &mut s, &mut slen)}; assert_eq!(r, 1); - let mut topic = String::from_utf8_lossy(build_slice!(s, slen as usize)); + let mut topic = String::from_utf8_lossy(unsafe{build_slice!(s, slen as usize)}); assert_eq!(topic, "foo"); - r = rs_mqtt_tx_get_unsubscribe_topic(&t, 1, &mut s, &mut slen); + r = unsafe{rs_mqtt_tx_get_unsubscribe_topic(&t, 1, &mut s, &mut slen)}; assert_eq!(r, 1); - topic = String::from_utf8_lossy(build_slice!(s, slen as usize)); + topic = String::from_utf8_lossy(unsafe{build_slice!(s, slen as usize)}); assert_eq!(topic, "baar"); - r = rs_mqtt_tx_get_unsubscribe_topic(&t, 2, &mut s, &mut slen); + r = unsafe{rs_mqtt_tx_get_unsubscribe_topic(&t, 2, &mut s, &mut slen)}; assert_eq!(r, 1); - topic = String::from_utf8_lossy(build_slice!(s, slen as usize)); + topic = String::from_utf8_lossy(unsafe{build_slice!(s, slen as usize)}); assert_eq!(topic, "fieee"); - r = rs_mqtt_tx_get_unsubscribe_topic(&t, 3, &mut s, &mut slen); + r = unsafe{rs_mqtt_tx_get_unsubscribe_topic(&t, 3, &mut s, &mut slen)}; assert_eq!(r, 1); - topic = String::from_utf8_lossy(build_slice!(s, slen as usize)); + topic = String::from_utf8_lossy(unsafe{build_slice!(s, slen as usize)}); assert_eq!(topic, "baaaaz"); - r = rs_mqtt_tx_get_unsubscribe_topic(&t, 4, &mut s, &mut slen); + r = unsafe{rs_mqtt_tx_get_unsubscribe_topic(&t, 4, &mut s, &mut slen)}; assert_eq!(r, 0); } @@ -588,23 +540,23 @@ mod test { }); let mut s: *const u8 = std::ptr::null_mut(); let mut slen: u32 = 0; - let mut r = rs_mqtt_tx_get_subscribe_topic(&t, 0, &mut s, &mut slen); + let mut r = unsafe{rs_mqtt_tx_get_subscribe_topic(&t, 0, &mut s, &mut slen)}; assert_eq!(r, 1); - let mut topic = String::from_utf8_lossy(build_slice!(s, slen as usize)); + let mut topic = String::from_utf8_lossy(unsafe{build_slice!(s, slen as usize)}); assert_eq!(topic, "foo"); - r = rs_mqtt_tx_get_subscribe_topic(&t, 1, &mut s, &mut slen); + r = unsafe{rs_mqtt_tx_get_subscribe_topic(&t, 1, &mut s, &mut slen)}; assert_eq!(r, 1); - topic = String::from_utf8_lossy(build_slice!(s, slen as usize)); + topic = String::from_utf8_lossy(unsafe{build_slice!(s, slen as usize)}); assert_eq!(topic, "baar"); - r = rs_mqtt_tx_get_subscribe_topic(&t, 2, &mut s, &mut slen); + r = unsafe{rs_mqtt_tx_get_subscribe_topic(&t, 2, &mut s, &mut slen)}; assert_eq!(r, 1); - topic = String::from_utf8_lossy(build_slice!(s, slen as usize)); + topic = String::from_utf8_lossy(unsafe{build_slice!(s, slen as usize)}); assert_eq!(topic, "fieee"); - r = rs_mqtt_tx_get_subscribe_topic(&t, 3, &mut s, &mut slen); + r = unsafe{rs_mqtt_tx_get_subscribe_topic(&t, 3, &mut s, &mut slen)}; assert_eq!(r, 1); - topic = String::from_utf8_lossy(build_slice!(s, slen as usize)); + topic = String::from_utf8_lossy(unsafe{build_slice!(s, slen as usize)}); assert_eq!(topic, "baaaaz"); - r = rs_mqtt_tx_get_subscribe_topic(&t, 4, &mut s, &mut slen); + r = unsafe{rs_mqtt_tx_get_subscribe_topic(&t, 4, &mut s, &mut slen)}; assert_eq!(r, 0); } } diff --git a/rust/src/mqtt/logger.rs b/rust/src/mqtt/logger.rs index 71b7fe0d633e..bda119aae67c 100644 --- a/rust/src/mqtt/logger.rs +++ b/rust/src/mqtt/logger.rs @@ -297,7 +297,7 @@ fn log_mqtt(tx: &MQTTTransaction, flags: u32, js: &mut JsonBuilder) -> Result<() } #[no_mangle] -pub extern "C" fn rs_mqtt_logger_log(_state: &mut MQTTState, tx: *mut std::os::raw::c_void, flags: u32, js: &mut JsonBuilder) -> bool { +pub unsafe extern "C" fn rs_mqtt_logger_log(_state: &mut MQTTState, tx: *mut std::os::raw::c_void, flags: u32, js: &mut JsonBuilder) -> bool { let tx = cast_pointer!(tx, MQTTTransaction); log_mqtt(tx, flags, js).is_ok() } diff --git a/rust/src/mqtt/mqtt.rs b/rust/src/mqtt/mqtt.rs index 1ced80ee34d9..0f3d62babdfd 100644 --- a/rust/src/mqtt/mqtt.rs +++ b/rust/src/mqtt/mqtt.rs @@ -551,7 +551,7 @@ export_tx_get_detect_state!(rs_mqtt_tx_get_detect_state, MQTTTransaction); export_tx_set_detect_state!(rs_mqtt_tx_set_detect_state, MQTTTransaction); #[no_mangle] -pub extern "C" fn rs_mqtt_probing_parser( +pub unsafe extern "C" fn rs_mqtt_probing_parser( _flow: *const Flow, _direction: u8, input: *const u8, @@ -563,16 +563,16 @@ pub extern "C" fn rs_mqtt_probing_parser( Ok((_, hdr)) => { // reject unassigned message type if hdr.message_type == MQTTTypeCode::UNASSIGNED { - return unsafe { ALPROTO_FAILED } ; + return ALPROTO_FAILED; } // with 2 being the highest valid QoS level if hdr.qos_level > 2 { - return unsafe { ALPROTO_FAILED }; + return ALPROTO_FAILED; } - return unsafe { ALPROTO_MQTT }; + return ALPROTO_MQTT; }, Err(nom::Err::Incomplete(_)) => ALPROTO_UNKNOWN, - Err(_) => unsafe { ALPROTO_FAILED } + Err(_) => ALPROTO_FAILED } } @@ -589,13 +589,13 @@ pub extern "C" fn rs_mqtt_state_free(state: *mut std::os::raw::c_void) { } #[no_mangle] -pub extern "C" fn rs_mqtt_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { +pub unsafe extern "C" fn rs_mqtt_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { let state = cast_pointer!(state, MQTTState); state.free_tx(tx_id); } #[no_mangle] -pub extern "C" fn rs_mqtt_parse_request( +pub unsafe extern "C" fn rs_mqtt_parse_request( _flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, @@ -610,7 +610,7 @@ pub extern "C" fn rs_mqtt_parse_request( } #[no_mangle] -pub extern "C" fn rs_mqtt_parse_response( +pub unsafe extern "C" fn rs_mqtt_parse_response( _flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, @@ -625,7 +625,7 @@ pub extern "C" fn rs_mqtt_parse_response( } #[no_mangle] -pub extern "C" fn rs_mqtt_state_get_tx( +pub unsafe extern "C" fn rs_mqtt_state_get_tx( state: *mut std::os::raw::c_void, tx_id: u64, ) -> *mut std::os::raw::c_void { @@ -641,13 +641,13 @@ pub extern "C" fn rs_mqtt_state_get_tx( } #[no_mangle] -pub extern "C" fn rs_mqtt_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { +pub unsafe extern "C" fn rs_mqtt_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { let state = cast_pointer!(state, MQTTState); return state.tx_id; } #[no_mangle] -pub extern "C" fn rs_mqtt_tx_is_toclient(tx: *const std::os::raw::c_void) -> std::os::raw::c_int { +pub unsafe extern "C" fn rs_mqtt_tx_is_toclient(tx: *const std::os::raw::c_void) -> std::os::raw::c_int { let tx = cast_pointer!(tx, MQTTTransaction); if tx.toclient { return 1; @@ -656,7 +656,7 @@ pub extern "C" fn rs_mqtt_tx_is_toclient(tx: *const std::os::raw::c_void) -> std } #[no_mangle] -pub extern "C" fn rs_mqtt_tx_get_alstate_progress( +pub unsafe extern "C" fn rs_mqtt_tx_get_alstate_progress( tx: *mut std::os::raw::c_void, direction: u8, ) -> std::os::raw::c_int { @@ -676,7 +676,7 @@ pub extern "C" fn rs_mqtt_tx_get_alstate_progress( } #[no_mangle] -pub extern "C" fn rs_mqtt_tx_get_logged( +pub unsafe extern "C" fn rs_mqtt_tx_get_logged( _state: *mut std::os::raw::c_void, tx: *mut std::os::raw::c_void, ) -> u32 { @@ -685,7 +685,7 @@ pub extern "C" fn rs_mqtt_tx_get_logged( } #[no_mangle] -pub extern "C" fn rs_mqtt_tx_set_logged( +pub unsafe extern "C" fn rs_mqtt_tx_set_logged( _state: *mut std::os::raw::c_void, tx: *mut std::os::raw::c_void, logged: u32, @@ -695,7 +695,7 @@ pub extern "C" fn rs_mqtt_tx_set_logged( } #[no_mangle] -pub extern "C" fn rs_mqtt_state_get_events( +pub unsafe extern "C" fn rs_mqtt_state_get_events( tx: *mut std::os::raw::c_void, ) -> *mut core::AppLayerDecoderEvents { let tx = cast_pointer!(tx, MQTTTransaction); @@ -703,7 +703,7 @@ pub extern "C" fn rs_mqtt_state_get_events( } #[no_mangle] -pub extern "C" fn rs_mqtt_state_get_event_info_by_id(event_id: std::os::raw::c_int, +pub unsafe extern "C" fn rs_mqtt_state_get_event_info_by_id(event_id: std::os::raw::c_int, event_name: *mut *const std::os::raw::c_char, event_type: *mut core::AppLayerEventType) -> i8 @@ -720,10 +720,8 @@ pub extern "C" fn rs_mqtt_state_get_event_info_by_id(event_id: std::os::raw::c_i MQTTEvent::MissingMsgId => { "missing_msg_id\0" }, MQTTEvent::UnassignedMsgtype => { "unassigned_msg_type\0" }, }; - unsafe{ - *event_name = estr.as_ptr() as *const std::os::raw::c_char; - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - }; + *event_name = estr.as_ptr() as *const std::os::raw::c_char; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; 0 } else { -1 @@ -731,13 +729,13 @@ pub extern "C" fn rs_mqtt_state_get_event_info_by_id(event_id: std::os::raw::c_i } #[no_mangle] -pub extern "C" fn rs_mqtt_state_get_event_info(event_name: *const std::os::raw::c_char, +pub unsafe extern "C" fn rs_mqtt_state_get_event_info(event_name: *const std::os::raw::c_char, event_id: *mut std::os::raw::c_int, event_type: *mut core::AppLayerEventType) -> std::os::raw::c_int { if event_name == std::ptr::null() { return -1; } - let c_event_name: &CStr = unsafe { CStr::from_ptr(event_name) }; + let c_event_name: &CStr = CStr::from_ptr(event_name); let event = match c_event_name.to_str() { Ok(s) => { match s { @@ -755,15 +753,13 @@ pub extern "C" fn rs_mqtt_state_get_event_info(event_name: *const std::os::raw:: }, Err(_) => -1, // UTF-8 conversion failed }; - unsafe{ - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - *event_id = event as std::os::raw::c_int; - }; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; + *event_id = event as std::os::raw::c_int; 0 } #[no_mangle] -pub extern "C" fn rs_mqtt_state_get_tx_iterator( +pub unsafe extern "C" fn rs_mqtt_state_get_tx_iterator( _ipproto: u8, _alproto: AppProto, state: *mut std::os::raw::c_void, diff --git a/rust/src/nfs/nfs.rs b/rust/src/nfs/nfs.rs index 2c3f18f7c226..f22483ae9f51 100644 --- a/rust/src/nfs/nfs.rs +++ b/rust/src/nfs/nfs.rs @@ -1390,7 +1390,7 @@ pub extern "C" fn rs_nfs_state_free(state: *mut std::os::raw::c_void) { /// C binding parse a NFS TCP request. Returns 1 on success, -1 on failure. #[no_mangle] -pub extern "C" fn rs_nfs_parse_request(flow: *const Flow, +pub unsafe extern "C" fn rs_nfs_parse_request(flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, @@ -1401,13 +1401,13 @@ pub extern "C" fn rs_nfs_parse_request(flow: *const Flow, { let state = cast_pointer!(state, NFSState); let flow = cast_pointer!(flow, Flow); - let file_flags = unsafe { FileFlowToFlags(flow, STREAM_TOSERVER) }; + let file_flags = FileFlowToFlags(flow, STREAM_TOSERVER); rs_nfs_setfileflags(STREAM_TOSERVER, state, file_flags); if input.is_null() == true && input_len > 0 { return rs_nfs_parse_request_tcp_gap(state, input_len); } - let buf = unsafe{std::slice::from_raw_parts(input, input_len as usize)}; + let buf = std::slice::from_raw_parts(input, input_len as usize); SCLogDebug!("parsing {} bytes of request data", input_len); state.update_ts(flow.get_last_time().as_secs()); @@ -1424,7 +1424,7 @@ pub extern "C" fn rs_nfs_parse_request_tcp_gap( } #[no_mangle] -pub extern "C" fn rs_nfs_parse_response(flow: *const Flow, +pub unsafe extern "C" fn rs_nfs_parse_response(flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, @@ -1435,14 +1435,14 @@ pub extern "C" fn rs_nfs_parse_response(flow: *const Flow, { let state = cast_pointer!(state, NFSState); let flow = cast_pointer!(flow, Flow); - let file_flags = unsafe { FileFlowToFlags(flow, STREAM_TOCLIENT) }; + let file_flags = FileFlowToFlags(flow, STREAM_TOCLIENT); rs_nfs_setfileflags(STREAM_TOCLIENT, state, file_flags); if input.is_null() == true && input_len > 0 { return rs_nfs_parse_response_tcp_gap(state, input_len); } SCLogDebug!("parsing {} bytes of response data", input_len); - let buf = unsafe{std::slice::from_raw_parts(input, input_len as usize)}; + let buf = std::slice::from_raw_parts(input, input_len as usize); state.update_ts(flow.get_last_time().as_secs()); state.parse_tcp_data_tc(buf) @@ -1459,7 +1459,7 @@ pub extern "C" fn rs_nfs_parse_response_tcp_gap( /// C binding to parse an NFS/UDP request. Returns 1 on success, -1 on failure. #[no_mangle] -pub extern "C" fn rs_nfs_parse_request_udp(f: *const Flow, +pub unsafe extern "C" fn rs_nfs_parse_request_udp(f: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, @@ -1468,16 +1468,16 @@ pub extern "C" fn rs_nfs_parse_request_udp(f: *const Flow, _flags: u8) -> AppLayerResult { let state = cast_pointer!(state, NFSState); - let file_flags = unsafe { FileFlowToFlags(f, STREAM_TOSERVER) }; + let file_flags = FileFlowToFlags(f, STREAM_TOSERVER); rs_nfs_setfileflags(STREAM_TOSERVER, state, file_flags); - let buf = unsafe{std::slice::from_raw_parts(input, input_len as usize)}; + let buf = std::slice::from_raw_parts(input, input_len as usize); SCLogDebug!("parsing {} bytes of request data", input_len); state.parse_udp_ts(buf) } #[no_mangle] -pub extern "C" fn rs_nfs_parse_response_udp(f: *const Flow, +pub unsafe extern "C" fn rs_nfs_parse_response_udp(f: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, @@ -1486,15 +1486,15 @@ pub extern "C" fn rs_nfs_parse_response_udp(f: *const Flow, _flags: u8) -> AppLayerResult { let state = cast_pointer!(state, NFSState); - let file_flags = unsafe { FileFlowToFlags(f, STREAM_TOCLIENT) }; + let file_flags = FileFlowToFlags(f, STREAM_TOCLIENT); rs_nfs_setfileflags(STREAM_TOCLIENT, state, file_flags); SCLogDebug!("parsing {} bytes of response data", input_len); - let buf = unsafe{std::slice::from_raw_parts(input, input_len as usize)}; + let buf = std::slice::from_raw_parts(input, input_len as usize); state.parse_udp_tc(buf) } #[no_mangle] -pub extern "C" fn rs_nfs_state_get_tx_count(state: *mut std::os::raw::c_void) +pub unsafe extern "C" fn rs_nfs_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { let state = cast_pointer!(state, NFSState); @@ -1503,7 +1503,7 @@ pub extern "C" fn rs_nfs_state_get_tx_count(state: *mut std::os::raw::c_void) } #[no_mangle] -pub extern "C" fn rs_nfs_state_get_tx(state: *mut std::os::raw::c_void, +pub unsafe extern "C" fn rs_nfs_state_get_tx(state: *mut std::os::raw::c_void, tx_id: u64) -> *mut std::os::raw::c_void { @@ -1520,7 +1520,7 @@ pub extern "C" fn rs_nfs_state_get_tx(state: *mut std::os::raw::c_void, // for use with the C API call StateGetTxIterator #[no_mangle] -pub extern "C" fn rs_nfs_state_get_tx_iterator( +pub unsafe extern "C" fn rs_nfs_state_get_tx_iterator( _ipproto: u8, _alproto: AppProto, state: *mut std::os::raw::c_void, @@ -1543,7 +1543,7 @@ pub extern "C" fn rs_nfs_state_get_tx_iterator( } #[no_mangle] -pub extern "C" fn rs_nfs_state_tx_free(state: *mut std::os::raw::c_void, +pub unsafe extern "C" fn rs_nfs_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { let state = cast_pointer!(state, NFSState); @@ -1551,7 +1551,7 @@ pub extern "C" fn rs_nfs_state_tx_free(state: *mut std::os::raw::c_void, } #[no_mangle] -pub extern "C" fn rs_nfs_tx_get_alstate_progress(tx: *mut std::os::raw::c_void, +pub unsafe extern "C" fn rs_nfs_tx_get_alstate_progress(tx: *mut std::os::raw::c_void, direction: u8) -> std::os::raw::c_int { @@ -1569,7 +1569,7 @@ pub extern "C" fn rs_nfs_tx_get_alstate_progress(tx: *mut std::os::raw::c_void, } #[no_mangle] -pub extern "C" fn rs_nfs_get_tx_data( +pub unsafe extern "C" fn rs_nfs_get_tx_data( tx: *mut std::os::raw::c_void) -> *mut AppLayerTxData { @@ -1578,7 +1578,7 @@ pub extern "C" fn rs_nfs_get_tx_data( } #[no_mangle] -pub extern "C" fn rs_nfs_state_set_tx_detect_state( +pub unsafe extern "C" fn rs_nfs_state_set_tx_detect_state( tx: *mut std::os::raw::c_void, de_state: &mut DetectEngineState) -> i32 { @@ -1588,7 +1588,7 @@ pub extern "C" fn rs_nfs_state_set_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_nfs_state_get_tx_detect_state( +pub unsafe extern "C" fn rs_nfs_state_get_tx_detect_state( tx: *mut std::os::raw::c_void) -> *mut DetectEngineState { @@ -1606,7 +1606,7 @@ pub extern "C" fn rs_nfs_state_get_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_nfs_state_get_events(tx: *mut std::os::raw::c_void) +pub unsafe extern "C" fn rs_nfs_state_get_events(tx: *mut std::os::raw::c_void) -> *mut AppLayerDecoderEvents { let tx = cast_pointer!(tx, NFSTransaction); @@ -1614,7 +1614,7 @@ pub extern "C" fn rs_nfs_state_get_events(tx: *mut std::os::raw::c_void) } #[no_mangle] -pub extern "C" fn rs_nfs_state_get_event_info_by_id(event_id: std::os::raw::c_int, +pub unsafe extern "C" fn rs_nfs_state_get_event_info_by_id(event_id: std::os::raw::c_int, event_name: *mut *const std::os::raw::c_char, event_type: *mut AppLayerEventType) -> i8 @@ -1625,10 +1625,8 @@ pub extern "C" fn rs_nfs_state_get_event_info_by_id(event_id: std::os::raw::c_in NFSEvent::NonExistingVersion => { "non_existing_version\0" }, NFSEvent::UnsupportedVersion => { "unsupported_version\0" }, }; - unsafe{ - *event_name = estr.as_ptr() as *const std::os::raw::c_char; - *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION; - }; + *event_name = estr.as_ptr() as *const std::os::raw::c_char; + *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION; 0 } else { -1 @@ -1637,7 +1635,7 @@ pub extern "C" fn rs_nfs_state_get_event_info_by_id(event_id: std::os::raw::c_in #[no_mangle] -pub extern "C" fn rs_nfs_state_get_event_info(event_name: *const std::os::raw::c_char, +pub unsafe extern "C" fn rs_nfs_state_get_event_info(event_name: *const std::os::raw::c_char, event_id: *mut std::os::raw::c_int, event_type: *mut AppLayerEventType) -> std::os::raw::c_int @@ -1645,7 +1643,7 @@ pub extern "C" fn rs_nfs_state_get_event_info(event_name: *const std::os::raw::c if event_name == std::ptr::null() { return -1; } - let c_event_name: &CStr = unsafe { CStr::from_ptr(event_name) }; + let c_event_name: &CStr = CStr::from_ptr(event_name); let event = match c_event_name.to_str() { Ok(s) => { match s { @@ -1655,10 +1653,8 @@ pub extern "C" fn rs_nfs_state_get_event_info(event_name: *const std::os::raw::c }, Err(_) => -1, // UTF-8 conversion failed }; - unsafe{ - *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION; - *event_id = event as std::os::raw::c_int; - }; + *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION; + *event_id = event as std::os::raw::c_int; 0 } @@ -1666,15 +1662,13 @@ pub extern "C" fn rs_nfs_state_get_event_info(event_name: *const std::os::raw::c /// otherwise get procs from the 'file_additional_procs'. /// Keep calling until 0 is returned. #[no_mangle] -pub extern "C" fn rs_nfs_tx_get_procedures(tx: &mut NFSTransaction, +pub unsafe extern "C" fn rs_nfs_tx_get_procedures(tx: &mut NFSTransaction, i: u16, procedure: *mut u32) -> u8 { if i == 0 { - unsafe { - *procedure = tx.procedure as u32; - } + *procedure = tx.procedure as u32; return 1; } @@ -1688,9 +1682,7 @@ pub extern "C" fn rs_nfs_tx_get_procedures(tx: &mut NFSTransaction, let idx = i as usize - 1; if idx < tdf.file_additional_procs.len() { let p = tdf.file_additional_procs[idx]; - unsafe { - *procedure = p as u32; - } + *procedure = p as u32; return 1; } } @@ -1698,20 +1690,16 @@ pub extern "C" fn rs_nfs_tx_get_procedures(tx: &mut NFSTransaction, } #[no_mangle] -pub extern "C" fn rs_nfs_tx_get_version(tx: &mut NFSTransaction, +pub unsafe extern "C" fn rs_nfs_tx_get_version(tx: &mut NFSTransaction, version: *mut u32) { - unsafe { - *version = tx.nfs_version as u32; - } + *version = tx.nfs_version as u32; } #[no_mangle] -pub extern "C" fn rs_nfs_init(context: &'static mut SuricataFileContext) +pub unsafe extern "C" fn rs_nfs_init(context: &'static mut SuricataFileContext) { - unsafe { - SURICATA_NFS_FILE_CONFIG = Some(context); - } + SURICATA_NFS_FILE_CONFIG = Some(context); } fn nfs_probe_dir(i: &[u8], rdir: *mut u8) -> i8 { @@ -1826,7 +1814,7 @@ pub fn nfs_probe_udp(i: &[u8], direction: u8) -> i32 { /// MIDSTREAM #[no_mangle] -pub extern "C" fn rs_nfs_probe_ms( +pub unsafe extern "C" fn rs_nfs_probe_ms( _flow: *const Flow, direction: u8, input: *const u8, len: u32, rdir: *mut u8) -> AppProto @@ -1845,25 +1833,25 @@ pub extern "C" fn rs_nfs_probe_ms( 1 => { SCLogDebug!("nfs_probe success: dir {:02x} adir {:02x}", direction, adirection); if (direction & (STREAM_TOSERVER|STREAM_TOCLIENT)) != adirection { - unsafe { *rdir = adirection; } + *rdir = adirection; } - unsafe { ALPROTO_NFS } + ALPROTO_NFS }, 0 => { ALPROTO_UNKNOWN }, - _ => { unsafe { ALPROTO_FAILED } }, + _ => { ALPROTO_FAILED }, } }, 0 => { ALPROTO_UNKNOWN }, _ => { - unsafe { ALPROTO_FAILED } + ALPROTO_FAILED } } } #[no_mangle] -pub extern "C" fn rs_nfs_probe(_f: *const Flow, +pub unsafe extern "C" fn rs_nfs_probe(_f: *const Flow, direction: u8, input: *const u8, len: u32, @@ -1873,15 +1861,15 @@ pub extern "C" fn rs_nfs_probe(_f: *const Flow, let slice: &[u8] = build_slice!(input, len as usize); SCLogDebug!("rs_nfs_probe: running probe"); match nfs_probe(slice, direction) { - 1 => { unsafe { ALPROTO_NFS } }, - -1 => { unsafe { ALPROTO_FAILED } }, + 1 => { ALPROTO_NFS }, + -1 => { ALPROTO_FAILED }, _ => { ALPROTO_UNKNOWN }, } } /// TOSERVER probe function #[no_mangle] -pub extern "C" fn rs_nfs_probe_udp_ts(_f: *const Flow, +pub unsafe extern "C" fn rs_nfs_probe_udp_ts(_f: *const Flow, _direction: u8, input: *const u8, len: u32, @@ -1890,15 +1878,15 @@ pub extern "C" fn rs_nfs_probe_udp_ts(_f: *const Flow, { let slice: &[u8] = build_slice!(input, len as usize); match nfs_probe_udp(slice, STREAM_TOSERVER) { - 1 => { unsafe { ALPROTO_NFS } }, - -1 => { unsafe { ALPROTO_FAILED } }, + 1 => { ALPROTO_NFS }, + -1 => { ALPROTO_FAILED }, _ => { ALPROTO_UNKNOWN }, } } /// TOCLIENT probe function #[no_mangle] -pub extern "C" fn rs_nfs_probe_udp_tc(_f: *const Flow, +pub unsafe extern "C" fn rs_nfs_probe_udp_tc(_f: *const Flow, _direction: u8, input: *const u8, len: u32, @@ -1907,22 +1895,22 @@ pub extern "C" fn rs_nfs_probe_udp_tc(_f: *const Flow, { let slice: &[u8] = build_slice!(input, len as usize); match nfs_probe_udp(slice, STREAM_TOCLIENT) { - 1 => { unsafe { ALPROTO_NFS } }, - -1 => { unsafe { ALPROTO_FAILED } }, + 1 => { ALPROTO_NFS }, + -1 => { ALPROTO_FAILED }, _ => { ALPROTO_UNKNOWN }, } } #[no_mangle] -pub extern "C" fn rs_nfs_getfiles(ptr: *mut std::ffi::c_void, direction: u8) -> * mut FileContainer { +pub unsafe extern "C" fn rs_nfs_getfiles(ptr: *mut std::ffi::c_void, direction: u8) -> * mut FileContainer { if ptr.is_null() { panic!("NULL ptr"); }; let parser = cast_pointer!(ptr, NFSState); parser.getfiles(direction) } #[no_mangle] -pub extern "C" fn rs_nfs_setfileflags(direction: u8, ptr: *mut NFSState, flags: u16) { +pub unsafe extern "C" fn rs_nfs_setfileflags(direction: u8, ptr: *mut NFSState, flags: u16) { if ptr.is_null() { panic!("NULL ptr"); }; - let parser = unsafe { &mut *ptr }; + let parser = &mut *ptr; SCLogDebug!("direction {} flags {}", direction, flags); parser.setfileflags(direction, flags) } diff --git a/rust/src/ntp/ntp.rs b/rust/src/ntp/ntp.rs index 7d6ce269d1f4..61283099eed6 100644 --- a/rust/src/ntp/ntp.rs +++ b/rust/src/ntp/ntp.rs @@ -190,7 +190,7 @@ pub extern "C" fn rs_ntp_state_free(state: *mut std::os::raw::c_void) { } #[no_mangle] -pub extern "C" fn rs_ntp_parse_request(_flow: *const core::Flow, +pub unsafe extern "C" fn rs_ntp_parse_request(_flow: *const core::Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, @@ -206,7 +206,7 @@ pub extern "C" fn rs_ntp_parse_request(_flow: *const core::Flow, } #[no_mangle] -pub extern "C" fn rs_ntp_parse_response(_flow: *const core::Flow, +pub unsafe extern "C" fn rs_ntp_parse_response(_flow: *const core::Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, @@ -222,7 +222,7 @@ pub extern "C" fn rs_ntp_parse_response(_flow: *const core::Flow, } #[no_mangle] -pub extern "C" fn rs_ntp_state_get_tx(state: *mut std::os::raw::c_void, +pub unsafe extern "C" fn rs_ntp_state_get_tx(state: *mut std::os::raw::c_void, tx_id: u64) -> *mut std::os::raw::c_void { @@ -234,7 +234,7 @@ pub extern "C" fn rs_ntp_state_get_tx(state: *mut std::os::raw::c_void, } #[no_mangle] -pub extern "C" fn rs_ntp_state_get_tx_count(state: *mut std::os::raw::c_void) +pub unsafe extern "C" fn rs_ntp_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { let state = cast_pointer!(state,NTPState); @@ -242,7 +242,7 @@ pub extern "C" fn rs_ntp_state_get_tx_count(state: *mut std::os::raw::c_void) } #[no_mangle] -pub extern "C" fn rs_ntp_state_tx_free(state: *mut std::os::raw::c_void, +pub unsafe extern "C" fn rs_ntp_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { let state = cast_pointer!(state,NTPState); @@ -258,7 +258,7 @@ pub extern "C" fn rs_ntp_tx_get_alstate_progress(_tx: *mut std::os::raw::c_void, } #[no_mangle] -pub extern "C" fn rs_ntp_state_set_tx_detect_state( +pub unsafe extern "C" fn rs_ntp_state_set_tx_detect_state( tx: *mut std::os::raw::c_void, de_state: &mut core::DetectEngineState) -> std::os::raw::c_int { @@ -268,7 +268,7 @@ pub extern "C" fn rs_ntp_state_set_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_ntp_state_get_tx_detect_state( +pub unsafe extern "C" fn rs_ntp_state_get_tx_detect_state( tx: *mut std::os::raw::c_void) -> *mut core::DetectEngineState { @@ -280,7 +280,7 @@ pub extern "C" fn rs_ntp_state_get_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_ntp_state_get_event_info_by_id(event_id: std::os::raw::c_int, +pub unsafe extern "C" fn rs_ntp_state_get_event_info_by_id(event_id: std::os::raw::c_int, event_name: *mut *const std::os::raw::c_char, event_type: *mut core::AppLayerEventType) -> i8 @@ -292,10 +292,8 @@ pub extern "C" fn rs_ntp_state_get_event_info_by_id(event_id: std::os::raw::c_in NTPEvent::NotRequest => { "not_request\0" }, NTPEvent::NotResponse => { "not_response\0" }, }; - unsafe{ - *event_name = estr.as_ptr() as *const std::os::raw::c_char; - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - }; + *event_name = estr.as_ptr() as *const std::os::raw::c_char; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; 0 } else { -1 @@ -303,7 +301,7 @@ pub extern "C" fn rs_ntp_state_get_event_info_by_id(event_id: std::os::raw::c_in } #[no_mangle] -pub extern "C" fn rs_ntp_state_get_events(tx: *mut std::os::raw::c_void) +pub unsafe extern "C" fn rs_ntp_state_get_events(tx: *mut std::os::raw::c_void) -> *mut core::AppLayerDecoderEvents { let tx = cast_pointer!(tx, NTPTransaction); @@ -311,13 +309,13 @@ pub extern "C" fn rs_ntp_state_get_events(tx: *mut std::os::raw::c_void) } #[no_mangle] -pub extern "C" fn rs_ntp_state_get_event_info(event_name: *const std::os::raw::c_char, +pub unsafe extern "C" fn rs_ntp_state_get_event_info(event_name: *const std::os::raw::c_char, event_id: *mut std::os::raw::c_int, event_type: *mut core::AppLayerEventType) -> std::os::raw::c_int { if event_name == std::ptr::null() { return -1; } - let c_event_name: &CStr = unsafe { CStr::from_ptr(event_name) }; + let c_event_name: &CStr = CStr::from_ptr(event_name); let event = match c_event_name.to_str() { Ok(s) => { match s { @@ -327,10 +325,8 @@ pub extern "C" fn rs_ntp_state_get_event_info(event_name: *const std::os::raw::c }, Err(_) => -1, // UTF-8 conversion failed }; - unsafe{ - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - *event_id = event as std::os::raw::c_int; - }; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; + *event_id = event as std::os::raw::c_int; 0 } diff --git a/rust/src/rdp/rdp.rs b/rust/src/rdp/rdp.rs index 6f578189fec3..dbe10300240e 100644 --- a/rust/src/rdp/rdp.rs +++ b/rust/src/rdp/rdp.rs @@ -79,7 +79,7 @@ impl Drop for RdpTransaction { } #[no_mangle] -pub extern "C" fn rs_rdp_state_get_tx( +pub unsafe extern "C" fn rs_rdp_state_get_tx( state: *mut std::os::raw::c_void, tx_id: u64, ) -> *mut std::os::raw::c_void { let state = cast_pointer!(state, RdpState); @@ -94,7 +94,7 @@ pub extern "C" fn rs_rdp_state_get_tx( } #[no_mangle] -pub extern "C" fn rs_rdp_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { +pub unsafe extern "C" fn rs_rdp_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { let state = cast_pointer!(state, RdpState); return state.next_id; } @@ -384,7 +384,7 @@ pub extern "C" fn rs_rdp_state_free(state: *mut std::os::raw::c_void) { } #[no_mangle] -pub extern "C" fn rs_rdp_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { +pub unsafe extern "C" fn rs_rdp_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { let state = cast_pointer!(state, RdpState); state.free_tx(tx_id); } @@ -407,7 +407,7 @@ fn probe_rdp(input: &[u8]) -> bool { /// probe for T.123 message, whether to client or to server #[no_mangle] -pub extern "C" fn rs_rdp_probe_ts_tc( +pub unsafe extern "C" fn rs_rdp_probe_ts_tc( _flow: *const Flow, _direction: u8, input: *const u8, input_len: u32, _rdir: *mut u8, ) -> AppProto { if input != std::ptr::null_mut() { @@ -418,7 +418,7 @@ pub extern "C" fn rs_rdp_probe_ts_tc( // https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=view&target=rdp-ssl.pcap.gz // but this callback will not be exercised, so `probe_tls_handshake` not needed here. if probe_rdp(slice) { - return unsafe { ALPROTO_RDP }; + return ALPROTO_RDP; } } return ALPROTO_UNKNOWN; @@ -434,7 +434,7 @@ fn probe_tls_handshake(input: &[u8]) -> bool { // #[no_mangle] -pub extern "C" fn rs_rdp_parse_ts( +pub unsafe extern "C" fn rs_rdp_parse_ts( _flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, input_len: u32, _data: *const std::os::raw::c_void, _flags: u8, ) -> AppLayerResult { @@ -445,7 +445,7 @@ pub extern "C" fn rs_rdp_parse_ts( } #[no_mangle] -pub extern "C" fn rs_rdp_parse_tc( +pub unsafe extern "C" fn rs_rdp_parse_tc( _flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, input_len: u32, _data: *const std::os::raw::c_void, _flags: u8, ) -> AppLayerResult { diff --git a/rust/src/rfb/logger.rs b/rust/src/rfb/logger.rs index 7933659161da..70c44ae1b7e8 100644 --- a/rust/src/rfb/logger.rs +++ b/rust/src/rfb/logger.rs @@ -113,7 +113,7 @@ fn log_rfb(tx: &RFBTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> { } #[no_mangle] -pub extern "C" fn rs_rfb_logger_log(_state: &mut RFBState, +pub unsafe extern "C" fn rs_rfb_logger_log(_state: &mut RFBState, tx: *mut std::os::raw::c_void, js: &mut JsonBuilder) -> bool { let tx = cast_pointer!(tx, RFBTransaction); diff --git a/rust/src/rfb/rfb.rs b/rust/src/rfb/rfb.rs index 1b0c5019da53..0059c7bd18cb 100644 --- a/rust/src/rfb/rfb.rs +++ b/rust/src/rfb/rfb.rs @@ -530,7 +530,7 @@ pub extern "C" fn rs_rfb_state_free(state: *mut std::os::raw::c_void) { } #[no_mangle] -pub extern "C" fn rs_rfb_state_tx_free( +pub unsafe extern "C" fn rs_rfb_state_tx_free( state: *mut std::os::raw::c_void, tx_id: u64, ) { @@ -539,7 +539,7 @@ pub extern "C" fn rs_rfb_state_tx_free( } #[no_mangle] -pub extern "C" fn rs_rfb_parse_request( +pub unsafe extern "C" fn rs_rfb_parse_request( _flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, @@ -554,7 +554,7 @@ pub extern "C" fn rs_rfb_parse_request( } #[no_mangle] -pub extern "C" fn rs_rfb_parse_response( +pub unsafe extern "C" fn rs_rfb_parse_response( _flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, @@ -569,7 +569,7 @@ pub extern "C" fn rs_rfb_parse_response( } #[no_mangle] -pub extern "C" fn rs_rfb_state_get_tx( +pub unsafe extern "C" fn rs_rfb_state_get_tx( state: *mut std::os::raw::c_void, tx_id: u64, ) -> *mut std::os::raw::c_void { @@ -585,7 +585,7 @@ pub extern "C" fn rs_rfb_state_get_tx( } #[no_mangle] -pub extern "C" fn rs_rfb_state_get_tx_count( +pub unsafe extern "C" fn rs_rfb_state_get_tx_count( state: *mut std::os::raw::c_void, ) -> u64 { let state = cast_pointer!(state, RFBState); @@ -593,7 +593,7 @@ pub extern "C" fn rs_rfb_state_get_tx_count( } #[no_mangle] -pub extern "C" fn rs_rfb_tx_get_alstate_progress( +pub unsafe extern "C" fn rs_rfb_tx_get_alstate_progress( tx: *mut std::os::raw::c_void, _direction: u8, ) -> std::os::raw::c_int { @@ -605,7 +605,7 @@ pub extern "C" fn rs_rfb_tx_get_alstate_progress( } #[no_mangle] -pub extern "C" fn rs_rfb_state_get_events( +pub unsafe extern "C" fn rs_rfb_state_get_events( tx: *mut std::os::raw::c_void ) -> *mut core::AppLayerDecoderEvents { let tx = cast_pointer!(tx, RFBTransaction); @@ -629,7 +629,7 @@ pub extern "C" fn rs_rfb_state_get_event_info_by_id(_event_id: std::os::raw::c_i return -1; } #[no_mangle] -pub extern "C" fn rs_rfb_state_get_tx_iterator( +pub unsafe extern "C" fn rs_rfb_state_get_tx_iterator( _ipproto: u8, _alproto: AppProto, state: *mut std::os::raw::c_void, diff --git a/rust/src/sip/sip.rs b/rust/src/sip/sip.rs index 4cc8bf8e21b6..d725c195bf10 100755 --- a/rust/src/sip/sip.rs +++ b/rust/src/sip/sip.rs @@ -182,7 +182,7 @@ pub extern "C" fn rs_sip_state_free(state: *mut std::os::raw::c_void) { } #[no_mangle] -pub extern "C" fn rs_sip_state_get_tx( +pub unsafe extern "C" fn rs_sip_state_get_tx( state: *mut std::os::raw::c_void, tx_id: u64, ) -> *mut std::os::raw::c_void { @@ -194,13 +194,13 @@ pub extern "C" fn rs_sip_state_get_tx( } #[no_mangle] -pub extern "C" fn rs_sip_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { +pub unsafe extern "C" fn rs_sip_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { let state = cast_pointer!(state, SIPState); state.tx_id } #[no_mangle] -pub extern "C" fn rs_sip_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { +pub unsafe extern "C" fn rs_sip_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { let state = cast_pointer!(state, SIPState); state.free_tx(tx_id); } @@ -214,7 +214,7 @@ pub extern "C" fn rs_sip_tx_get_alstate_progress( } #[no_mangle] -pub extern "C" fn rs_sip_state_set_tx_detect_state( +pub unsafe extern "C" fn rs_sip_state_set_tx_detect_state( tx: *mut std::os::raw::c_void, de_state: &mut core::DetectEngineState, ) -> std::os::raw::c_int { @@ -224,7 +224,7 @@ pub extern "C" fn rs_sip_state_set_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_sip_state_get_tx_detect_state( +pub unsafe extern "C" fn rs_sip_state_get_tx_detect_state( tx: *mut std::os::raw::c_void, ) -> *mut core::DetectEngineState { let tx = cast_pointer!(tx, SIPTransaction); @@ -235,7 +235,7 @@ pub extern "C" fn rs_sip_state_get_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_sip_state_get_events( +pub unsafe extern "C" fn rs_sip_state_get_events( tx: *mut std::os::raw::c_void, ) -> *mut core::AppLayerDecoderEvents { let tx = cast_pointer!(tx, SIPTransaction); @@ -243,7 +243,7 @@ pub extern "C" fn rs_sip_state_get_events( } #[no_mangle] -pub extern "C" fn rs_sip_state_get_event_info( +pub unsafe extern "C" fn rs_sip_state_get_event_info( event_name: *const std::os::raw::c_char, event_id: *mut std::os::raw::c_int, event_type: *mut core::AppLayerEventType, @@ -251,7 +251,7 @@ pub extern "C" fn rs_sip_state_get_event_info( if event_name == std::ptr::null() { return -1; } - let c_event_name: &CStr = unsafe { CStr::from_ptr(event_name) }; + let c_event_name: &CStr = CStr::from_ptr(event_name); let event = match c_event_name.to_str() { Ok(s) => { match s { @@ -262,15 +262,13 @@ pub extern "C" fn rs_sip_state_get_event_info( } Err(_) => -1, // UTF-8 conversion failed }; - unsafe { - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - *event_id = event as std::os::raw::c_int; - }; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; + *event_id = event as std::os::raw::c_int; 0 } #[no_mangle] -pub extern "C" fn rs_sip_state_get_event_info_by_id( +pub unsafe extern "C" fn rs_sip_state_get_event_info_by_id( event_id: std::os::raw::c_int, event_name: *mut *const std::os::raw::c_char, event_type: *mut core::AppLayerEventType, @@ -280,10 +278,8 @@ pub extern "C" fn rs_sip_state_get_event_info_by_id( SIPEvent::IncompleteData => "incomplete_data\0", SIPEvent::InvalidData => "invalid_data\0", }; - unsafe { - *event_name = estr.as_ptr() as *const std::os::raw::c_char; - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - }; + *event_name = estr.as_ptr() as *const std::os::raw::c_char; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; 0 } else { -1 @@ -293,7 +289,7 @@ pub extern "C" fn rs_sip_state_get_event_info_by_id( static mut ALPROTO_SIP: AppProto = ALPROTO_UNKNOWN; #[no_mangle] -pub extern "C" fn rs_sip_probing_parser_ts( +pub unsafe extern "C" fn rs_sip_probing_parser_ts( _flow: *const Flow, _direction: u8, input: *const u8, @@ -302,13 +298,13 @@ pub extern "C" fn rs_sip_probing_parser_ts( ) -> AppProto { let buf = build_slice!(input, input_len as usize); if sip_parse_request(buf).is_ok() { - return unsafe { ALPROTO_SIP }; + return ALPROTO_SIP; } return ALPROTO_UNKNOWN; } #[no_mangle] -pub extern "C" fn rs_sip_probing_parser_tc( +pub unsafe extern "C" fn rs_sip_probing_parser_tc( _flow: *const Flow, _direction: u8, input: *const u8, @@ -317,13 +313,13 @@ pub extern "C" fn rs_sip_probing_parser_tc( ) -> AppProto { let buf = build_slice!(input, input_len as usize); if sip_parse_response(buf).is_ok() { - return unsafe { ALPROTO_SIP }; + return ALPROTO_SIP; } return ALPROTO_UNKNOWN; } #[no_mangle] -pub extern "C" fn rs_sip_parse_request( +pub unsafe extern "C" fn rs_sip_parse_request( _flow: *const core::Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, @@ -338,7 +334,7 @@ pub extern "C" fn rs_sip_parse_request( } #[no_mangle] -pub extern "C" fn rs_sip_parse_response( +pub unsafe extern "C" fn rs_sip_parse_response( _flow: *const core::Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, diff --git a/rust/src/smb/detect.rs b/rust/src/smb/detect.rs index 102b852a108a..cd6e16012985 100644 --- a/rust/src/smb/detect.rs +++ b/rust/src/smb/detect.rs @@ -21,7 +21,7 @@ use crate::smb::smb::*; use crate::dcerpc::detect::{DCEIfaceData, DCEOpnumData, DETECT_DCE_OPNUM_RANGE_UNINITIALIZED}; #[no_mangle] -pub extern "C" fn rs_smb_tx_get_share(tx: &mut SMBTransaction, +pub unsafe extern "C" fn rs_smb_tx_get_share(tx: &mut SMBTransaction, buffer: *mut *const u8, buffer_len: *mut u32) -> u8 @@ -30,26 +30,22 @@ pub extern "C" fn rs_smb_tx_get_share(tx: &mut SMBTransaction, Some(SMBTransactionTypeData::TREECONNECT(ref x)) => { SCLogDebug!("is_pipe {}", x.is_pipe); if !x.is_pipe { - unsafe { - *buffer = x.share_name.as_ptr(); - *buffer_len = x.share_name.len() as u32; - return 1; - } + *buffer = x.share_name.as_ptr(); + *buffer_len = x.share_name.len() as u32; + return 1; } } _ => { } } - unsafe { - *buffer = ptr::null(); - *buffer_len = 0; - } + *buffer = ptr::null(); + *buffer_len = 0; return 0; } #[no_mangle] -pub extern "C" fn rs_smb_tx_get_named_pipe(tx: &mut SMBTransaction, +pub unsafe extern "C" fn rs_smb_tx_get_named_pipe(tx: &mut SMBTransaction, buffer: *mut *const u8, buffer_len: *mut u32) -> u8 @@ -58,26 +54,22 @@ pub extern "C" fn rs_smb_tx_get_named_pipe(tx: &mut SMBTransaction, Some(SMBTransactionTypeData::TREECONNECT(ref x)) => { SCLogDebug!("is_pipe {}", x.is_pipe); if x.is_pipe { - unsafe { - *buffer = x.share_name.as_ptr(); - *buffer_len = x.share_name.len() as u32; - return 1; - } + *buffer = x.share_name.as_ptr(); + *buffer_len = x.share_name.len() as u32; + return 1; } } _ => { } } - unsafe { - *buffer = ptr::null(); - *buffer_len = 0; - } + *buffer = ptr::null(); + *buffer_len = 0; return 0; } #[no_mangle] -pub extern "C" fn rs_smb_tx_get_stub_data(tx: &mut SMBTransaction, +pub unsafe extern "C" fn rs_smb_tx_get_stub_data(tx: &mut SMBTransaction, direction: u8, buffer: *mut *const u8, buffer_len: *mut u32) @@ -91,21 +83,17 @@ pub extern "C" fn rs_smb_tx_get_stub_data(tx: &mut SMBTransaction, &x.stub_data_tc }; if vref.len() > 0 { - unsafe { - *buffer = vref.as_ptr(); - *buffer_len = vref.len() as u32; - return 1; - } + *buffer = vref.as_ptr(); + *buffer_len = vref.len() as u32; + return 1; } } _ => { } } - unsafe { - *buffer = ptr::null(); - *buffer_len = 0; - } + *buffer = ptr::null(); + *buffer_len = 0; return 0; } diff --git a/rust/src/smb/files.rs b/rust/src/smb/files.rs index 43295b1b9882..0b9339d191f1 100644 --- a/rust/src/smb/files.rs +++ b/rust/src/smb/files.rs @@ -190,16 +190,16 @@ impl SMBState { } #[no_mangle] -pub extern "C" fn rs_smb_getfiles(ptr: *mut std::ffi::c_void, direction: u8) -> * mut FileContainer { +pub unsafe extern "C" fn rs_smb_getfiles(ptr: *mut std::ffi::c_void, direction: u8) -> * mut FileContainer { if ptr.is_null() { panic!("NULL ptr"); }; let parser = cast_pointer!(ptr, SMBState); parser.getfiles(direction) } #[no_mangle] -pub extern "C" fn rs_smb_setfileflags(direction: u8, ptr: *mut SMBState, flags: u16) { +pub unsafe extern "C" fn rs_smb_setfileflags(direction: u8, ptr: *mut SMBState, flags: u16) { if ptr.is_null() { panic!("NULL ptr"); }; - let parser = unsafe { &mut *ptr }; + let parser = &mut *ptr; SCLogDebug!("direction {} flags {}", direction, flags); parser.setfileflags(direction, flags) } diff --git a/rust/src/smb/smb.rs b/rust/src/smb/smb.rs index f2f091b616ff..02ebda70d05f 100644 --- a/rust/src/smb/smb.rs +++ b/rust/src/smb/smb.rs @@ -1811,7 +1811,7 @@ pub extern "C" fn rs_smb_state_free(state: *mut std::os::raw::c_void) { /// C binding parse a SMB request. Returns 1 on success, -1 on failure. #[no_mangle] -pub extern "C" fn rs_smb_parse_request_tcp(flow: *const Flow, +pub unsafe extern "C" fn rs_smb_parse_request_tcp(flow: *const Flow, state: *mut ffi::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, @@ -1820,10 +1820,10 @@ pub extern "C" fn rs_smb_parse_request_tcp(flow: *const Flow, flags: u8) -> AppLayerResult { - let buf = unsafe{std::slice::from_raw_parts(input, input_len as usize)}; + let buf = std::slice::from_raw_parts(input, input_len as usize); let mut state = cast_pointer!(state, SMBState); let flow = cast_pointer!(flow, Flow); - let file_flags = unsafe { FileFlowToFlags(flow, STREAM_TOSERVER) }; + let file_flags = FileFlowToFlags(flow, STREAM_TOSERVER); rs_smb_setfileflags(STREAM_TOSERVER, state, file_flags|FILE_USE_DETECT); SCLogDebug!("parsing {} bytes of request data", input_len); @@ -1850,7 +1850,7 @@ pub extern "C" fn rs_smb_parse_request_tcp_gap( #[no_mangle] -pub extern "C" fn rs_smb_parse_response_tcp(flow: *const Flow, +pub unsafe extern "C" fn rs_smb_parse_response_tcp(flow: *const Flow, state: *mut ffi::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, @@ -1861,14 +1861,14 @@ pub extern "C" fn rs_smb_parse_response_tcp(flow: *const Flow, { let mut state = cast_pointer!(state, SMBState); let flow = cast_pointer!(flow, Flow); - let file_flags = unsafe { FileFlowToFlags(flow, STREAM_TOCLIENT) }; + let file_flags = FileFlowToFlags(flow, STREAM_TOCLIENT); rs_smb_setfileflags(STREAM_TOCLIENT, state, file_flags|FILE_USE_DETECT); if input.is_null() && input_len > 0 { return rs_smb_parse_response_tcp_gap(state, input_len); } SCLogDebug!("parsing {} bytes of response data", input_len); - let buf = unsafe{std::slice::from_raw_parts(input, input_len as usize)}; + let buf = std::slice::from_raw_parts(input, input_len as usize); /* START with MISTREAM set: record might be starting the middle. */ if flags & (STREAM_START|STREAM_MIDSTREAM) == (STREAM_START|STREAM_MIDSTREAM) { @@ -1954,7 +1954,7 @@ fn smb_probe_tcp_midstream(direction: u8, slice: &[u8], rdir: *mut u8) -> i8 // probing parser // return 1 if found, 0 is not found #[no_mangle] -pub extern "C" fn rs_smb_probe_tcp(_f: *const Flow, +pub unsafe extern "C" fn rs_smb_probe_tcp(_f: *const Flow, flags: u8, input: *const u8, len: u32, rdir: *mut u8) -> AppProto { @@ -1964,14 +1964,14 @@ pub extern "C" fn rs_smb_probe_tcp(_f: *const Flow, let slice = build_slice!(input, len as usize); if flags & STREAM_MIDSTREAM == STREAM_MIDSTREAM { if smb_probe_tcp_midstream(flags, slice, rdir) == 1 { - return unsafe { ALPROTO_SMB }; + return ALPROTO_SMB; } } match parse_nbss_record_partial(slice) { Ok((_, ref hdr)) => { if hdr.is_smb() { SCLogDebug!("smb found"); - return unsafe { ALPROTO_SMB }; + return ALPROTO_SMB; } else if hdr.needs_more(){ return 0; } else if hdr.is_valid() && @@ -1984,7 +1984,7 @@ pub extern "C" fn rs_smb_probe_tcp(_f: *const Flow, Ok((_, ref hdr2)) => { if hdr2.is_smb() { SCLogDebug!("smb found"); - return unsafe { ALPROTO_SMB }; + return ALPROTO_SMB; } } _ => {} @@ -1999,11 +1999,11 @@ pub extern "C" fn rs_smb_probe_tcp(_f: *const Flow, _ => { }, } SCLogDebug!("no smb"); - return unsafe { ALPROTO_FAILED }; + return ALPROTO_FAILED; } #[no_mangle] -pub extern "C" fn rs_smb_state_get_tx_count(state: *mut ffi::c_void) +pub unsafe extern "C" fn rs_smb_state_get_tx_count(state: *mut ffi::c_void) -> u64 { let state = cast_pointer!(state, SMBState); @@ -2012,7 +2012,7 @@ pub extern "C" fn rs_smb_state_get_tx_count(state: *mut ffi::c_void) } #[no_mangle] -pub extern "C" fn rs_smb_state_get_tx(state: *mut ffi::c_void, +pub unsafe extern "C" fn rs_smb_state_get_tx(state: *mut ffi::c_void, tx_id: u64) -> *mut ffi::c_void { @@ -2029,7 +2029,7 @@ pub extern "C" fn rs_smb_state_get_tx(state: *mut ffi::c_void, // for use with the C API call StateGetTxIterator #[no_mangle] -pub extern "C" fn rs_smb_state_get_tx_iterator( +pub unsafe extern "C" fn rs_smb_state_get_tx_iterator( _ipproto: u8, _alproto: AppProto, state: *mut std::os::raw::c_void, @@ -2052,7 +2052,7 @@ pub extern "C" fn rs_smb_state_get_tx_iterator( } #[no_mangle] -pub extern "C" fn rs_smb_state_tx_free(state: *mut ffi::c_void, +pub unsafe extern "C" fn rs_smb_state_tx_free(state: *mut ffi::c_void, tx_id: u64) { let state = cast_pointer!(state, SMBState); @@ -2061,7 +2061,7 @@ pub extern "C" fn rs_smb_state_tx_free(state: *mut ffi::c_void, } #[no_mangle] -pub extern "C" fn rs_smb_tx_get_alstate_progress(tx: *mut ffi::c_void, +pub unsafe extern "C" fn rs_smb_tx_get_alstate_progress(tx: *mut ffi::c_void, direction: u8) -> i32 { @@ -2081,7 +2081,7 @@ pub extern "C" fn rs_smb_tx_get_alstate_progress(tx: *mut ffi::c_void, #[no_mangle] -pub extern "C" fn rs_smb_get_tx_data( +pub unsafe extern "C" fn rs_smb_get_tx_data( tx: *mut std::os::raw::c_void) -> *mut AppLayerTxData { @@ -2090,7 +2090,7 @@ pub extern "C" fn rs_smb_get_tx_data( } #[no_mangle] -pub extern "C" fn rs_smb_state_get_tx_detect_state( +pub unsafe extern "C" fn rs_smb_state_get_tx_detect_state( tx: *mut std::os::raw::c_void) -> *mut DetectEngineState { @@ -2106,7 +2106,7 @@ pub extern "C" fn rs_smb_state_get_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_smb_state_set_tx_detect_state( +pub unsafe extern "C" fn rs_smb_state_set_tx_detect_state( tx: *mut std::os::raw::c_void, de_state: &mut DetectEngineState) -> std::os::raw::c_int { @@ -2116,7 +2116,7 @@ pub extern "C" fn rs_smb_state_set_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_smb_state_truncate( +pub unsafe extern "C" fn rs_smb_state_truncate( state: *mut std::ffi::c_void, direction: u8) { @@ -2129,7 +2129,7 @@ pub extern "C" fn rs_smb_state_truncate( } #[no_mangle] -pub extern "C" fn rs_smb_state_get_events(tx: *mut std::os::raw::c_void) +pub unsafe extern "C" fn rs_smb_state_get_events(tx: *mut std::os::raw::c_void) -> *mut AppLayerDecoderEvents { let tx = cast_pointer!(tx, SMBTransaction); @@ -2137,7 +2137,7 @@ pub extern "C" fn rs_smb_state_get_events(tx: *mut std::os::raw::c_void) } #[no_mangle] -pub extern "C" fn rs_smb_state_get_event_info_by_id(event_id: std::os::raw::c_int, +pub unsafe extern "C" fn rs_smb_state_get_event_info_by_id(event_id: std::os::raw::c_int, event_name: *mut *const std::os::raw::c_char, event_type: *mut AppLayerEventType) -> i8 @@ -2153,10 +2153,8 @@ pub extern "C" fn rs_smb_state_get_event_info_by_id(event_id: std::os::raw::c_in SMBEvent::NegotiateMalformedDialects => { "netogiate_malformed_dialects\0" }, SMBEvent::FileOverlap => { "file_overlap\0" }, }; - unsafe{ - *event_name = estr.as_ptr() as *const std::os::raw::c_char; - *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION; - }; + *event_name = estr.as_ptr() as *const std::os::raw::c_char; + *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION; 0 } else { -1 @@ -2164,7 +2162,7 @@ pub extern "C" fn rs_smb_state_get_event_info_by_id(event_id: std::os::raw::c_in } #[no_mangle] -pub extern "C" fn rs_smb_state_get_event_info(event_name: *const std::os::raw::c_char, +pub unsafe extern "C" fn rs_smb_state_get_event_info(event_name: *const std::os::raw::c_char, event_id: *mut std::os::raw::c_int, event_type: *mut AppLayerEventType) -> i32 @@ -2172,27 +2170,25 @@ pub extern "C" fn rs_smb_state_get_event_info(event_name: *const std::os::raw::c if event_name == std::ptr::null() { return -1; } - let c_event_name: &CStr = unsafe { CStr::from_ptr(event_name) }; + let c_event_name: &CStr = CStr::from_ptr(event_name); let event = match c_event_name.to_str() { Ok(s) => { smb_str_to_event(s) }, Err(_) => -1, // UTF-8 conversion failed }; - unsafe { - *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION; - *event_id = event as std::os::raw::c_int; - }; + *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION; + *event_id = event as std::os::raw::c_int; if event == -1 { return -1; } 0 } -pub extern "C" fn smb3_probe_tcp(f: *const Flow, dir: u8, input: *const u8, len: u32, rdir: *mut u8) -> u16 { +pub unsafe extern "C" fn smb3_probe_tcp(f: *const Flow, dir: u8, input: *const u8, len: u32, rdir: *mut u8) -> u16 { let retval = rs_smb_probe_tcp(f, dir, input, len, rdir); let f = cast_pointer!(f, Flow); - if unsafe { retval != ALPROTO_SMB } { + if retval != ALPROTO_SMB { return retval; } let (sp, dp) = f.get_ports(); @@ -2200,15 +2196,13 @@ pub extern "C" fn smb3_probe_tcp(f: *const Flow, dir: u8, input: *const u8, len: let fsp = if (flags & FLOW_DIR_REVERSED) != 0 { dp } else { sp }; let fdp = if (flags & FLOW_DIR_REVERSED) != 0 { sp } else { dp }; if fsp == 445 && fdp != 445 { - unsafe { - if dir & STREAM_TOSERVER != 0 { - *rdir = STREAM_TOCLIENT; - } else { - *rdir = STREAM_TOSERVER; - } + if dir & STREAM_TOSERVER != 0 { + *rdir = STREAM_TOCLIENT; + } else { + *rdir = STREAM_TOSERVER; } } - return unsafe { ALPROTO_SMB }; + return ALPROTO_SMB; } fn register_pattern_probe() -> i8 { diff --git a/rust/src/snmp/detect.rs b/rust/src/snmp/detect.rs index 88da4ff10107..f6ceedf05197 100644 --- a/rust/src/snmp/detect.rs +++ b/rust/src/snmp/detect.rs @@ -20,43 +20,37 @@ use crate::snmp::snmp::SNMPTransaction; #[no_mangle] -pub extern "C" fn rs_snmp_tx_get_version(tx: &mut SNMPTransaction, +pub unsafe extern "C" fn rs_snmp_tx_get_version(tx: &mut SNMPTransaction, version: *mut u32) { debug_assert!(tx.version != 0, "SNMP version is 0"); - unsafe { - *version = tx.version as u32; - } + *version = tx.version as u32; } #[no_mangle] -pub extern "C" fn rs_snmp_tx_get_community(tx: &mut SNMPTransaction, +pub unsafe extern "C" fn rs_snmp_tx_get_community(tx: &mut SNMPTransaction, buf: *mut *const u8, len: *mut u32) { match tx.community { Some(ref c) => { - unsafe { - *buf = (&c).as_ptr(); - *len = c.len() as u32; - } + *buf = (&c).as_ptr(); + *len = c.len() as u32; }, None => () } } #[no_mangle] -pub extern "C" fn rs_snmp_tx_get_pdu_type(tx: &mut SNMPTransaction, +pub unsafe extern "C" fn rs_snmp_tx_get_pdu_type(tx: &mut SNMPTransaction, pdu_type: *mut u32) { - unsafe { - match tx.info { - Some(ref info) => { - *pdu_type = info.pdu_type.0 as u32; - }, - None => { - *pdu_type = 0xffffffff; - } + match tx.info { + Some(ref info) => { + *pdu_type = info.pdu_type.0 as u32; + }, + None => { + *pdu_type = 0xffffffff; } } } diff --git a/rust/src/snmp/snmp.rs b/rust/src/snmp/snmp.rs index 3ad187593f05..608c83356ae9 100644 --- a/rust/src/snmp/snmp.rs +++ b/rust/src/snmp/snmp.rs @@ -311,7 +311,7 @@ pub extern "C" fn rs_snmp_state_free(state: *mut std::os::raw::c_void) { } #[no_mangle] -pub extern "C" fn rs_snmp_parse_request(_flow: *const core::Flow, +pub unsafe extern "C" fn rs_snmp_parse_request(_flow: *const core::Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, @@ -324,7 +324,7 @@ pub extern "C" fn rs_snmp_parse_request(_flow: *const core::Flow, } #[no_mangle] -pub extern "C" fn rs_snmp_parse_response(_flow: *const core::Flow, +pub unsafe extern "C" fn rs_snmp_parse_response(_flow: *const core::Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, input: *const u8, @@ -337,7 +337,7 @@ pub extern "C" fn rs_snmp_parse_response(_flow: *const core::Flow, } #[no_mangle] -pub extern "C" fn rs_snmp_state_get_tx(state: *mut std::os::raw::c_void, +pub unsafe extern "C" fn rs_snmp_state_get_tx(state: *mut std::os::raw::c_void, tx_id: u64) -> *mut std::os::raw::c_void { @@ -349,7 +349,7 @@ pub extern "C" fn rs_snmp_state_get_tx(state: *mut std::os::raw::c_void, } #[no_mangle] -pub extern "C" fn rs_snmp_state_get_tx_count(state: *mut std::os::raw::c_void) +pub unsafe extern "C" fn rs_snmp_state_get_tx_count(state: *mut std::os::raw::c_void) -> u64 { let state = cast_pointer!(state,SNMPState); @@ -357,7 +357,7 @@ pub extern "C" fn rs_snmp_state_get_tx_count(state: *mut std::os::raw::c_void) } #[no_mangle] -pub extern "C" fn rs_snmp_state_tx_free(state: *mut std::os::raw::c_void, +pub unsafe extern "C" fn rs_snmp_state_tx_free(state: *mut std::os::raw::c_void, tx_id: u64) { let state = cast_pointer!(state,SNMPState); @@ -373,7 +373,7 @@ pub extern "C" fn rs_snmp_tx_get_alstate_progress(_tx: *mut std::os::raw::c_void } #[no_mangle] -pub extern "C" fn rs_snmp_state_set_tx_detect_state( +pub unsafe extern "C" fn rs_snmp_state_set_tx_detect_state( tx: *mut std::os::raw::c_void, de_state: &mut core::DetectEngineState) -> std::os::raw::c_int { @@ -383,7 +383,7 @@ pub extern "C" fn rs_snmp_state_set_tx_detect_state( } #[no_mangle] -pub extern "C" fn rs_snmp_state_get_tx_detect_state( +pub unsafe extern "C" fn rs_snmp_state_get_tx_detect_state( tx: *mut std::os::raw::c_void) -> *mut core::DetectEngineState { @@ -396,7 +396,7 @@ pub extern "C" fn rs_snmp_state_get_tx_detect_state( #[no_mangle] -pub extern "C" fn rs_snmp_state_get_events(tx: *mut std::os::raw::c_void) +pub unsafe extern "C" fn rs_snmp_state_get_events(tx: *mut std::os::raw::c_void) -> *mut core::AppLayerDecoderEvents { let tx = cast_pointer!(tx, SNMPTransaction); @@ -404,7 +404,7 @@ pub extern "C" fn rs_snmp_state_get_events(tx: *mut std::os::raw::c_void) } #[no_mangle] -pub extern "C" fn rs_snmp_state_get_event_info_by_id(event_id: std::os::raw::c_int, +pub unsafe extern "C" fn rs_snmp_state_get_event_info_by_id(event_id: std::os::raw::c_int, event_name: *mut *const std::os::raw::c_char, event_type: *mut core::AppLayerEventType) -> i8 @@ -415,10 +415,8 @@ pub extern "C" fn rs_snmp_state_get_event_info_by_id(event_id: std::os::raw::c_i SNMPEvent::UnknownSecurityModel => { "unknown_security_model\0" }, SNMPEvent::VersionMismatch => { "version_mismatch\0" }, }; - unsafe{ - *event_name = estr.as_ptr() as *const std::os::raw::c_char; - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - }; + *event_name = estr.as_ptr() as *const std::os::raw::c_char; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; 0 } else { -1 @@ -426,13 +424,13 @@ pub extern "C" fn rs_snmp_state_get_event_info_by_id(event_id: std::os::raw::c_i } #[no_mangle] -pub extern "C" fn rs_snmp_state_get_event_info(event_name: *const std::os::raw::c_char, +pub unsafe extern "C" fn rs_snmp_state_get_event_info(event_name: *const std::os::raw::c_char, event_id: *mut std::os::raw::c_int, event_type: *mut core::AppLayerEventType) -> std::os::raw::c_int { if event_name == std::ptr::null() { return -1; } - let c_event_name: &CStr = unsafe { CStr::from_ptr(event_name) }; + let c_event_name: &CStr = CStr::from_ptr(event_name); let event = match c_event_name.to_str() { Ok(s) => { match s { @@ -444,10 +442,8 @@ pub extern "C" fn rs_snmp_state_get_event_info(event_name: *const std::os::raw:: }, Err(_) => -1, // UTF-8 conversion failed }; - unsafe{ - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - *event_id = event as std::os::raw::c_int; - }; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; + *event_id = event as std::os::raw::c_int; 0 } @@ -473,7 +469,7 @@ pub extern "C" fn rs_snmp_state_get_tx_iterator( // for use with the C API call StateGetTxIterator #[no_mangle] -pub extern "C" fn rs_snmp_get_tx_iterator(_ipproto: u8, +pub unsafe extern "C" fn rs_snmp_get_tx_iterator(_ipproto: u8, _alproto: AppProto, alstate: *mut std::os::raw::c_void, min_tx_id: u64, @@ -524,18 +520,18 @@ fn parse_pdu_enveloppe_version(i:&[u8]) -> IResult<&[u8],u32> { } #[no_mangle] -pub extern "C" fn rs_snmp_probing_parser(_flow: *const Flow, +pub unsafe extern "C" fn rs_snmp_probing_parser(_flow: *const Flow, _direction: u8, input:*const u8, input_len: u32, _rdir: *mut u8) -> AppProto { let slice = build_slice!(input,input_len as usize); - let alproto = unsafe{ ALPROTO_SNMP }; - if slice.len() < 4 { return unsafe{ALPROTO_FAILED}; } + let alproto = ALPROTO_SNMP; + if slice.len() < 4 { return ALPROTO_FAILED; } match parse_pdu_enveloppe_version(slice) { Ok((_,_)) => alproto, Err(nom::Err::Incomplete(_)) => ALPROTO_UNKNOWN, - _ => unsafe{ALPROTO_FAILED}, + _ => ALPROTO_FAILED, } } diff --git a/rust/src/ssh/detect.rs b/rust/src/ssh/detect.rs index ed5341625bad..c7ea3c03054c 100644 --- a/rust/src/ssh/detect.rs +++ b/rust/src/ssh/detect.rs @@ -20,7 +20,7 @@ use crate::core::{STREAM_TOCLIENT, STREAM_TOSERVER}; use std::ptr; #[no_mangle] -pub extern "C" fn rs_ssh_tx_get_protocol( +pub unsafe extern "C" fn rs_ssh_tx_get_protocol( tx: *mut std::os::raw::c_void, buffer: *mut *const u8, buffer_len: *mut u32, direction: u8, ) -> u8 { let tx = cast_pointer!(tx, SSHTransaction); @@ -28,35 +28,29 @@ pub extern "C" fn rs_ssh_tx_get_protocol( STREAM_TOSERVER => { let m = &tx.cli_hdr.protover; if m.len() > 0 { - unsafe { - *buffer = m.as_ptr(); - *buffer_len = m.len() as u32; - } + *buffer = m.as_ptr(); + *buffer_len = m.len() as u32; return 1; } } STREAM_TOCLIENT => { let m = &tx.srv_hdr.protover; if m.len() > 0 { - unsafe { - *buffer = m.as_ptr(); - *buffer_len = m.len() as u32; - } + *buffer = m.as_ptr(); + *buffer_len = m.len() as u32; return 1; } } _ => {} } - unsafe { - *buffer = ptr::null(); - *buffer_len = 0; - } + *buffer = ptr::null(); + *buffer_len = 0; return 0; } #[no_mangle] -pub extern "C" fn rs_ssh_tx_get_software( +pub unsafe extern "C" fn rs_ssh_tx_get_software( tx: *mut std::os::raw::c_void, buffer: *mut *const u8, buffer_len: *mut u32, direction: u8, ) -> u8 { let tx = cast_pointer!(tx, SSHTransaction); @@ -64,35 +58,29 @@ pub extern "C" fn rs_ssh_tx_get_software( STREAM_TOSERVER => { let m = &tx.cli_hdr.swver; if m.len() > 0 { - unsafe { - *buffer = m.as_ptr(); - *buffer_len = m.len() as u32; - } + *buffer = m.as_ptr(); + *buffer_len = m.len() as u32; return 1; } } STREAM_TOCLIENT => { let m = &tx.srv_hdr.swver; if m.len() > 0 { - unsafe { - *buffer = m.as_ptr(); - *buffer_len = m.len() as u32; - } + *buffer = m.as_ptr(); + *buffer_len = m.len() as u32; return 1; } } _ => {} } - unsafe { - *buffer = ptr::null(); - *buffer_len = 0; - } + *buffer = ptr::null(); + *buffer_len = 0; return 0; } #[no_mangle] -pub extern "C" fn rs_ssh_tx_get_hassh( +pub unsafe extern "C" fn rs_ssh_tx_get_hassh( tx: *mut std::os::raw::c_void, buffer: *mut *const u8, buffer_len: *mut u32, @@ -103,35 +91,29 @@ pub extern "C" fn rs_ssh_tx_get_hassh( STREAM_TOSERVER => { let m = &tx.cli_hdr.hassh; if m.len() > 0 { - unsafe { - *buffer = m.as_ptr(); - *buffer_len = m.len() as u32; - } + *buffer = m.as_ptr(); + *buffer_len = m.len() as u32; return 1; } } STREAM_TOCLIENT => { let m = &tx.srv_hdr.hassh; if m.len() > 0 { - unsafe { - *buffer = m.as_ptr(); - *buffer_len = m.len() as u32; - } + *buffer = m.as_ptr(); + *buffer_len = m.len() as u32; return 1; } } _ => {} } - unsafe { - *buffer = ptr::null(); - *buffer_len = 0; - } + *buffer = ptr::null(); + *buffer_len = 0; return 0; } #[no_mangle] -pub extern "C" fn rs_ssh_tx_get_hassh_string( +pub unsafe extern "C" fn rs_ssh_tx_get_hassh_string( tx: *mut std::os::raw::c_void, buffer: *mut *const u8, buffer_len: *mut u32, @@ -142,29 +124,23 @@ pub extern "C" fn rs_ssh_tx_get_hassh_string( STREAM_TOSERVER => { let m = &tx.cli_hdr.hassh_string; if m.len() > 0 { - unsafe { - *buffer = m.as_ptr(); - *buffer_len = m.len() as u32; - } + *buffer = m.as_ptr(); + *buffer_len = m.len() as u32; return 1; } } STREAM_TOCLIENT => { let m = &tx.srv_hdr.hassh_string; if m.len() > 0 { - unsafe { - *buffer = m.as_ptr(); - *buffer_len = m.len() as u32; - } + *buffer = m.as_ptr(); + *buffer_len = m.len() as u32; return 1; } } _ => {} } - unsafe { - *buffer = ptr::null(); - *buffer_len = 0; - } + *buffer = ptr::null(); + *buffer_len = 0; return 0; } diff --git a/rust/src/ssh/logger.rs b/rust/src/ssh/logger.rs index 7f5965146373..30823b61b61f 100644 --- a/rust/src/ssh/logger.rs +++ b/rust/src/ssh/logger.rs @@ -62,7 +62,7 @@ fn log_ssh(tx: &SSHTransaction, js: &mut JsonBuilder) -> Result } #[no_mangle] -pub extern "C" fn rs_ssh_log_json(tx: *mut std::os::raw::c_void, js: &mut JsonBuilder) -> bool { +pub unsafe extern "C" fn rs_ssh_log_json(tx: *mut std::os::raw::c_void, js: &mut JsonBuilder) -> bool { let tx = cast_pointer!(tx, SSHTransaction); if let Ok(x) = log_ssh(tx, js) { return x; diff --git a/rust/src/ssh/ssh.rs b/rust/src/ssh/ssh.rs index d79f9860f61f..0ebad5cb10c2 100644 --- a/rust/src/ssh/ssh.rs +++ b/rust/src/ssh/ssh.rs @@ -369,7 +369,7 @@ export_tx_set_detect_state!(rs_ssh_tx_set_detect_state, SSHTransaction); export_tx_data_get!(rs_ssh_get_tx_data, SSHTransaction); #[no_mangle] -pub extern "C" fn rs_ssh_state_get_events( +pub unsafe extern "C" fn rs_ssh_state_get_events( tx: *mut std::os::raw::c_void, ) -> *mut core::AppLayerDecoderEvents { let tx = cast_pointer!(tx, SSHTransaction); @@ -377,14 +377,14 @@ pub extern "C" fn rs_ssh_state_get_events( } #[no_mangle] -pub extern "C" fn rs_ssh_state_get_event_info( +pub unsafe extern "C" fn rs_ssh_state_get_event_info( event_name: *const std::os::raw::c_char, event_id: *mut std::os::raw::c_int, event_type: *mut core::AppLayerEventType, ) -> std::os::raw::c_int { if event_name == std::ptr::null() { return -1; } - let c_event_name: &CStr = unsafe { CStr::from_ptr(event_name) }; + let c_event_name: &CStr = CStr::from_ptr(event_name); let event = match c_event_name.to_str() { Ok(s) => { match s { @@ -397,15 +397,13 @@ pub extern "C" fn rs_ssh_state_get_event_info( } Err(_) => -1, // UTF-8 conversion failed }; - unsafe { - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - *event_id = event as std::os::raw::c_int; - }; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; + *event_id = event as std::os::raw::c_int; 0 } #[no_mangle] -pub extern "C" fn rs_ssh_state_get_event_info_by_id( +pub unsafe extern "C" fn rs_ssh_state_get_event_info_by_id( event_id: std::os::raw::c_int, event_name: *mut *const std::os::raw::c_char, event_type: *mut core::AppLayerEventType, ) -> i8 { @@ -416,10 +414,8 @@ pub extern "C" fn rs_ssh_state_get_event_info_by_id( SSHEvent::InvalidRecord => "invalid_record\0", SSHEvent::LongKexRecord => "long_kex_record\0", }; - unsafe { - *event_name = estr.as_ptr() as *const std::os::raw::c_char; - *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; - }; + *event_name = estr.as_ptr() as *const std::os::raw::c_char; + *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION; 0 } else { -1 @@ -434,8 +430,8 @@ pub extern "C" fn rs_ssh_state_new(_orig_state: *mut std::os::raw::c_void, _orig } #[no_mangle] -pub extern "C" fn rs_ssh_state_free(state: *mut std::os::raw::c_void) { - std::mem::drop(unsafe { Box::from_raw(state as *mut SSHState) }); +pub unsafe extern "C" fn rs_ssh_state_free(state: *mut std::os::raw::c_void) { + std::mem::drop(Box::from_raw(state as *mut SSHState)); } #[no_mangle] @@ -444,7 +440,7 @@ pub extern "C" fn rs_ssh_state_tx_free(_state: *mut std::os::raw::c_void, _tx_id } #[no_mangle] -pub extern "C" fn rs_ssh_parse_request( +pub unsafe extern "C" fn rs_ssh_parse_request( _flow: *const Flow, state: *mut std::os::raw::c_void, pstate: *mut std::os::raw::c_void, input: *const u8, input_len: u32, _data: *const std::os::raw::c_void, _flags: u8, ) -> AppLayerResult { @@ -459,7 +455,7 @@ pub extern "C" fn rs_ssh_parse_request( } #[no_mangle] -pub extern "C" fn rs_ssh_parse_response( +pub unsafe extern "C" fn rs_ssh_parse_response( _flow: *const Flow, state: *mut std::os::raw::c_void, pstate: *mut std::os::raw::c_void, input: *const u8, input_len: u32, _data: *const std::os::raw::c_void, _flags: u8, ) -> AppLayerResult { @@ -474,7 +470,7 @@ pub extern "C" fn rs_ssh_parse_response( } #[no_mangle] -pub extern "C" fn rs_ssh_state_get_tx( +pub unsafe extern "C" fn rs_ssh_state_get_tx( state: *mut std::os::raw::c_void, _tx_id: u64, ) -> *mut std::os::raw::c_void { let state = cast_pointer!(state, SSHState); @@ -487,7 +483,7 @@ pub extern "C" fn rs_ssh_state_get_tx_count(_state: *mut std::os::raw::c_void) - } #[no_mangle] -pub extern "C" fn rs_ssh_tx_get_flags( +pub unsafe extern "C" fn rs_ssh_tx_get_flags( tx: *mut std::os::raw::c_void, direction: u8, ) -> SSHConnectionState { let tx = cast_pointer!(tx, SSHTransaction); @@ -499,7 +495,7 @@ pub extern "C" fn rs_ssh_tx_get_flags( } #[no_mangle] -pub extern "C" fn rs_ssh_tx_get_alstate_progress( +pub unsafe extern "C" fn rs_ssh_tx_get_alstate_progress( tx: *mut std::os::raw::c_void, direction: u8, ) -> std::os::raw::c_int { let tx = cast_pointer!(tx, SSHTransaction); @@ -588,7 +584,7 @@ pub extern "C" fn rs_ssh_hassh_is_enabled() -> bool { } #[no_mangle] -pub extern "C" fn rs_ssh_tx_get_log_condition( tx: *mut std::os::raw::c_void) -> bool { +pub unsafe extern "C" fn rs_ssh_tx_get_log_condition( tx: *mut std::os::raw::c_void) -> bool { let tx = cast_pointer!(tx, SSHTransaction); if rs_ssh_hassh_is_enabled() { diff --git a/rust/src/tftp/tftp.rs b/rust/src/tftp/tftp.rs index 977f95c7b4bb..8b62f2eae62d 100644 --- a/rust/src/tftp/tftp.rs +++ b/rust/src/tftp/tftp.rs @@ -153,10 +153,10 @@ fn parse_tftp_request(input: &[u8]) -> Option { } #[no_mangle] -pub extern "C" fn rs_tftp_request(state: &mut TFTPState, +pub unsafe extern "C" fn rs_tftp_request(state: &mut TFTPState, input: *const u8, len: u32) -> i64 { - let buf = unsafe{std::slice::from_raw_parts(input, len as usize)}; + let buf = std::slice::from_raw_parts(input, len as usize); match parse_tftp_request(buf) { Some(mut tx) => { state.tx_id += 1; @@ -171,7 +171,7 @@ pub extern "C" fn rs_tftp_request(state: &mut TFTPState, } #[no_mangle] -pub extern "C" fn rs_tftp_get_tx_data( +pub unsafe extern "C" fn rs_tftp_get_tx_data( tx: *mut std::os::raw::c_void) -> *mut AppLayerTxData { diff --git a/rust/src/x509/mod.rs b/rust/src/x509/mod.rs index 353edb1d4411..428e66793511 100644 --- a/rust/src/x509/mod.rs +++ b/rust/src/x509/mod.rs @@ -66,7 +66,7 @@ pub unsafe extern "C" fn rs_x509_decode( } #[no_mangle] -pub extern "C" fn rs_x509_get_subject(ptr: *const X509) -> *mut c_char { +pub unsafe extern "C" fn rs_x509_get_subject(ptr: *const X509) -> *mut c_char { if ptr.is_null() { return std::ptr::null_mut(); } @@ -76,7 +76,7 @@ pub extern "C" fn rs_x509_get_subject(ptr: *const X509) -> *mut c_char { } #[no_mangle] -pub extern "C" fn rs_x509_get_issuer(ptr: *const X509) -> *mut c_char { +pub unsafe extern "C" fn rs_x509_get_issuer(ptr: *const X509) -> *mut c_char { if ptr.is_null() { return std::ptr::null_mut(); } @@ -86,7 +86,7 @@ pub extern "C" fn rs_x509_get_issuer(ptr: *const X509) -> *mut c_char { } #[no_mangle] -pub extern "C" fn rs_x509_get_serial(ptr: *const X509) -> *mut c_char { +pub unsafe extern "C" fn rs_x509_get_serial(ptr: *const X509) -> *mut c_char { if ptr.is_null() { return std::ptr::null_mut(); } From 69cf5c9eea83a2c0f0412fc02034a4b21ee70da4 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Fri, 20 Aug 2021 10:22:30 -0600 Subject: [PATCH 12/24] rust(lint): remove needless borrows These are needless borrows (references) as the item is already a reference. --- rust/src/asn1/parse_rules.rs | 2 +- rust/src/dcerpc/dcerpc.rs | 62 +++++++++++++++++----------------- rust/src/dcerpc/dcerpc_udp.rs | 4 +-- rust/src/dcerpc/detect.rs | 4 +-- rust/src/dns/detect.rs | 2 +- rust/src/dns/dns.rs | 4 +-- rust/src/dns/log.rs | 28 +++++++-------- rust/src/ffi/hashing.rs | 6 ++-- rust/src/filecontainer.rs | 14 ++++---- rust/src/http2/detect.rs | 12 +++---- rust/src/ike/ike.rs | 2 +- rust/src/ike/ikev2.rs | 2 +- rust/src/ike/logger.rs | 6 ++-- rust/src/krb/krb5.rs | 2 +- rust/src/modbus/detect.rs | 6 ++-- rust/src/modbus/log.rs | 14 ++++---- rust/src/modbus/modbus.rs | 48 +++++++++++++------------- rust/src/mqtt/mqtt_property.rs | 20 +++++------ rust/src/mqtt/parser.rs | 6 ++-- rust/src/nfs/nfs.rs | 4 +-- rust/src/nfs/nfs2.rs | 4 +-- rust/src/nfs/nfs3.rs | 4 +-- rust/src/nfs/nfs4.rs | 10 +++--- rust/src/ntp/ntp.rs | 2 +- rust/src/rdp/log.rs | 2 +- rust/src/rdp/rdp.rs | 28 +++++++-------- rust/src/sip/log.rs | 4 +-- rust/src/sip/sip.rs | 2 +- rust/src/smb/auth.rs | 2 +- rust/src/smb/dcerpc.rs | 8 ++--- rust/src/smb/files.rs | 2 +- rust/src/smb/log.rs | 16 ++++----- rust/src/smb/nbss_records.rs | 6 ++-- rust/src/smb/smb.rs | 34 +++++++++---------- rust/src/smb/smb1.rs | 14 ++++---- rust/src/smb/smb1_records.rs | 2 +- rust/src/smb/smb2.rs | 6 ++-- rust/src/smb/smb2_ioctl.rs | 4 +-- rust/src/snmp/detect.rs | 2 +- rust/src/snmp/snmp.rs | 2 +- rust/src/tftp/tftp.rs | 2 +- 41 files changed, 202 insertions(+), 202 deletions(-) diff --git a/rust/src/asn1/parse_rules.rs b/rust/src/asn1/parse_rules.rs index 49547b9f45ff..aa079edd88c9 100644 --- a/rust/src/asn1/parse_rules.rs +++ b/rust/src/asn1/parse_rules.rs @@ -45,7 +45,7 @@ pub unsafe extern "C" fn rs_detect_asn1_parse(input: *const c_char) -> *mut Dete } }; - match asn1_parse_rule(&arg) { + match asn1_parse_rule(arg) { Ok((_rest, data)) => { let mut data = data; diff --git a/rust/src/dcerpc/dcerpc.rs b/rust/src/dcerpc/dcerpc.rs index 97061bd920bd..3f00e8312b9b 100644 --- a/rust/src/dcerpc/dcerpc.rs +++ b/rust/src/dcerpc/dcerpc.rs @@ -902,7 +902,7 @@ impl DCERPCState { } } let parsed = self.handle_common_stub( - &input, + input, (input.len() - leftover_input.len()) as u16, core::STREAM_TOSERVER, ); @@ -998,7 +998,7 @@ impl DCERPCState { // Check if header data was complete. In case of EoF or incomplete data, wait for more // data else return error if self.bytes_consumed < DCERPC_HDR_LEN && input_len > 0 { - parsed = self.process_header(&buffer); + parsed = self.process_header(buffer); if parsed == -1 { self.extend_buffer(buffer, direction); return AppLayerResult::ok(); @@ -1121,7 +1121,7 @@ fn evaluate_stub_params( } let input_slice = &input[..stub_len as usize]; - stub_data_buffer.extend_from_slice(&input_slice); + stub_data_buffer.extend_from_slice(input_slice); stub_len } @@ -1779,7 +1779,7 @@ mod tests { 0x69, 0x00, ]; let mut dcerpc_state = DCERPCState::new(); - assert_eq!(16, dcerpc_state.process_header(&request)); + assert_eq!(16, dcerpc_state.process_header(request)); assert_eq!(1008, dcerpc_state.process_request_pdu(&request[16..])); } @@ -1864,7 +1864,7 @@ mod tests { let mut dcerpc_state = DCERPCState::new(); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&request, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(request, core::STREAM_TOSERVER) ); if let Some(hdr) = dcerpc_state.header { assert_eq!(0, hdr.hdrtype); @@ -1900,11 +1900,11 @@ mod tests { let mut dcerpc_state = DCERPCState::new(); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&bind1, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(bind1, core::STREAM_TOSERVER) ); assert_eq!( AppLayerResult::ok(), // TODO ASK if this is correct? - dcerpc_state.handle_input_data(&bind2, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(bind2, core::STREAM_TOSERVER) ); } @@ -1970,11 +1970,11 @@ mod tests { let mut dcerpc_state = DCERPCState::new(); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&bind1, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(bind1, core::STREAM_TOSERVER) ); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&bind2, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(bind2, core::STREAM_TOSERVER) ); if let Some(ref bind) = dcerpc_state.bind { assert_eq!(16, bind.numctxitems); @@ -1994,15 +1994,15 @@ mod tests { let mut dcerpc_state = DCERPCState::new(); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&request1, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(request1, core::STREAM_TOSERVER) ); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&request2, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(request2, core::STREAM_TOSERVER) ); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&request3, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(request3, core::STREAM_TOSERVER) ); let tx = &dcerpc_state.transactions[0]; assert_eq!(20, tx.stub_data_buffer_ts.len()); @@ -2018,7 +2018,7 @@ mod tests { let mut dcerpc_state = DCERPCState::new(); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&request1, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(request1, core::STREAM_TOSERVER) ); } @@ -2032,7 +2032,7 @@ mod tests { let mut dcerpc_state = DCERPCState::new(); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&request1, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(request1, core::STREAM_TOSERVER) ); } @@ -2052,15 +2052,15 @@ mod tests { let mut dcerpc_state = DCERPCState::new(); assert_eq!( AppLayerResult::err(), - dcerpc_state.handle_input_data(&fault, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(fault, core::STREAM_TOSERVER) ); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&request1, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(request1, core::STREAM_TOSERVER) ); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&request2, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(request2, core::STREAM_TOSERVER) ); let tx = &dcerpc_state.transactions[0]; assert_eq!(12, tx.stub_data_buffer_ts.len()); @@ -2082,15 +2082,15 @@ mod tests { let mut dcerpc_state = DCERPCState::new(); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&request1, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(request1, core::STREAM_TOSERVER) ); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&request2, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(request2, core::STREAM_TOSERVER) ); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&request3, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(request3, core::STREAM_TOSERVER) ); } @@ -2110,11 +2110,11 @@ mod tests { dcerpc_state.data_needed_for_dir = core::STREAM_TOCLIENT; assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&bind_ack1, core::STREAM_TOCLIENT) + dcerpc_state.handle_input_data(bind_ack1, core::STREAM_TOCLIENT) ); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&bind_ack2, core::STREAM_TOCLIENT) + dcerpc_state.handle_input_data(bind_ack2, core::STREAM_TOCLIENT) ); } @@ -2137,7 +2137,7 @@ mod tests { ]; assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&bindbuf, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(bindbuf, core::STREAM_TOSERVER) ); if let Some(ref bind) = dcerpc_state.bind { let bind_uuid = &bind.uuid_list[0].uuid; @@ -2171,7 +2171,7 @@ mod tests { let mut dcerpc_state = DCERPCState::new(); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&bindbuf, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(bindbuf, core::STREAM_TOSERVER) ); } @@ -2191,7 +2191,7 @@ mod tests { dcerpc_state.data_needed_for_dir = core::STREAM_TOCLIENT; assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&bind_ack, core::STREAM_TOCLIENT) + dcerpc_state.handle_input_data(bind_ack, core::STREAM_TOCLIENT) ); } @@ -2442,11 +2442,11 @@ mod tests { ]; assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&bind1, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(bind1, core::STREAM_TOSERVER) ); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&bind_ack1, core::STREAM_TOCLIENT) + dcerpc_state.handle_input_data(bind_ack1, core::STREAM_TOCLIENT) ); if let Some(ref back) = dcerpc_state.bindack { assert_eq!(1, back.accepted_uuid_list.len()); @@ -2455,11 +2455,11 @@ mod tests { } assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&bind2, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(bind2, core::STREAM_TOSERVER) ); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&bind_ack2, core::STREAM_TOCLIENT) + dcerpc_state.handle_input_data(bind_ack2, core::STREAM_TOCLIENT) ); if let Some(ref back) = dcerpc_state.bindack { assert_eq!(1, back.accepted_uuid_list.len()); @@ -2468,11 +2468,11 @@ mod tests { } assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&bind3, core::STREAM_TOSERVER) + dcerpc_state.handle_input_data(bind3, core::STREAM_TOSERVER) ); assert_eq!( AppLayerResult::ok(), - dcerpc_state.handle_input_data(&bind_ack3, core::STREAM_TOCLIENT) + dcerpc_state.handle_input_data(bind_ack3, core::STREAM_TOCLIENT) ); if let Some(ref back) = dcerpc_state.bindack { assert_eq!(1, back.accepted_uuid_list.len()); diff --git a/rust/src/dcerpc/dcerpc_udp.rs b/rust/src/dcerpc/dcerpc_udp.rs index 44990ee484c9..42184d44bd6f 100644 --- a/rust/src/dcerpc/dcerpc_udp.rs +++ b/rust/src/dcerpc/dcerpc_udp.rs @@ -145,7 +145,7 @@ impl DCERPCUDPState { match hdr.pkt_type { DCERPC_TYPE_REQUEST => { - tx.stub_data_buffer_ts.extend_from_slice(&input); + tx.stub_data_buffer_ts.extend_from_slice(input); tx.frag_cnt_ts += 1; if done { tx.req_done = true; @@ -153,7 +153,7 @@ impl DCERPCUDPState { return true; } DCERPC_TYPE_RESPONSE => { - tx.stub_data_buffer_tc.extend_from_slice(&input); + tx.stub_data_buffer_tc.extend_from_slice(input); tx.frag_cnt_tc += 1; if done { tx.resp_done = true; diff --git a/rust/src/dcerpc/detect.rs b/rust/src/dcerpc/detect.rs index 5be74782a6c1..3a8fb632d794 100644 --- a/rust/src/dcerpc/detect.rs +++ b/rust/src/dcerpc/detect.rs @@ -280,7 +280,7 @@ pub unsafe extern "C" fn rs_dcerpc_iface_parse(carg: *const c_char) -> *mut c_vo } }; - match parse_iface_data(&arg) { + match parse_iface_data(arg) { Ok(detect) => Box::into_raw(Box::new(detect)) as *mut _, Err(_) => std::ptr::null_mut(), } @@ -327,7 +327,7 @@ pub unsafe extern "C" fn rs_dcerpc_opnum_parse(carg: *const c_char) -> *mut c_vo } }; - match parse_opnum_data(&arg) { + match parse_opnum_data(arg) { Ok(detect) => Box::into_raw(Box::new(detect)) as *mut _, Err(_) => std::ptr::null_mut(), } diff --git a/rust/src/dns/detect.rs b/rust/src/dns/detect.rs index 3cf626e0172d..326e9c38a714 100644 --- a/rust/src/dns/detect.rs +++ b/rust/src/dns/detect.rs @@ -107,7 +107,7 @@ pub unsafe extern "C" fn rs_detect_dns_opcode_parse(carg: *const c_char) -> *mut } }; - match parse_opcode(&arg) { + match parse_opcode(arg) { Ok(detect) => Box::into_raw(Box::new(detect)) as *mut _, Err(_) => std::ptr::null_mut(), } diff --git a/rust/src/dns/dns.rs b/rust/src/dns/dns.rs index 4bb8d6de8fbd..467c5a9acb0d 100644 --- a/rust/src/dns/dns.rs +++ b/rust/src/dns/dns.rs @@ -564,7 +564,7 @@ impl DNSState { if cur_i.len() == 1 { return AppLayerResult::incomplete(consumed as u32, 2 as u32); } - let size = match be_u16(&cur_i) as IResult<&[u8],u16> { + let size = match be_u16(cur_i) as IResult<&[u8],u16> { Ok((_, len)) => len, _ => 0 } as usize; @@ -611,7 +611,7 @@ impl DNSState { if cur_i.len() == 1 { return AppLayerResult::incomplete(consumed as u32, 2 as u32); } - let size = match be_u16(&cur_i) as IResult<&[u8],u16> { + let size = match be_u16(cur_i) as IResult<&[u8],u16> { Ok((_, len)) => len, _ => 0 } as usize; diff --git a/rust/src/dns/log.rs b/rust/src/dns/log.rs index 12c6358c7e0b..22ffefa5218d 100644 --- a/rust/src/dns/log.rs +++ b/rust/src/dns/log.rs @@ -453,7 +453,7 @@ fn dns_log_json_answer_detail(answer: &DNSAnswerEntry) -> Result { - jsa.set_string("rdata", &dns_print_addr(&addr))?; + jsa.set_string("rdata", &dns_print_addr(addr))?; } DNSRData::CNAME(bytes) | DNSRData::MX(bytes) | @@ -461,16 +461,16 @@ fn dns_log_json_answer_detail(answer: &DNSAnswerEntry) -> Result { - jsa.set_string_from_bytes("rdata", &bytes)?; + jsa.set_string_from_bytes("rdata", bytes)?; } DNSRData::SOA(soa) => { - jsa.set_object("soa", &dns_log_soa(&soa)?)?; + jsa.set_object("soa", &dns_log_soa(soa)?)?; } DNSRData::SSHFP(sshfp) => { - jsa.set_object("sshfp", &dns_log_sshfp(&sshfp)?)?; + jsa.set_object("sshfp", &dns_log_sshfp(sshfp)?)?; } DNSRData::SRV(srv) => { - jsa.set_object("srv", &dns_log_srv(&srv)?)?; + jsa.set_object("srv", &dns_log_srv(srv)?)?; } _ => {} } @@ -528,7 +528,7 @@ fn dns_log_json_answer(js: &mut JsonBuilder, response: &DNSResponse, flags: u64) JsonBuilder::new_array()); } if let Some(a) = answer_types.get_mut(&type_string) { - a.append_string(&dns_print_addr(&addr))?; + a.append_string(&dns_print_addr(addr))?; } } DNSRData::CNAME(bytes) | @@ -542,7 +542,7 @@ fn dns_log_json_answer(js: &mut JsonBuilder, response: &DNSResponse, flags: u64) JsonBuilder::new_array()); } if let Some(a) = answer_types.get_mut(&type_string) { - a.append_string_from_bytes(&bytes)?; + a.append_string_from_bytes(bytes)?; } }, DNSRData::SOA(soa) => { @@ -551,7 +551,7 @@ fn dns_log_json_answer(js: &mut JsonBuilder, response: &DNSResponse, flags: u64) JsonBuilder::new_array()); } if let Some(a) = answer_types.get_mut(&type_string) { - a.append_object(&dns_log_soa(&soa)?)?; + a.append_object(&dns_log_soa(soa)?)?; } }, DNSRData::SSHFP(sshfp) => { @@ -560,7 +560,7 @@ fn dns_log_json_answer(js: &mut JsonBuilder, response: &DNSResponse, flags: u64) JsonBuilder::new_array()); } if let Some(a) = answer_types.get_mut(&type_string) { - a.append_object(&dns_log_sshfp(&sshfp)?)?; + a.append_object(&dns_log_sshfp(sshfp)?)?; } }, DNSRData::SRV(srv) => { @@ -569,7 +569,7 @@ fn dns_log_json_answer(js: &mut JsonBuilder, response: &DNSResponse, flags: u64) JsonBuilder::new_array()); } if let Some(a) = answer_types.get_mut(&type_string) { - a.append_object(&dns_log_srv(&srv)?)?; + a.append_object(&dns_log_srv(srv)?)?; } }, _ => {} @@ -712,7 +712,7 @@ fn dns_log_json_answer_v1(header: &DNSHeader, answer: &DNSAnswerEntry) match &answer.data { DNSRData::A(addr) | DNSRData::AAAA(addr) => { - js.set_string("rdata", &dns_print_addr(&addr))?; + js.set_string("rdata", &dns_print_addr(addr))?; } DNSRData::CNAME(bytes) | DNSRData::MX(bytes) | @@ -720,13 +720,13 @@ fn dns_log_json_answer_v1(header: &DNSHeader, answer: &DNSAnswerEntry) DNSRData::TXT(bytes) | DNSRData::NULL(bytes) | DNSRData::PTR(bytes) => { - js.set_string_from_bytes("rdata", &bytes)?; + js.set_string_from_bytes("rdata", bytes)?; } DNSRData::SOA(soa) => { - js.set_object("soa", &dns_log_soa(&soa)?)?; + js.set_object("soa", &dns_log_soa(soa)?)?; } DNSRData::SSHFP(sshfp) => { - js.set_object("sshfp", &dns_log_sshfp(&sshfp)?)?; + js.set_object("sshfp", &dns_log_sshfp(sshfp)?)?; } _ => {} } diff --git a/rust/src/ffi/hashing.rs b/rust/src/ffi/hashing.rs index 7f9f09aef76d..c04ad19b3784 100644 --- a/rust/src/ffi/hashing.rs +++ b/rust/src/ffi/hashing.rs @@ -66,7 +66,7 @@ pub unsafe extern "C" fn SCSha256FinalizeToHex(hasher: &mut SCSha256, out: *mut let output = std::slice::from_raw_parts_mut(out, len as usize); // This will panic if the sizes differ. - output[0..len as usize - 1].copy_from_slice(&hex.as_bytes()); + output[0..len as usize - 1].copy_from_slice(hex.as_bytes()); // Terminate the string. output[output.len() - 1] = 0; @@ -171,7 +171,7 @@ pub unsafe extern "C" fn SCMd5FinalizeToHex(hasher: &mut SCMd5, out: *mut c_char let output = std::slice::from_raw_parts_mut(out, len as usize); // This will panic if the sizes differ. - output[0..len as usize - 1].copy_from_slice(&hex.as_bytes()); + output[0..len as usize - 1].copy_from_slice(hex.as_bytes()); // Terminate the string. output[output.len() - 1] = 0; @@ -205,7 +205,7 @@ pub unsafe extern "C" fn SCMd5HashBufferToHex( let hex = format!("{:x}", &hash); // This will panic if the sizes differ. - output[0..len as usize - 1].copy_from_slice(&hex.as_bytes()); + output[0..len as usize - 1].copy_from_slice(hex.as_bytes()); // Terminate the string. output[output.len() - 1] = 0; diff --git a/rust/src/filecontainer.rs b/rust/src/filecontainer.rs index d61d2461644b..e2ff51db5c31 100644 --- a/rust/src/filecontainer.rs +++ b/rust/src/filecontainer.rs @@ -77,7 +77,7 @@ impl FileContainer { match unsafe {SC} { None => panic!("BUG no suricata_config"), Some(c) => { - (c.FileContainerRecycle)(&self); + (c.FileContainerRecycle)(self); }, } } @@ -88,7 +88,7 @@ impl FileContainer { Some(c) => { SCLogDebug!("FILE {:p} OPEN flags {:04X}", &self, flags); - let res = (c.FileOpenFile)(&self, cfg.files_sbcfg, *track_id, + let res = (c.FileOpenFile)(self, cfg.files_sbcfg, *track_id, name.as_ptr(), name.len() as u16, ptr::null(), 0u32, flags); res @@ -107,13 +107,13 @@ impl FileContainer { let res = match is_gap { false => { SCLogDebug!("appending file data"); - let r = (c.FileAppendData)(&self, *track_id, + let r = (c.FileAppendData)(self, *track_id, data.as_ptr(), data.len() as u32); r }, true => { SCLogDebug!("appending GAP"); - let r = (c.FileAppendGAP)(&self, *track_id, + let r = (c.FileAppendGAP)(self, *track_id, data.as_ptr(), data.len() as u32); r }, @@ -129,7 +129,7 @@ impl FileContainer { match unsafe {SC} { None => panic!("BUG no suricata_config"), Some(c) => { - let res = (c.FileCloseFile)(&self, *track_id, ptr::null(), 0u32, flags); + let res = (c.FileCloseFile)(self, *track_id, ptr::null(), 0u32, flags); res } } @@ -141,7 +141,7 @@ impl FileContainer { match unsafe {SC} { None => panic!("BUG no suricata_config"), Some(c) => { - (c.FilePrune)(&self); + (c.FilePrune)(self); } } } @@ -150,7 +150,7 @@ impl FileContainer { match unsafe {SC} { None => panic!("BUG no suricata_config"), Some(c) => { - (c.FileSetTx)(&self, tx_id); + (c.FileSetTx)(self, tx_id); } } } diff --git a/rust/src/http2/detect.rs b/rust/src/http2/detect.rs index e017ec5b9031..296153bba47b 100644 --- a/rust/src/http2/detect.rs +++ b/rust/src/http2/detect.rs @@ -289,7 +289,7 @@ fn http2_detect_settingsctx_match( for i in 0..tx.frames_ts.len() { match &tx.frames_ts[i].data { HTTP2FrameTypeData::SETTINGS(set) => { - if http2_detect_settings_match(&set, ctx) != 0 { + if http2_detect_settings_match(set, ctx) != 0 { return 1; } } @@ -300,7 +300,7 @@ fn http2_detect_settingsctx_match( for i in 0..tx.frames_tc.len() { match &tx.frames_tc[i].data { HTTP2FrameTypeData::SETTINGS(set) => { - if http2_detect_settings_match(&set, ctx) != 0 { + if http2_detect_settings_match(set, ctx) != 0 { return 1; } } @@ -390,7 +390,7 @@ fn http2_detect_sizeupdatectx_match( for i in 0..tx.frames_ts.len() { match &tx.frames_ts[i].data { HTTP2FrameTypeData::HEADERS(hd) => { - if http2_detect_sizeupdate_match(&hd, ctx) != 0 { + if http2_detect_sizeupdate_match(hd, ctx) != 0 { return 1; } } @@ -401,7 +401,7 @@ fn http2_detect_sizeupdatectx_match( for i in 0..tx.frames_tc.len() { match &tx.frames_tc[i].data { HTTP2FrameTypeData::HEADERS(hd) => { - if http2_detect_sizeupdate_match(&hd, ctx) != 0 { + if http2_detect_sizeupdate_match(hd, ctx) != 0 { return 1; } } @@ -634,7 +634,7 @@ pub unsafe extern "C" fn rs_http2_tx_get_header( match &tx.frames_ts[i].data { HTTP2FrameTypeData::HEADERS(hd) => { if nb < pos + hd.blocks.len() as u32 { - let ehdr = http2_escape_header(&hd, nb - pos); + let ehdr = http2_escape_header(hd, nb - pos); tx.escaped.push(ehdr); let idx = tx.escaped.len() - 1; let value = &tx.escaped[idx]; @@ -653,7 +653,7 @@ pub unsafe extern "C" fn rs_http2_tx_get_header( match &tx.frames_tc[i].data { HTTP2FrameTypeData::HEADERS(hd) => { if nb < pos + hd.blocks.len() as u32 { - let ehdr = http2_escape_header(&hd, nb - pos); + let ehdr = http2_escape_header(hd, nb - pos); tx.escaped.push(ehdr); let idx = tx.escaped.len() - 1; let value = &tx.escaped[idx]; diff --git a/rust/src/ike/ike.rs b/rust/src/ike/ike.rs index 1b1fb0bd8b35..38aefeb01bf7 100644 --- a/rust/src/ike/ike.rs +++ b/rust/src/ike/ike.rs @@ -179,7 +179,7 @@ impl IKEState { let tx = self .transactions .iter() - .position(|ref tx| tx.tx_id == tx_id + 1); + .position(|tx| tx.tx_id == tx_id + 1); debug_assert!(tx != None); if let Some(idx) = tx { let _ = self.transactions.remove(idx); diff --git a/rust/src/ike/ikev2.rs b/rust/src/ike/ikev2.rs index 53e7072c245c..c6e4d24f7cb8 100644 --- a/rust/src/ike/ikev2.rs +++ b/rust/src/ike/ikev2.rs @@ -216,7 +216,7 @@ pub fn handle_ikev2( } fn add_proposals(state: &mut IKEState, prop: &Vec, direction: u8) { - for ref p in prop { + for p in prop { let transforms: Vec = p.transforms.iter().map(|x| x.into()).collect(); // Rule 1: warn on weak or unknown transforms for xform in &transforms { diff --git a/rust/src/ike/logger.rs b/rust/src/ike/logger.rs index c6809e933099..b5ee8fdb3a80 100644 --- a/rust/src/ike/logger.rs +++ b/rust/src/ike/logger.rs @@ -40,7 +40,7 @@ fn add_attributes(transform: &Vec, js: &mut JsonBuilder) -> Result< } else if let Some(hex_value) = &attribute.hex_value { js.set_string( format!("{}_raw", attribute.attribute_type).as_str(), - &hex_value, + hex_value, )?; } } @@ -89,9 +89,9 @@ fn log_ike( } } else if tx.ike_version == 2 { if tx.hdr.flags & IKEV2_FLAG_INITIATOR != 0 { - jb.set_string("role", &"initiator")?; + jb.set_string("role", "initiator")?; } else { - jb.set_string("role", &"responder")?; + jb.set_string("role", "responder")?; jb.set_string("alg_enc", &format!("{:?}", state.ikev2_container.alg_enc))?; jb.set_string("alg_auth", &format!("{:?}", state.ikev2_container.alg_auth))?; jb.set_string("alg_prf", &format!("{:?}", state.ikev2_container.alg_prf))?; diff --git a/rust/src/krb/krb5.rs b/rust/src/krb/krb5.rs index 3f619bb29684..733e0d1e35f8 100644 --- a/rust/src/krb/krb5.rs +++ b/rust/src/krb/krb5.rs @@ -210,7 +210,7 @@ impl KRB5State { } fn free_tx(&mut self, tx_id: u64) { - let tx = self.transactions.iter().position(|ref tx| tx.id == tx_id + 1); + let tx = self.transactions.iter().position(|tx| tx.id == tx_id + 1); debug_assert!(tx != None); if let Some(idx) = tx { let _ = self.transactions.remove(idx); diff --git a/rust/src/modbus/detect.rs b/rust/src/modbus/detect.rs index a540edafc5c9..44d873161229 100644 --- a/rust/src/modbus/detect.rs +++ b/rust/src/modbus/detect.rs @@ -154,9 +154,9 @@ pub unsafe extern "C" fn rs_modbus_parse(c_arg: *const c_char) -> *mut c_void { return std::ptr::null_mut(); } if let Ok(arg) = CStr::from_ptr(c_arg).to_str() { - match parse_unit_id(&arg) - .or_else(|_| parse_function(&arg)) - .or_else(|_| parse_access(&arg)) + match parse_unit_id(arg) + .or_else(|_| parse_function(arg)) + .or_else(|_| parse_access(arg)) { Ok(detect) => return Box::into_raw(Box::new(detect)) as *mut c_void, Err(()) => return std::ptr::null_mut(), diff --git a/rust/src/modbus/log.rs b/rust/src/modbus/log.rs index ff8eef2fcf17..6724291de786 100644 --- a/rust/src/modbus/log.rs +++ b/rust/src/modbus/log.rs @@ -32,13 +32,13 @@ fn log(tx: &ModbusTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> { if let Some(req) = &tx.request { js.open_object("request")?; - log_message(&req, js)?; + log_message(req, js)?; js.close()?; } if let Some(resp) = &tx.response { js.open_object("response")?; - log_message(&resp, js)?; + log_message(resp, js)?; js.close()?; } @@ -67,14 +67,14 @@ fn log_message(msg: &Message, js: &mut JsonBuilder) -> Result<(), JsonError> { js.open_object("diagnostic")?; js.set_uint("raw", func.raw.into())?; js.set_string("code", &func.code.to_string())?; - js.set_string_from_bytes("data", &data)?; + js.set_string_from_bytes("data", data)?; js.close()?; } Data::MEI { mei_type, data } => { js.open_object("mei")?; js.set_uint("raw", mei_type.raw.into())?; js.set_string("code", &mei_type.code.to_string())?; - js.set_string_from_bytes("data", &data)?; + js.set_string_from_bytes("data", data)?; js.close()?; } Data::Read(read) => { @@ -96,7 +96,7 @@ fn log_message(msg: &Message, js: &mut JsonBuilder) -> Result<(), JsonError> { js.close()?; } Data::ByteVec(data) => { - js.set_string_from_bytes("data", &data)?; + js.set_string_from_bytes("data", data)?; } Data::Empty => {} } @@ -111,7 +111,7 @@ fn log_read(read: &Read, js: &mut JsonBuilder) -> Result<(), JsonError> { js.set_uint("quantity", (*quantity).into())?; } Read::Response(data) => { - js.set_string_from_bytes("data", &data)?; + js.set_string_from_bytes("data", data)?; } } @@ -127,7 +127,7 @@ fn log_write(write: &Write, js: &mut JsonBuilder) -> Result<(), JsonError> { } => { js.set_uint("address", (*address).into())?; js.set_uint("quantity", (*quantity).into())?; - js.set_string_from_bytes("data", &data)?; + js.set_string_from_bytes("data", data)?; } Write::Mask { address, diff --git a/rust/src/modbus/modbus.rs b/rust/src/modbus/modbus.rs index e901e3c54146..5fc6df4ad97c 100644 --- a/rust/src/modbus/modbus.rs +++ b/rust/src/modbus/modbus.rs @@ -1082,7 +1082,7 @@ mod tests { let mut state = ModbusState::new(); assert_eq!( AppLayerResult::ok(), - state.parse(&RD_COILS_REQ, Direction::ToServer) + state.parse(RD_COILS_REQ, Direction::ToServer) ); assert_eq!(state.transactions.len(), 1); @@ -1099,7 +1099,7 @@ mod tests { assert_eq!( AppLayerResult::ok(), - state.parse(&RD_COILS_RESP, Direction::ToClient) + state.parse(RD_COILS_RESP, Direction::ToClient) ); assert_eq!(state.transactions.len(), 1); @@ -1114,7 +1114,7 @@ mod tests { let mut state = ModbusState::new(); assert_eq!( AppLayerResult::ok(), - state.parse(&WR_MULT_REG_REQ, Direction::ToServer) + state.parse(WR_MULT_REG_REQ, Direction::ToServer) ); assert_eq!(state.transactions.len(), 1); @@ -1132,7 +1132,7 @@ mod tests { assert_eq!( AppLayerResult::ok(), - state.parse(&WR_MULT_REG_RESP, Direction::ToClient) + state.parse(WR_MULT_REG_RESP, Direction::ToClient) ); assert_eq!(state.transactions.len(), 1); @@ -1153,7 +1153,7 @@ mod tests { let mut state = ModbusState::new(); assert_eq!( AppLayerResult::ok(), - state.parse(&RD_WR_MULT_REG_REQ, Direction::ToServer) + state.parse(RD_WR_MULT_REG_REQ, Direction::ToServer) ); assert_eq!(state.transactions.len(), 1); @@ -1177,7 +1177,7 @@ mod tests { assert_eq!( AppLayerResult::ok(), - state.parse(&RD_WR_MULT_REG_RESP, Direction::ToClient) + state.parse(RD_WR_MULT_REG_RESP, Direction::ToClient) ); assert_eq!(state.transactions.len(), 1); @@ -1197,7 +1197,7 @@ mod tests { let mut state = ModbusState::new(); assert_eq!( AppLayerResult::ok(), - state.parse(&FORCE_LISTEN_ONLY_MODE, Direction::ToServer) + state.parse(FORCE_LISTEN_ONLY_MODE, Direction::ToServer) ); assert_eq!(state.transactions.len(), 1); @@ -1221,7 +1221,7 @@ mod tests { let mut state = ModbusState::new(); assert_eq!( AppLayerResult::ok(), - state.parse(&INVALID_PROTO_REQ, Direction::ToServer) + state.parse(INVALID_PROTO_REQ, Direction::ToServer) ); assert_eq!(state.transactions.len(), 1); @@ -1235,7 +1235,7 @@ mod tests { let mut state = ModbusState::new(); assert_eq!( AppLayerResult::ok(), - state.parse(&RD_COILS_RESP, Direction::ToClient) + state.parse(RD_COILS_RESP, Direction::ToClient) ); assert_eq!(state.transactions.len(), 1); @@ -1250,7 +1250,7 @@ mod tests { let mut state = ModbusState::new(); assert_eq!( AppLayerResult::incomplete(15, 4), - state.parse(&INVALID_LEN_WR_MULT_REG_REQ, Direction::ToServer) + state.parse(INVALID_LEN_WR_MULT_REG_REQ, Direction::ToServer) ); assert_eq!(state.transactions.len(), 1); @@ -1273,7 +1273,7 @@ mod tests { let mut state = ModbusState::new(); assert_eq!( AppLayerResult::ok(), - state.parse(&RD_COILS_REQ, Direction::ToServer) + state.parse(RD_COILS_REQ, Direction::ToServer) ); assert_eq!(state.transactions.len(), 1); @@ -1290,7 +1290,7 @@ mod tests { assert_eq!( AppLayerResult::ok(), - state.parse(&RD_COILS_ERR_RESP, Direction::ToClient) + state.parse(RD_COILS_ERR_RESP, Direction::ToClient) ); assert_eq!(state.transactions.len(), 1); @@ -1319,7 +1319,7 @@ mod tests { assert_eq!(state.transactions.len(), 0); assert_eq!( AppLayerResult::ok(), - state.parse(&RD_COILS_REQ, Direction::ToServer) + state.parse(RD_COILS_REQ, Direction::ToServer) ); assert_eq!(state.transactions.len(), 1); @@ -1396,7 +1396,7 @@ mod tests { let mut state = ModbusState::new(); assert_eq!( AppLayerResult::ok(), - state.parse(&EXCEEDED_LEN_WR_MULT_REG_REQ, Direction::ToServer) + state.parse(EXCEEDED_LEN_WR_MULT_REG_REQ, Direction::ToServer) ); assert_eq!(state.transactions.len(), 1); @@ -1410,7 +1410,7 @@ mod tests { let mut state = ModbusState::new(); assert_eq!( AppLayerResult::ok(), - state.parse(&INVALID_PDU_WR_MULT_REG_REQ, Direction::ToServer) + state.parse(INVALID_PDU_WR_MULT_REG_REQ, Direction::ToServer) ); assert_eq!(state.transactions.len(), 1); @@ -1426,7 +1426,7 @@ mod tests { let mut state = ModbusState::new(); assert_eq!( AppLayerResult::ok(), - state.parse(&MASK_WR_REG_REQ, Direction::ToServer) + state.parse(MASK_WR_REG_REQ, Direction::ToServer) ); assert_eq!(state.transactions.len(), 1); @@ -1444,7 +1444,7 @@ mod tests { assert_eq!( AppLayerResult::ok(), - state.parse(&MASK_WR_REG_RESP, Direction::ToClient) + state.parse(MASK_WR_REG_RESP, Direction::ToClient) ); assert_eq!(state.transactions.len(), 1); @@ -1466,7 +1466,7 @@ mod tests { let mut state = ModbusState::new(); assert_eq!( AppLayerResult::ok(), - state.parse(&WR_SINGLE_REG_REQ, Direction::ToServer) + state.parse(WR_SINGLE_REG_REQ, Direction::ToServer) ); assert_eq!(state.transactions.len(), 1); @@ -1483,7 +1483,7 @@ mod tests { assert_eq!( AppLayerResult::ok(), - state.parse(&WR_SINGLE_REG_RESP, Direction::ToClient) + state.parse(WR_SINGLE_REG_RESP, Direction::ToClient) ); assert_eq!(state.transactions.len(), 1); @@ -1504,7 +1504,7 @@ mod tests { let mut state = ModbusState::new(); assert_eq!( AppLayerResult::ok(), - state.parse(&INVALID_MASK_WR_REG_REQ, Direction::ToServer) + state.parse(INVALID_MASK_WR_REG_REQ, Direction::ToServer) ); assert_eq!(state.transactions.len(), 1); @@ -1516,7 +1516,7 @@ mod tests { assert_eq!( AppLayerResult::ok(), - state.parse(&MASK_WR_REG_RESP, Direction::ToClient) + state.parse(MASK_WR_REG_RESP, Direction::ToClient) ); assert_eq!(state.transactions.len(), 1); @@ -1538,7 +1538,7 @@ mod tests { let mut state = ModbusState::new(); assert_eq!( AppLayerResult::ok(), - state.parse(&INVALID_WR_SINGLE_REG_REQ, Direction::ToServer) + state.parse(INVALID_WR_SINGLE_REG_REQ, Direction::ToServer) ); assert_eq!(state.transactions.len(), 1); @@ -1550,7 +1550,7 @@ mod tests { assert_eq!( AppLayerResult::ok(), - state.parse(&WR_SINGLE_REG_RESP, Direction::ToClient) + state.parse(WR_SINGLE_REG_RESP, Direction::ToClient) ); assert_eq!(state.transactions.len(), 1); @@ -1571,7 +1571,7 @@ mod tests { let mut state = ModbusState::new(); assert_eq!( AppLayerResult::ok(), - state.parse(&INVALID_FUNC_CODE, Direction::ToServer) + state.parse(INVALID_FUNC_CODE, Direction::ToServer) ); assert_eq!(state.transactions.len(), 1); diff --git a/rust/src/mqtt/mqtt_property.rs b/rust/src/mqtt/mqtt_property.rs index 5f09c3d6f298..c407349f6048 100644 --- a/rust/src/mqtt/mqtt_property.rs +++ b/rust/src/mqtt/mqtt_property.rs @@ -68,13 +68,13 @@ impl crate::mqtt::mqtt_property::MQTTProperty { js.set_uint("message_expiry_interval", *v as u64)?; } crate::mqtt::mqtt_property::MQTTProperty::CONTENT_TYPE(v) => { - js.set_string("content_type", &v)?; + js.set_string("content_type", v)?; } crate::mqtt::mqtt_property::MQTTProperty::RESPONSE_TOPIC(v) => { - js.set_string("response_topic", &v)?; + js.set_string("response_topic", v)?; } crate::mqtt::mqtt_property::MQTTProperty::CORRELATION_DATA(v) => { - js.set_string_from_bytes("correlation_data", &v)?; + js.set_string_from_bytes("correlation_data", v)?; } crate::mqtt::mqtt_property::MQTTProperty::SUBSCRIPTION_IDENTIFIER(v) => { js.set_uint("subscription_identifier", *v as u64)?; @@ -83,16 +83,16 @@ impl crate::mqtt::mqtt_property::MQTTProperty { js.set_uint("session_expiry_interval", *v as u64)?; } crate::mqtt::mqtt_property::MQTTProperty::ASSIGNED_CLIENT_IDENTIFIER(v) => { - js.set_string("assigned_client_identifier", &v)?; + js.set_string("assigned_client_identifier", v)?; } crate::mqtt::mqtt_property::MQTTProperty::SERVER_KEEP_ALIVE(v) => { js.set_uint("server_keep_alive", *v as u64)?; } crate::mqtt::mqtt_property::MQTTProperty::AUTHENTICATION_METHOD(v) => { - js.set_string("authentication_method", &v)?; + js.set_string("authentication_method", v)?; } crate::mqtt::mqtt_property::MQTTProperty::AUTHENTICATION_DATA(v) => { - js.set_string_from_bytes("authentication_data", &v)?; + js.set_string_from_bytes("authentication_data", v)?; } crate::mqtt::mqtt_property::MQTTProperty::REQUEST_PROBLEM_INFORMATION(v) => { js.set_uint("request_problem_information", *v as u64)?; @@ -104,13 +104,13 @@ impl crate::mqtt::mqtt_property::MQTTProperty { js.set_uint("request_response_information", *v as u64)?; } crate::mqtt::mqtt_property::MQTTProperty::RESPONSE_INFORMATION(v) => { - js.set_string("response_information", &v)?; + js.set_string("response_information", v)?; } crate::mqtt::mqtt_property::MQTTProperty::SERVER_REFERENCE(v) => { - js.set_string("server_reference", &v)?; + js.set_string("server_reference", v)?; } crate::mqtt::mqtt_property::MQTTProperty::REASON_STRING(v) => { - js.set_string("reason_string", &v)?; + js.set_string("reason_string", v)?; } crate::mqtt::mqtt_property::MQTTProperty::RECEIVE_MAXIMUM(v) => { js.set_uint("receive_maximum", *v as u64)?; @@ -128,7 +128,7 @@ impl crate::mqtt::mqtt_property::MQTTProperty { js.set_uint("retain_available", *v as u64)?; } crate::mqtt::mqtt_property::MQTTProperty::USER_PROPERTY((k, v)) => { - js.set_string(k, &v)?; + js.set_string(k, v)?; } crate::mqtt::mqtt_property::MQTTProperty::MAXIMUM_PACKET_SIZE(v) => { js.set_uint("maximum_packet_size", *v as u64)?; diff --git a/rust/src/mqtt/parser.rs b/rust/src/mqtt/parser.rs index 86ad3d83b9ed..31adb8755a32 100644 --- a/rust/src/mqtt/parser.rs +++ b/rust/src/mqtt/parser.rs @@ -59,7 +59,7 @@ named!(#[inline], pub parse_mqtt_string, length: be_u16 >> content: take!(length) >> ( - String::from_utf8_lossy(&content).to_string() + String::from_utf8_lossy(content).to_string() ) )); @@ -124,7 +124,7 @@ fn parse_properties(input: &[u8], precond: bool) -> IResult<&[u8], Option return Err(e), } @@ -503,7 +503,7 @@ pub fn parse_message(input: &[u8], protocol_version: u8, max_msg_size: usize) -> // In this case we return the full input buffer, since this is // what the skipped_length value also refers to: header _and_ // remaining length. - return Ok((&input, msg)); + return Ok((input, msg)); } // We have not exceeded the maximum length limit, but still do not diff --git a/rust/src/nfs/nfs.rs b/rust/src/nfs/nfs.rs index f22483ae9f51..2187abb6b964 100644 --- a/rust/src/nfs/nfs.rs +++ b/rust/src/nfs/nfs.rs @@ -266,7 +266,7 @@ pub fn filetracker_newchunk(ft: &mut FileTransferTracker, files: &mut FileContai { match unsafe {SURICATA_NFS_FILE_CONFIG} { Some(sfcm) => { - ft.new_chunk(sfcm, files, flags, &name, data, chunk_offset, + ft.new_chunk(sfcm, files, flags, name, data, chunk_offset, chunk_size, fill_bytes, is_last, xid); } None => panic!("no SURICATA_NFS_FILE_CONFIG"), } @@ -1214,7 +1214,7 @@ impl NFSState { SCLogDebug!("large record {}, likely file xfer", rec_size); // quick peek, are in READ mode? - if self.peek_reply_record(&rpc_hdr) == NFSPROC3_READ { + if self.peek_reply_record(rpc_hdr) == NFSPROC3_READ { SCLogDebug!("CONFIRMED large READ record {}, likely file chunk xfer", rec_size); // we should have enough data to parse the RPC record diff --git a/rust/src/nfs/nfs2.rs b/rust/src/nfs/nfs2.rs index 44a49ead4b3a..945ba25f0aa1 100644 --- a/rust/src/nfs/nfs2.rs +++ b/rust/src/nfs/nfs2.rs @@ -99,7 +99,7 @@ impl NFSState { match parse_nfs2_reply_read(r.prog_data) { Ok((_, ref reply)) => { SCLogDebug!("NFSv2: READ reply record"); - self.process_read_record(r, reply, Some(&xidmap)); + self.process_read_record(r, reply, Some(xidmap)); nfs_status = reply.status; }, _ => { @@ -107,7 +107,7 @@ impl NFSState { }, } } else { - let stat : u32 = match be_u32(&r.prog_data) as IResult<&[u8],_> { + let stat : u32 = match be_u32(r.prog_data) as IResult<&[u8],_> { Ok((_, stat)) => stat, _ => 0 }; diff --git a/rust/src/nfs/nfs3.rs b/rust/src/nfs/nfs3.rs index f910b8a9195b..9160a80c160d 100644 --- a/rust/src/nfs/nfs3.rs +++ b/rust/src/nfs/nfs3.rs @@ -220,7 +220,7 @@ impl NFSState { }; } else if xidmap.procedure == NFSPROC3_READ { if let Ok((_, rd)) = parse_nfs3_reply_read(r.prog_data) { - self.process_read_record(r, &rd, Some(&xidmap)); + self.process_read_record(r, &rd, Some(xidmap)); nfs_status = rd.status; } else { self.set_event(NFSEvent::MalformedData); @@ -259,7 +259,7 @@ impl NFSState { } // for all other record types only parse the status else { - let stat : u32 = match be_u32(&r.prog_data) as IResult<&[u8],_> { + let stat : u32 = match be_u32(r.prog_data) as IResult<&[u8],_> { Ok((_, stat)) => stat, _ => 0 }; diff --git a/rust/src/nfs/nfs4.rs b/rust/src/nfs/nfs4.rs index 51d78800e3a7..790d44333871 100644 --- a/rust/src/nfs/nfs4.rs +++ b/rust/src/nfs/nfs4.rs @@ -197,7 +197,7 @@ impl NFSState { xidmap.file_name = rd.filename.to_vec(); main_opcode = NFSPROC4_CREATE; } - &Nfs4RequestContent::Remove(ref rd) => { + &Nfs4RequestContent::Remove(rd) => { SCLogDebug!("REMOVEv4: {:?}", rd); xidmap.file_name = rd.to_vec(); main_opcode = NFSPROC4_REMOVE; @@ -213,7 +213,7 @@ impl NFSState { } if main_opcode != 0 { - self.new_tx_v4(r, &xidmap, main_opcode, &aux_opcodes); + self.new_tx_v4(r, xidmap, main_opcode, &aux_opcodes); } } @@ -263,7 +263,7 @@ impl NFSState { match parse_nfs4_request_compound(data) { Ok((_, rd)) => { SCLogDebug!("NFSPROC4_COMPOUND: {:?}", rd); - self.compound_request(&r, &rd, &mut xidmap); + self.compound_request(r, &rd, &mut xidmap); }, Err(nom::Err::Incomplete(_n)) => { SCLogDebug!("NFSPROC4_COMPOUND: INCOMPLETE {:?}", _n); @@ -326,7 +326,7 @@ impl NFSState { data_len: rd.data.len() as u32, data: rd.data, }; - self.process_read_record(r, &reply, Some(&xidmap)); + self.process_read_record(r, &reply, Some(xidmap)); } }, &Nfs4ResponseContent::Open(_s, ref rd) => { @@ -388,7 +388,7 @@ impl NFSState { match parse_nfs4_response_compound(data) { Ok((_, rd)) => { SCLogDebug!("COMPOUNDv4: {:?}", rd); - self.compound_response(&r, &rd, xidmap); + self.compound_response(r, &rd, xidmap); }, Err(nom::Err::Incomplete(_)) => { self.set_event(NFSEvent::MalformedData); diff --git a/rust/src/ntp/ntp.rs b/rust/src/ntp/ntp.rs index 61283099eed6..6b0de9705aee 100644 --- a/rust/src/ntp/ntp.rs +++ b/rust/src/ntp/ntp.rs @@ -132,7 +132,7 @@ impl NTPState { } fn free_tx(&mut self, tx_id: u64) { - let tx = self.transactions.iter().position(|ref tx| tx.id == tx_id + 1); + let tx = self.transactions.iter().position(|tx| tx.id == tx_id + 1); debug_assert!(tx != None); if let Some(idx) = tx { let _ = self.transactions.remove(idx); diff --git a/rust/src/rdp/log.rs b/rust/src/rdp/log.rs index a2e806c0e287..14d75b86a793 100644 --- a/rust/src/rdp/log.rs +++ b/rust/src/rdp/log.rs @@ -364,7 +364,7 @@ fn mcs_req_to_json(mcs: &McsConnectRequest, js: &mut JsonBuilder) -> Result<(), if net.channels.len() > 0 { js.open_array("channels")?; for channel in &net.channels { - js.append_string(&channel)?; + js.append_string(channel)?; } js.close()?; } diff --git a/rust/src/rdp/rdp.rs b/rust/src/rdp/rdp.rs index dbe10300240e..e86b6dd1657b 100644 --- a/rust/src/rdp/rdp.rs +++ b/rust/src/rdp/rdp.rs @@ -175,7 +175,7 @@ impl RdpState { return AppLayerResult::ok(); } if self.tls_parsing { - match parse_tls_plaintext(&available) { + match parse_tls_plaintext(available) { Ok((remainder, _tls)) => { // bytes available for futher parsing are what remain available = remainder; @@ -195,7 +195,7 @@ impl RdpState { } } else { // every message should be encapsulated within a T.123 tpkt - match parse_t123_tpkt(&available) { + match parse_t123_tpkt(available) { // success Ok((remainder, t123)) => { // bytes available for futher parsing are what remain @@ -268,7 +268,7 @@ impl RdpState { return AppLayerResult::ok(); } if self.tls_parsing { - match parse_tls_plaintext(&available) { + match parse_tls_plaintext(available) { Ok((remainder, tls)) => { // bytes available for futher parsing are what remain available = remainder; @@ -307,7 +307,7 @@ impl RdpState { } } else { // every message should be encapsulated within a T.123 tpkt - match parse_t123_tpkt(&available) { + match parse_t123_tpkt(available) { // success Ok((remainder, t123)) => { // bytes available for futher parsing are what remain @@ -518,25 +518,25 @@ mod tests { #[test] fn test_probe_rdp() { let buf: &[u8] = &[0x03, 0x00]; - assert_eq!(true, probe_rdp(&buf)); + assert_eq!(true, probe_rdp(buf)); } #[test] fn test_probe_rdp_other() { let buf: &[u8] = &[0x04, 0x00]; - assert_eq!(false, probe_rdp(&buf)); + assert_eq!(false, probe_rdp(buf)); } #[test] fn test_probe_tls_handshake() { let buf: &[u8] = &[0x16, 0x00]; - assert_eq!(true, probe_tls_handshake(&buf)); + assert_eq!(true, probe_tls_handshake(buf)); } #[test] fn test_probe_tls_handshake_other() { let buf: &[u8] = &[0x17, 0x00]; - assert_eq!(false, probe_tls_handshake(&buf)); + assert_eq!(false, probe_tls_handshake(buf)); } #[test] @@ -549,10 +549,10 @@ mod tests { ]; let mut state = RdpState::new(); // will consume 0, request length + 1 - assert_eq!(AppLayerResult::incomplete(0, 9), state.parse_ts(&buf_1)); + assert_eq!(AppLayerResult::incomplete(0, 9), state.parse_ts(buf_1)); assert_eq!(0, state.transactions.len()); // exactly aligns with transaction - assert_eq!(AppLayerResult::ok(), state.parse_ts(&buf_2)); + assert_eq!(AppLayerResult::ok(), state.parse_ts(buf_2)); assert_eq!(1, state.transactions.len()); let item = RdpTransactionItem::X224ConnectionRequest(X224ConnectionRequest { cdt: 0, @@ -573,7 +573,7 @@ mod tests { fn test_parse_ts_other() { let buf: &[u8] = &[0x03, 0x00, 0x00, 0x01, 0x00]; let mut state = RdpState::new(); - assert_eq!(AppLayerResult::err(), state.parse_ts(&buf)); + assert_eq!(AppLayerResult::err(), state.parse_ts(buf)); } #[test] @@ -582,10 +582,10 @@ mod tests { let buf_2: &[u8] = &[0x03, 0x00, 0x00, 0x09, 0x02, 0xf0, 0x80, 0x7f, 0x66]; let mut state = RdpState::new(); // will consume 0, request length + 1 - assert_eq!(AppLayerResult::incomplete(0, 6), state.parse_tc(&buf_1)); + assert_eq!(AppLayerResult::incomplete(0, 6), state.parse_tc(buf_1)); assert_eq!(0, state.transactions.len()); // exactly aligns with transaction - assert_eq!(AppLayerResult::ok(), state.parse_tc(&buf_2)); + assert_eq!(AppLayerResult::ok(), state.parse_tc(buf_2)); assert_eq!(1, state.transactions.len()); let item = RdpTransactionItem::McsConnectResponse(McsConnectResponse {}); assert_eq!(item, state.transactions[0].item); @@ -595,7 +595,7 @@ mod tests { fn test_parse_tc_other() { let buf: &[u8] = &[0x03, 0x00, 0x00, 0x01, 0x00]; let mut state = RdpState::new(); - assert_eq!(AppLayerResult::err(), state.parse_tc(&buf)); + assert_eq!(AppLayerResult::err(), state.parse_tc(buf)); } #[test] diff --git a/rust/src/sip/log.rs b/rust/src/sip/log.rs index d7efbd89270e..792acfa49021 100644 --- a/rust/src/sip/log.rs +++ b/rust/src/sip/log.rs @@ -30,7 +30,7 @@ fn log(tx: &SIPTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> { } if let Some(req_line) = &tx.request_line { - js.set_string("request_line", &req_line)?; + js.set_string("request_line", req_line)?; } if let Some(resp) = &tx.response { @@ -40,7 +40,7 @@ fn log(tx: &SIPTransaction, js: &mut JsonBuilder) -> Result<(), JsonError> { } if let Some(resp_line) = &tx.response_line { - js.set_string("response_line", &resp_line)?; + js.set_string("response_line", resp_line)?; } js.close()?; diff --git a/rust/src/sip/sip.rs b/rust/src/sip/sip.rs index d725c195bf10..0a83e845818e 100755 --- a/rust/src/sip/sip.rs +++ b/rust/src/sip/sip.rs @@ -83,7 +83,7 @@ impl SIPState { let tx = self .transactions .iter() - .position(|ref tx| tx.id == tx_id + 1); + .position(|tx| tx.id == tx_id + 1); debug_assert!(tx != None); if let Some(idx) = tx { let _ = self.transactions.remove(idx); diff --git a/rust/src/smb/auth.rs b/rust/src/smb/auth.rs index fd7bc2742173..f90ab0ae61a7 100644 --- a/rust/src/smb/auth.rs +++ b/rust/src/smb/auth.rs @@ -123,7 +123,7 @@ fn parse_secblob_spnego(blob: &[u8]) -> Option } } }, - BerObjectContent::OctetString(ref os) => { + BerObjectContent::OctetString(os) => { if have_kerberos { match parse_kerberos5_request(os) { Ok((_, t)) => { diff --git a/rust/src/smb/dcerpc.rs b/rust/src/smb/dcerpc.rs index 9d44b39944c7..c379189fc363 100644 --- a/rust/src/smb/dcerpc.rs +++ b/rust/src/smb/dcerpc.rs @@ -199,7 +199,7 @@ pub fn smb_write_dcerpc_record<'b>(state: &mut SMBState, dcer.packet_type, tx.id, tx); if let Some(SMBTransactionTypeData::DCERPC(ref mut tdn)) = tx.type_data { SCLogDebug!("additional frag of size {}", recr.data.len()); - tdn.stub_data_ts.extend_from_slice(&recr.data); + tdn.stub_data_ts.extend_from_slice(recr.data); tdn.frag_cnt_ts += 1; SCLogDebug!("stub_data now {}", tdn.stub_data_ts.len()); } @@ -233,7 +233,7 @@ pub fn smb_write_dcerpc_record<'b>(state: &mut SMBState, SCLogDebug!("DCERPC: REQUEST {:?}", recr); if let Some(SMBTransactionTypeData::DCERPC(ref mut tdn)) = tx.type_data { SCLogDebug!("first frag size {}", recr.data.len()); - tdn.stub_data_ts.extend_from_slice(&recr.data); + tdn.stub_data_ts.extend_from_slice(recr.data); tdn.opnum = recr.opnum; tdn.frag_cnt_ts += 1; SCLogDebug!("DCERPC: REQUEST opnum {} stub data len {}", @@ -397,7 +397,7 @@ fn dcerpc_response_handle<'b>(tx: &mut SMBTransaction, if let Some(SMBTransactionTypeData::DCERPC(ref mut tdn)) = tx.type_data { SCLogDebug!("CMD 11 found at tx {}", tx.id); tdn.set_result(DCERPC_TYPE_RESPONSE); - tdn.stub_data_tc.extend_from_slice(&respr.data); + tdn.stub_data_tc.extend_from_slice(respr.data); tdn.frag_cnt_tc += 1; } tx.vercmd.set_ntstatus(ntstatus); @@ -452,7 +452,7 @@ pub fn smb_read_dcerpc_record<'b>(state: &mut SMBState, None => Vec::new(), }; SCLogDebug!("indata {} prevdata {}", indata.len(), prevdata.len()); - prevdata.extend_from_slice(&indata); + prevdata.extend_from_slice(indata); let data = prevdata; let mut malformed = false; diff --git a/rust/src/smb/files.rs b/rust/src/smb/files.rs index 0b9339d191f1..56567bafef74 100644 --- a/rust/src/smb/files.rs +++ b/rust/src/smb/files.rs @@ -51,7 +51,7 @@ pub fn filetracker_newchunk(ft: &mut FileTransferTracker, files: &mut FileContai { match unsafe {SURICATA_SMB_FILE_CONFIG} { Some(sfcm) => { - ft.new_chunk(sfcm, files, flags, &name, data, chunk_offset, + ft.new_chunk(sfcm, files, flags, name, data, chunk_offset, chunk_size, fill_bytes, is_last, xid); } None => panic!("no SURICATA_SMB_FILE_CONFIG"), } diff --git a/rust/src/smb/log.rs b/rust/src/smb/log.rs index 162dbe32026d..156d279511fa 100644 --- a/rust/src/smb/log.rs +++ b/rust/src/smb/log.rs @@ -70,13 +70,13 @@ fn smb_common_header(jsb: &mut JsonBuilder, state: &SMBState, tx: &SMBTransactio if state.dialect != 0 { let dialect = &smb2_dialect_string(state.dialect); - jsb.set_string("dialect", &dialect)?; + jsb.set_string("dialect", dialect)?; } else { let dialect = match &state.dialect_vec { - &Some(ref d) => str::from_utf8(&d).unwrap_or("invalid"), + &Some(ref d) => str::from_utf8(d).unwrap_or("invalid"), &None => "unknown", }; - jsb.set_string("dialect", &dialect)?; + jsb.set_string("dialect", dialect)?; } match tx.vercmd.get_version() { @@ -159,7 +159,7 @@ fn smb_common_header(jsb: &mut JsonBuilder, state: &SMBState, tx: &SMBTransactio jsb.set_string("realm", &ticket.realm.0)?; jsb.open_array("snames")?; for sname in ticket.sname.name_string.iter() { - jsb.append_string(&sname)?; + jsb.append_string(sname)?; } jsb.close()?; jsb.close()?; @@ -231,14 +231,14 @@ fn smb_common_header(jsb: &mut JsonBuilder, state: &SMBState, tx: &SMBTransactio if x.smb_ver == 1 { jsb.open_array("client_dialects")?; for d in &x.dialects { - let dialect = String::from_utf8_lossy(&d); + let dialect = String::from_utf8_lossy(d); jsb.append_string(&dialect)?; } jsb.close()?; } else if x.smb_ver == 2 { jsb.open_array("client_dialects")?; for d in &x.dialects2 { - let dialect = String::from_utf8_lossy(&d); + let dialect = String::from_utf8_lossy(d); jsb.append_string(&dialect)?; } jsb.close()?; @@ -265,11 +265,11 @@ fn smb_common_header(jsb: &mut JsonBuilder, state: &SMBState, tx: &SMBTransactio jsb.open_object("service")?; if let Some(ref s) = x.req_service { - let serv = String::from_utf8_lossy(&s); + let serv = String::from_utf8_lossy(s); jsb.set_string("request", &serv)?; } if let Some(ref s) = x.res_service { - let serv = String::from_utf8_lossy(&s); + let serv = String::from_utf8_lossy(s); jsb.set_string("response", &serv)?; } jsb.close()?; diff --git a/rust/src/smb/nbss_records.rs b/rust/src/smb/nbss_records.rs index 92845874432f..7256c8a91da6 100644 --- a/rust/src/smb/nbss_records.rs +++ b/rust/src/smb/nbss_records.rs @@ -109,7 +109,7 @@ mod tests { 0x64, 0x0b, 0x66, 0xba, 0x4a, 0xbb, 0x81, 0xe1, 0xea, 0x54, 0xae, 0xb8, 0x66]; - let result = parse_nbss_record(&buff); + let result = parse_nbss_record(buff); match result { Ok((remainder, p)) => { assert_eq!(p.message_type, NBSS_MSGTYPE_SESSION_MESSAGE); @@ -152,7 +152,7 @@ mod tests { 0x64, 0x0b, 0x66, 0xba, 0x4a, 0xbb, 0x81, 0xe1, 0xea, 0x54, 0xae, 0xb8, 0x66]; - let result_not_smb = parse_nbss_record(&buff_not_smb); + let result_not_smb = parse_nbss_record(buff_not_smb); match result_not_smb { Ok((remainder, p_not_smb)) => { assert_eq!(p_not_smb.message_type, NBSS_MSGTYPE_SESSION_MESSAGE); @@ -193,7 +193,7 @@ mod tests { 0x02, 0x08, 0xbd, 0x20, 0x02, 0x08, 0x06, 0x00, 0x02, 0x40, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00]; - let result = parse_nbss_record_partial(&buff); + let result = parse_nbss_record_partial(buff); match result { Ok((remainder, p)) => { assert_eq!(p.message_type, NBSS_MSGTYPE_SESSION_MESSAGE); diff --git a/rust/src/smb/smb.rs b/rust/src/smb/smb.rs index 02ebda70d05f..44e8b7c101fe 100644 --- a/rust/src/smb/smb.rs +++ b/rust/src/smb/smb.rs @@ -1073,7 +1073,7 @@ impl SMBState { for tx in &mut self.transactions { let found = match tx.type_data { Some(SMBTransactionTypeData::CREATE(ref _d)) => { - tx.hdr.compare(&hdr) + tx.hdr.compare(hdr) }, _ => { false }, }; @@ -1116,7 +1116,7 @@ impl SMBState { _ => { ("UNKNOWN", false) }, }; SCLogDebug!("service {} is_dcerpc {}", name, is_dcerpc); - (&name, is_dcerpc) + (name, is_dcerpc) } fn post_gap_housekeeping_for_files(&mut self) @@ -1268,12 +1268,12 @@ impl SMBState { Ok((output, ref nbss_part_hdr)) => { SCLogDebug!("parse_nbss_record_partial ok, output len {}", output.len()); if nbss_part_hdr.message_type == NBSS_MSGTYPE_SESSION_MESSAGE { - match parse_smb_version(&nbss_part_hdr.data) { + match parse_smb_version(nbss_part_hdr.data) { Ok((_, ref smb)) => { SCLogDebug!("SMB {:?}", smb); if smb.version == 0xff_u8 { // SMB1 SCLogDebug!("SMBv1 record"); - match parse_smb_record(&nbss_part_hdr.data) { + match parse_smb_record(nbss_part_hdr.data) { Ok((_, ref r)) => { if r.command == SMB1_COMMAND_WRITE_ANDX { // see if it's a write to a pipe. We only handle those @@ -1298,7 +1298,7 @@ impl SMBState { } else if smb.version == 0xfe_u8 { // SMB2 SCLogDebug!("NBSS record {:?}", nbss_part_hdr); SCLogDebug!("SMBv2 record"); - match parse_smb2_request_record(&nbss_part_hdr.data) { + match parse_smb2_request_record(nbss_part_hdr.data) { Ok((_, ref smb_record)) => { SCLogDebug!("SMB2: partial record {}", &smb2_command_string(smb_record.command)); @@ -1386,12 +1386,12 @@ impl SMBState { if nbss_hdr.message_type == NBSS_MSGTYPE_SESSION_MESSAGE { // we have the full records size worth of data, // let's parse it - match parse_smb_version(&nbss_hdr.data) { + match parse_smb_version(nbss_hdr.data) { Ok((_, ref smb)) => { SCLogDebug!("SMB {:?}", smb); if smb.version == 0xff_u8 { // SMB1 SCLogDebug!("SMBv1 record"); - match parse_smb_record(&nbss_hdr.data) { + match parse_smb_record(nbss_hdr.data) { Ok((_, ref smb_record)) => { smb1_request_record(self, smb_record); }, @@ -1404,7 +1404,7 @@ impl SMBState { let mut nbss_data = nbss_hdr.data; while nbss_data.len() > 0 { SCLogDebug!("SMBv2 record"); - match parse_smb2_request_record(&nbss_data) { + match parse_smb2_request_record(nbss_data) { Ok((nbss_data_rem, ref smb_record)) => { SCLogDebug!("nbss_data_rem {}", nbss_data_rem.len()); @@ -1421,7 +1421,7 @@ impl SMBState { let mut nbss_data = nbss_hdr.data; while nbss_data.len() > 0 { SCLogDebug!("SMBv3 transform record"); - match parse_smb3_transform_record(&nbss_data) { + match parse_smb3_transform_record(nbss_data) { Ok((nbss_data_rem, ref _smb3_record)) => { nbss_data = nbss_data_rem; }, @@ -1510,12 +1510,12 @@ impl SMBState { Ok((output, ref nbss_part_hdr)) => { SCLogDebug!("parse_nbss_record_partial ok, output len {}", output.len()); if nbss_part_hdr.message_type == NBSS_MSGTYPE_SESSION_MESSAGE { - match parse_smb_version(&nbss_part_hdr.data) { + match parse_smb_version(nbss_part_hdr.data) { Ok((_, ref smb)) => { SCLogDebug!("SMB {:?}", smb); if smb.version == 255u8 { // SMB1 SCLogDebug!("SMBv1 record"); - match parse_smb_record(&nbss_part_hdr.data) { + match parse_smb_record(nbss_part_hdr.data) { Ok((_, ref r)) => { SCLogDebug!("SMB1: partial record {}", r.command); @@ -1538,7 +1538,7 @@ impl SMBState { } } else if smb.version == 254u8 { // SMB2 SCLogDebug!("SMBv2 record"); - match parse_smb2_response_record(&nbss_part_hdr.data) { + match parse_smb2_response_record(nbss_part_hdr.data) { Ok((_, ref smb_record)) => { SCLogDebug!("SMB2: partial record {}", &smb2_command_string(smb_record.command)); @@ -1625,12 +1625,12 @@ impl SMBState { if nbss_hdr.message_type == NBSS_MSGTYPE_SESSION_MESSAGE { // we have the full records size worth of data, // let's parse it - match parse_smb_version(&nbss_hdr.data) { + match parse_smb_version(nbss_hdr.data) { Ok((_, ref smb)) => { SCLogDebug!("SMB {:?}", smb); if smb.version == 0xff_u8 { // SMB1 SCLogDebug!("SMBv1 record"); - match parse_smb_record(&nbss_hdr.data) { + match parse_smb_record(nbss_hdr.data) { Ok((_, ref smb_record)) => { smb1_response_record(self, smb_record); }, @@ -1643,7 +1643,7 @@ impl SMBState { let mut nbss_data = nbss_hdr.data; while nbss_data.len() > 0 { SCLogDebug!("SMBv2 record"); - match parse_smb2_response_record(&nbss_data) { + match parse_smb2_response_record(nbss_data) { Ok((nbss_data_rem, ref smb_record)) => { smb2_response_record(self, smb_record); nbss_data = nbss_data_rem; @@ -1658,7 +1658,7 @@ impl SMBState { let mut nbss_data = nbss_hdr.data; while nbss_data.len() > 0 { SCLogDebug!("SMBv3 transform record"); - match parse_smb3_transform_record(&nbss_data) { + match parse_smb3_transform_record(nbss_data) { Ok((nbss_data_rem, ref _smb3_record)) => { nbss_data = nbss_data_rem; }, @@ -1891,7 +1891,7 @@ pub extern "C" fn rs_smb_parse_response_tcp_gap( fn smb_probe_tcp_midstream(direction: u8, slice: &[u8], rdir: *mut u8) -> i8 { match search_smb_record(slice) { - Ok((_, ref data)) => { + Ok((_, data)) => { SCLogDebug!("smb found"); match parse_smb_version(data) { Ok((_, ref smb)) => { diff --git a/rust/src/smb/smb1.rs b/rust/src/smb/smb1.rs index 58f6aded77ab..45136f69496c 100644 --- a/rust/src/smb/smb1.rs +++ b/rust/src/smb/smb1.rs @@ -148,7 +148,7 @@ fn smb1_close_file(state: &mut SMBState, fid: &Vec) { // we can have created 2 txs for a FID: one for reads // and one for writes. So close both. - match state.get_file_tx_by_fuid(&fid, STREAM_TOSERVER) { + match state.get_file_tx_by_fuid(fid, STREAM_TOSERVER) { Some((tx, files, flags)) => { SCLogDebug!("found tx {}", tx.id); if let Some(SMBTransactionTypeData::FILE(ref mut tdf)) = tx.type_data { @@ -163,7 +163,7 @@ fn smb1_close_file(state: &mut SMBState, fid: &Vec) }, None => { }, } - match state.get_file_tx_by_fuid(&fid, STREAM_TOCLIENT) { + match state.get_file_tx_by_fuid(fid, STREAM_TOCLIENT) { Some((tx, files, flags)) => { SCLogDebug!("found tx {}", tx.id); if let Some(SMBTransactionTypeData::FILE(ref mut tdf)) = tx.type_data { @@ -634,7 +634,7 @@ fn smb1_response_record_one<'b>(state: &mut SMBState, r: &SmbRecord<'b>, command let have_tx = match command { SMB1_COMMAND_READ_ANDX => { - smb1_read_response_record(state, &r, *andx_offset); + smb1_read_response_record(state, r, *andx_offset); true // tx handling in func }, SMB1_COMMAND_NEGOTIATE_PROTOCOL => { @@ -895,7 +895,7 @@ pub fn smb1_trans_request_record<'b>(state: &mut SMBState, r: &SmbRecord<'b>) SCLogDebug!("SMBv1 TRANS TO PIPE"); let hdr = SMBCommonHdr::from1(r, SMBHDR_TYPE_HEADER); let vercmd = SMBVerCmdStat::new1(r.command); - smb_write_dcerpc_record(state, vercmd, hdr, &rd.data.data); + smb_write_dcerpc_record(state, vercmd, hdr, rd.data.data); } }, _ => { @@ -940,7 +940,7 @@ pub fn smb1_trans_response_record<'b>(state: &mut SMBState, r: &SmbRecord<'b>) SCLogDebug!("SMBv1 TRANS TO PIPE"); let hdr = SMBCommonHdr::from1(r, SMBHDR_TYPE_HEADER); let vercmd = SMBVerCmdStat::new1_with_ntstatus(r.command, r.nt_status); - smb_read_dcerpc_record(state, vercmd, hdr, &fid, &rd.data); + smb_read_dcerpc_record(state, vercmd, hdr, &fid, rd.data); } }, _ => { @@ -1004,7 +1004,7 @@ pub fn smb1_write_request_record<'b>(state: &mut SMBState, r: &SmbRecord<'b>, an SCLogDebug!("SMBv1 WRITE TO PIPE"); let hdr = SMBCommonHdr::from1(r, SMBHDR_TYPE_HEADER); let vercmd = SMBVerCmdStat::new1_with_ntstatus(command, r.nt_status); - smb_write_dcerpc_record(state, vercmd, hdr, &rd.data); + smb_write_dcerpc_record(state, vercmd, hdr, rd.data); } else { let (tx, files, flags) = state.new_file_tx(&file_fid, &file_name, STREAM_TOSERVER); if let Some(SMBTransactionTypeData::FILE(ref mut tdf)) = tx.type_data { @@ -1113,7 +1113,7 @@ pub fn smb1_read_response_record<'b>(state: &mut SMBState, r: &SmbRecord<'b>, an // hack: we store fid with ssn id mixed in, but here we want the // real thing instead. let pure_fid = if file_fid.len() > 2 { &file_fid[0..2] } else { &[] }; - smb_read_dcerpc_record(state, vercmd, hdr, &pure_fid, &rd.data); + smb_read_dcerpc_record(state, vercmd, hdr, pure_fid, rd.data); } state.set_file_left(STREAM_TOCLIENT, rd.len, rd.data.len() as u32, file_fid.to_vec()); diff --git a/rust/src/smb/smb1_records.rs b/rust/src/smb/smb1_records.rs index 760daba00d6f..ec8f1f3052c2 100644 --- a/rust/src/smb/smb1_records.rs +++ b/rust/src/smb/smb1_records.rs @@ -372,7 +372,7 @@ pub fn parse_smb_trans_request_record<'a, 'b>(i: &'a[u8], r: &SmbRecord<'b>) let res = SmbRecordTransRequest { params: params, pipe: pipe, txname: n, data: recdata, }; - Ok((&rem, res)) + Ok((rem, res)) } diff --git a/rust/src/smb/smb2.rs b/rust/src/smb/smb2.rs index c25cf0cca696..f510dead4b52 100644 --- a/rust/src/smb/smb2.rs +++ b/rust/src/smb/smb2.rs @@ -280,7 +280,7 @@ pub fn smb2_write_request_record<'b>(state: &mut SMBState, r: &Smb2Record<'b>) _ => { (Vec::new(), false) }, }; let mut is_dcerpc = if is_pipe || (share_name.len() == 0 && !is_pipe) { - match state.get_service_for_guid(&wr.guid) { + match state.get_service_for_guid(wr.guid) { (_, x) => x, } } else { @@ -498,7 +498,7 @@ pub fn smb2_request_record<'b>(state: &mut SMBState, r: &Smb2Record<'b>) } }, SMB2_COMMAND_WRITE => { - smb2_write_request_record(state, &r); + smb2_write_request_record(state, r); true // write handling creates both file tx and generic tx }, SMB2_COMMAND_CLOSE => { @@ -604,7 +604,7 @@ pub fn smb2_response_record<'b>(state: &mut SMBState, r: &Smb2Record<'b>) SMB2_COMMAND_READ => { if r.nt_status == SMB_NTSTATUS_SUCCESS || r.nt_status == SMB_NTSTATUS_BUFFER_OVERFLOW { - smb2_read_response_record(state, &r); + smb2_read_response_record(state, r); false } else if r.nt_status == SMB_NTSTATUS_END_OF_FILE { diff --git a/rust/src/smb/smb2_ioctl.rs b/rust/src/smb/smb2_ioctl.rs index 22e6de03950d..cb6bb5a9b179 100644 --- a/rust/src/smb/smb2_ioctl.rs +++ b/rust/src/smb/smb2_ioctl.rs @@ -62,7 +62,7 @@ pub fn smb2_ioctl_request_record<'b>(state: &mut SMBState, r: &Smb2Record<'b>) match parse_smb2_request_ioctl(r.data) { Ok((_, rd)) => { SCLogDebug!("IOCTL request data: {:?}", rd); - let is_dcerpc = rd.is_pipe && match state.get_service_for_guid(&rd.guid) { + let is_dcerpc = rd.is_pipe && match state.get_service_for_guid(rd.guid) { (_, x) => x, }; if is_dcerpc { @@ -90,7 +90,7 @@ pub fn smb2_ioctl_response_record<'b>(state: &mut SMBState, r: &Smb2Record<'b>) Ok((_, rd)) => { SCLogDebug!("IOCTL response data: {:?}", rd); - let is_dcerpc = rd.is_pipe && match state.get_service_for_guid(&rd.guid) { + let is_dcerpc = rd.is_pipe && match state.get_service_for_guid(rd.guid) { (_, x) => x, }; if is_dcerpc { diff --git a/rust/src/snmp/detect.rs b/rust/src/snmp/detect.rs index f6ceedf05197..3bf38add4c95 100644 --- a/rust/src/snmp/detect.rs +++ b/rust/src/snmp/detect.rs @@ -34,7 +34,7 @@ pub unsafe extern "C" fn rs_snmp_tx_get_community(tx: &mut SNMPTransaction, { match tx.community { Some(ref c) => { - *buf = (&c).as_ptr(); + *buf = c.as_ptr(); *len = c.len() as u32; }, None => () diff --git a/rust/src/snmp/snmp.rs b/rust/src/snmp/snmp.rs index 608c83356ae9..f4d2e814dbf5 100644 --- a/rust/src/snmp/snmp.rs +++ b/rust/src/snmp/snmp.rs @@ -218,7 +218,7 @@ impl<'a> SNMPState<'a> { } fn free_tx(&mut self, tx_id: u64) { - let tx = self.transactions.iter().position(|ref tx| tx.id == tx_id + 1); + let tx = self.transactions.iter().position(|tx| tx.id == tx_id + 1); debug_assert!(tx != None); if let Some(idx) = tx { let _ = self.transactions.remove(idx); diff --git a/rust/src/tftp/tftp.rs b/rust/src/tftp/tftp.rs index 8b62f2eae62d..e64d4843b94c 100644 --- a/rust/src/tftp/tftp.rs +++ b/rust/src/tftp/tftp.rs @@ -52,7 +52,7 @@ impl TFTPState { } fn free_tx(&mut self, tx_id: u64) { - let tx = self.transactions.iter().position(|ref tx| tx.id == tx_id + 1); + let tx = self.transactions.iter().position(|tx| tx.id == tx_id + 1); debug_assert!(tx != None); if let Some(idx) = tx { let _ = self.transactions.remove(idx); From 602bb05e75382933359b052e60195a54c1f450a4 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Fri, 20 Aug 2021 10:51:46 -0600 Subject: [PATCH 13/24] rust(lint): fix redundant closures This lint checks for a closure where a function can be directly supplied. Runtime performance is unchanged, but this makes less work for the compiler. --- rust/src/kerberos.rs | 4 ++-- rust/src/smb/auth.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/rust/src/kerberos.rs b/rust/src/kerberos.rs index 06bfda75dc49..69e04662c266 100644 --- a/rust/src/kerberos.rs +++ b/rust/src/kerberos.rs @@ -56,7 +56,7 @@ pub struct Kerberos5Ticket { fn parse_kerberos5_request_do(blob: &[u8]) -> IResult<&[u8], ApReq, SecBlobError> { - let (_,b) = der_parser::parse_der(blob).map_err(|e| nom::Err::convert(e))?; + let (_,b) = der_parser::parse_der(blob).map_err(nom::Err::convert)?; let blob = b.as_slice().or( Err(nom::Err::Error(SecBlobError::KrbFmtError)) )?; @@ -71,7 +71,7 @@ fn parse_kerberos5_request_do(blob: &[u8]) -> IResult<&[u8], ApReq, SecBlobError ap_req }) ) - .map_err(|e| nom::Err::convert(e)) + .map_err(nom::Err::convert) } pub fn parse_kerberos5_request(blob: &[u8]) -> IResult<&[u8], Kerberos5Ticket, SecBlobError> diff --git a/rust/src/smb/auth.rs b/rust/src/smb/auth.rs index f90ab0ae61a7..bd15ca666e3b 100644 --- a/rust/src/smb/auth.rs +++ b/rust/src/smb/auth.rs @@ -27,13 +27,13 @@ use der_parser::der::{parse_der_oid, parse_der_sequence}; fn parse_secblob_get_spnego(blob: &[u8]) -> IResult<&[u8], &[u8], SecBlobError> { - let (rem, base_o) = der_parser::parse_der(blob).map_err(|e| nom::Err::convert(e))?; + let (rem, base_o) = der_parser::parse_der(blob).map_err(nom::Err::convert)?; SCLogDebug!("parse_secblob_get_spnego: base_o {:?}", base_o); let d = match base_o.content.as_slice() { Err(_) => { return Err(nom::Err::Error(SecBlobError::NotSpNego)); }, Ok(d) => d, }; - let (next, o) = parse_der_oid(d).map_err(|e| nom::Err::convert(e))?; + let (next, o) = parse_der_oid(d).map_err(nom::Err::convert)?; SCLogDebug!("parse_secblob_get_spnego: sub_o {:?}", o); let oid = match o.content.as_oid() { @@ -60,7 +60,7 @@ fn parse_secblob_get_spnego(blob: &[u8]) -> IResult<&[u8], &[u8], SecBlobError> fn parse_secblob_spnego_start(blob: &[u8]) -> IResult<&[u8], &[u8], SecBlobError> { - let (rem, o) = der_parser::parse_der(blob).map_err(|e| nom::Err::convert(e))?; + let (rem, o) = der_parser::parse_der(blob).map_err(nom::Err::convert)?; let d = match o.content.as_slice() { Ok(d) => { SCLogDebug!("d: next data len {}",d.len()); From 5bf5de335065ffd45507f08312cdb69ab190d004 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Fri, 20 Aug 2021 10:59:41 -0600 Subject: [PATCH 14/24] rust(lint): don't use unwrap_or for function calls Calling a function in unwrap_or causes that function to always be called even when not needed. Instead use unwrap_or_else with a closure which will only be called when needed. --- rust/src/dcerpc/dcerpc.rs | 2 +- rust/src/smb/smb1_records.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/src/dcerpc/dcerpc.rs b/rust/src/dcerpc/dcerpc.rs index 3f00e8312b9b..45b68a66d0e7 100644 --- a/rust/src/dcerpc/dcerpc.rs +++ b/rust/src/dcerpc/dcerpc.rs @@ -2149,7 +2149,7 @@ mod tests { .zip(expected_uuid) .map(|(x, y)| x.cmp(y)) .find(|&ord| ord != cmp::Ordering::Equal) - .unwrap_or(bind_uuid.len().cmp(&expected_uuid.len())) + .unwrap_or_else(|| bind_uuid.len().cmp(&expected_uuid.len())) ); } } diff --git a/rust/src/smb/smb1_records.rs b/rust/src/smb/smb1_records.rs index ec8f1f3052c2..5bc886532b37 100644 --- a/rust/src/smb/smb1_records.rs +++ b/rust/src/smb/smb1_records.rs @@ -582,7 +582,7 @@ pub fn parse_smb_create_andx_request_record<'a>(i: &'a[u8], r: &SmbRecord) >> (SmbRequestCreateAndXRecord { disposition: disposition, create_options: create_options, - file_name: file_name.unwrap_or(Vec::new()), + file_name: file_name.unwrap_or_default(), })) } From 8bb6dab69d73e8befdaa9606b7b6b31e0f187abb Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Fri, 20 Aug 2021 11:03:45 -0600 Subject: [PATCH 15/24] rust(lint): remove useless format calls In these simple cases to_string() is recommended and likely performs better as the formatter is not called. --- rust/src/ike/logger.rs | 2 +- rust/src/krb/log.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/src/ike/logger.rs b/rust/src/ike/logger.rs index b5ee8fdb3a80..0a1263b15025 100644 --- a/rust/src/ike/logger.rs +++ b/rust/src/ike/logger.rs @@ -29,7 +29,7 @@ fn add_attributes(transform: &Vec, js: &mut JsonBuilder) -> Result< for attribute in transform { js.set_string( attribute.attribute_type.to_string().as_str(), - format!("{}", attribute.attribute_value.to_string()).as_str(), + attribute.attribute_value.to_string().as_str(), )?; if let Some(numeric_value) = attribute.numeric_value { diff --git a/rust/src/krb/log.rs b/rust/src/krb/log.rs index d0e654b30038..e20f36e9b5b3 100644 --- a/rust/src/krb/log.rs +++ b/rust/src/krb/log.rs @@ -35,7 +35,7 @@ fn krb5_log_response(jsb: &mut JsonBuilder, tx: &mut KRB5Transaction) -> Result< None => "".to_owned(), }; let realm = match tx.realm { - Some(ref x) => format!("{}", x.0), + Some(ref x) => x.0.to_string(), None => "".to_owned(), }; let sname = match tx.sname { From ac3a20b6e06bf02a850be40385d6d624bcec439d Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Fri, 20 Aug 2021 11:11:23 -0600 Subject: [PATCH 16/24] rust(lint): remove useless conversions and clones These add complexity and may not be optimized out by the compiler. --- rust/src/applayertemplate/template.rs | 2 +- rust/src/modbus/detect.rs | 62 +++++++++++++-------------- rust/src/mqtt/mqtt.rs | 4 +- rust/src/snmp/snmp.rs | 6 +-- 4 files changed, 35 insertions(+), 39 deletions(-) diff --git a/rust/src/applayertemplate/template.rs b/rust/src/applayertemplate/template.rs index 25482cc4d410..8524061b23ac 100644 --- a/rust/src/applayertemplate/template.rs +++ b/rust/src/applayertemplate/template.rs @@ -372,7 +372,7 @@ pub unsafe extern "C" fn rs_template_parse_response( AppLayerResult::ok() } else { let buf = build_slice!(input, input_len as usize); - state.parse_response(buf).into() + state.parse_response(buf) } } diff --git a/rust/src/modbus/detect.rs b/rust/src/modbus/detect.rs index 44d873161229..50ff91bad45e 100644 --- a/rust/src/modbus/detect.rs +++ b/rust/src/modbus/detect.rs @@ -522,7 +522,7 @@ mod test { assert_eq!( parse_access("access read discretes"), Ok(DetectModbusRust { - access_type: Some(Flags::from(AccessType::READ | AccessType::DISCRETES)), + access_type: Some((AccessType::READ | AccessType::DISCRETES)), ..Default::default() }) ); @@ -537,7 +537,7 @@ mod test { assert_eq!( parse_access("access write coils, address <500"), Ok(DetectModbusRust { - access_type: Some(Flags::from(AccessType::WRITE | AccessType::COILS)), + access_type: Some((AccessType::WRITE | AccessType::COILS)), address: Some(std::u16::MIN..500), ..Default::default() }) @@ -545,7 +545,7 @@ mod test { assert_eq!( parse_access("access write coils, address >500"), Ok(DetectModbusRust { - access_type: Some(Flags::from(AccessType::WRITE | AccessType::COILS)), + access_type: Some((AccessType::WRITE | AccessType::COILS)), address: Some(500..std::u16::MAX), ..Default::default() }) @@ -553,7 +553,7 @@ mod test { assert_eq!( parse_access("access write holding, address 100, value <1000"), Ok(DetectModbusRust { - access_type: Some(Flags::from(AccessType::WRITE | AccessType::HOLDING)), + access_type: Some((AccessType::WRITE | AccessType::HOLDING)), address: Some(100..100), value: Some(std::u16::MIN..1000), ..Default::default() @@ -562,7 +562,7 @@ mod test { assert_eq!( parse_access("access write holding, address 100, value 500<>1000"), Ok(DetectModbusRust { - access_type: Some(Flags::from(AccessType::WRITE | AccessType::HOLDING)), + access_type: Some((AccessType::WRITE | AccessType::HOLDING)), address: Some(100..100), value: Some(500..1000), ..Default::default() @@ -661,7 +661,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::WRITE | AccessType::HOLDING)), + access_type: Some((AccessType::WRITE | AccessType::HOLDING)), address: Some(15..15), value: Some(std::u16::MIN..4660), ..Default::default() @@ -674,7 +674,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::WRITE | AccessType::HOLDING)), + access_type: Some((AccessType::WRITE | AccessType::HOLDING)), address: Some(15..15), value: Some(4661..4661), ..Default::default() @@ -687,7 +687,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::WRITE | AccessType::HOLDING)), + access_type: Some((AccessType::WRITE | AccessType::HOLDING)), address: Some(16..16), value: Some(20000..22136), ..Default::default() @@ -700,7 +700,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::WRITE | AccessType::HOLDING)), + access_type: Some((AccessType::WRITE | AccessType::HOLDING)), address: Some(16..16), value: Some(22136..30000), ..Default::default() @@ -713,7 +713,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::WRITE | AccessType::HOLDING)), + access_type: Some((AccessType::WRITE | AccessType::HOLDING)), address: Some(15..15), value: Some(4660..std::u16::MAX), ..Default::default() @@ -726,7 +726,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::WRITE | AccessType::HOLDING)), + access_type: Some((AccessType::WRITE | AccessType::HOLDING)), address: Some(16..16), value: Some(std::u16::MIN..22137), ..Default::default() @@ -739,7 +739,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::WRITE | AccessType::HOLDING)), + access_type: Some((AccessType::WRITE | AccessType::HOLDING)), address: Some(16..16), value: Some(std::u16::MIN..22137), ..Default::default() @@ -752,7 +752,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::WRITE | AccessType::HOLDING)), + access_type: Some((AccessType::WRITE | AccessType::HOLDING)), address: Some(17..17), value: Some(39612..39612), ..Default::default() @@ -765,7 +765,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::WRITE | AccessType::HOLDING)), + access_type: Some((AccessType::WRITE | AccessType::HOLDING)), address: Some(17..17), value: Some(30000..39613), ..Default::default() @@ -778,7 +778,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::WRITE | AccessType::HOLDING)), + access_type: Some((AccessType::WRITE | AccessType::HOLDING)), address: Some(15..15), value: Some(4659..5000), ..Default::default() @@ -791,7 +791,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::WRITE | AccessType::HOLDING)), + access_type: Some((AccessType::WRITE | AccessType::HOLDING)), address: Some(17..17), value: Some(39611..std::u16::MAX), ..Default::default() @@ -939,9 +939,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - category: Some(Flags::from( - CodeCategory::PUBLIC_ASSIGNED | CodeCategory::PUBLIC_UNASSIGNED - )), + category: Some((CodeCategory::PUBLIC_ASSIGNED | CodeCategory::PUBLIC_UNASSIGNED)), unit_id: Some(11..11), ..Default::default() } @@ -977,9 +975,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - category: Some(Flags::from( - CodeCategory::PUBLIC_ASSIGNED | CodeCategory::PUBLIC_UNASSIGNED - )), + category: Some((CodeCategory::PUBLIC_ASSIGNED | CodeCategory::PUBLIC_UNASSIGNED)), unit_id: Some(10..10), ..Default::default() } @@ -1212,7 +1208,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::READ | AccessType::INPUT)), + access_type: Some((AccessType::READ | AccessType::INPUT)), ..Default::default() } ), @@ -1223,7 +1219,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::READ | AccessType::INPUT)), + access_type: Some((AccessType::READ | AccessType::INPUT)), address: Some(std::u16::MIN..9), ..Default::default() } @@ -1235,7 +1231,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::READ | AccessType::INPUT)), + access_type: Some((AccessType::READ | AccessType::INPUT)), address: Some(5..9), ..Default::default() } @@ -1247,7 +1243,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::READ | AccessType::INPUT)), + access_type: Some((AccessType::READ | AccessType::INPUT)), address: Some(104..std::u16::MAX), ..Default::default() } @@ -1259,7 +1255,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::READ | AccessType::INPUT)), + access_type: Some((AccessType::READ | AccessType::INPUT)), address: Some(104..110), ..Default::default() } @@ -1271,7 +1267,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::READ | AccessType::INPUT)), + access_type: Some((AccessType::READ | AccessType::INPUT)), address: Some(9..9), ..Default::default() } @@ -1283,7 +1279,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::READ | AccessType::INPUT)), + access_type: Some((AccessType::READ | AccessType::INPUT)), address: Some(std::u16::MIN..10), ..Default::default() } @@ -1295,7 +1291,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::READ | AccessType::INPUT)), + access_type: Some((AccessType::READ | AccessType::INPUT)), address: Some(5..10), ..Default::default() } @@ -1307,7 +1303,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::READ | AccessType::INPUT)), + access_type: Some((AccessType::READ | AccessType::INPUT)), address: Some(103..std::u16::MAX), ..Default::default() } @@ -1319,7 +1315,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::READ | AccessType::INPUT)), + access_type: Some((AccessType::READ | AccessType::INPUT)), address: Some(103..110), ..Default::default() } @@ -1331,7 +1327,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some(Flags::from(AccessType::READ | AccessType::INPUT)), + access_type: Some((AccessType::READ | AccessType::INPUT)), address: Some(104..104), ..Default::default() } diff --git a/rust/src/mqtt/mqtt.rs b/rust/src/mqtt/mqtt.rs index 0f3d62babdfd..7403245f2ac0 100644 --- a/rust/src/mqtt/mqtt.rs +++ b/rust/src/mqtt/mqtt.rs @@ -606,7 +606,7 @@ pub unsafe extern "C" fn rs_mqtt_parse_request( ) -> AppLayerResult { let state = cast_pointer!(state, MQTTState); let buf = build_slice!(input, input_len as usize); - return state.parse_request(buf).into(); + return state.parse_request(buf); } #[no_mangle] @@ -621,7 +621,7 @@ pub unsafe extern "C" fn rs_mqtt_parse_response( ) -> AppLayerResult { let state = cast_pointer!(state, MQTTState); let buf = build_slice!(input, input_len as usize); - return state.parse_response(buf).into(); + return state.parse_response(buf); } #[no_mangle] diff --git a/rust/src/snmp/snmp.rs b/rust/src/snmp/snmp.rs index f4d2e814dbf5..aee0b373e0ee 100644 --- a/rust/src/snmp/snmp.rs +++ b/rust/src/snmp/snmp.rs @@ -131,7 +131,7 @@ impl<'a> SNMPState<'a> { SnmpPdu::Bulk(_) => { }, SnmpPdu::TrapV1(ref t) => { - pdu_info.trap_type = Some((t.generic_trap,t.enterprise.clone(),t.agent_addr.clone())); + pdu_info.trap_type = Some((t.generic_trap,t.enterprise.clone(),t.agent_addr)); } } @@ -149,7 +149,7 @@ impl<'a> SNMPState<'a> { self.set_event_tx(&mut tx, SNMPEvent::VersionMismatch); } self.add_pdu_info(&msg.pdu, &mut tx); - tx.community = Some(msg.community.clone()); + tx.community = Some(msg.community); self.transactions.push(tx); 0 } @@ -170,7 +170,7 @@ impl<'a> SNMPState<'a> { } match msg.security_params { SecurityParameters::USM(usm) => { - tx.usm = Some(usm.msg_user_name.clone()); + tx.usm = Some(usm.msg_user_name); }, _ => { self.set_event_tx(&mut tx, SNMPEvent::UnknownSecurityModel); From 4abbfd0d9718eb55cea92ebada620cad7ff8e5a5 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Fri, 20 Aug 2021 11:17:59 -0600 Subject: [PATCH 17/24] rust(lint): remove extra parens around bitwise or This is a readability fix, as on first look they almost look like a Rust tuple. --- rust/src/modbus/detect.rs | 58 +++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/rust/src/modbus/detect.rs b/rust/src/modbus/detect.rs index 50ff91bad45e..ab9888261a67 100644 --- a/rust/src/modbus/detect.rs +++ b/rust/src/modbus/detect.rs @@ -522,7 +522,7 @@ mod test { assert_eq!( parse_access("access read discretes"), Ok(DetectModbusRust { - access_type: Some((AccessType::READ | AccessType::DISCRETES)), + access_type: Some(AccessType::READ | AccessType::DISCRETES), ..Default::default() }) ); @@ -537,7 +537,7 @@ mod test { assert_eq!( parse_access("access write coils, address <500"), Ok(DetectModbusRust { - access_type: Some((AccessType::WRITE | AccessType::COILS)), + access_type: Some(AccessType::WRITE | AccessType::COILS), address: Some(std::u16::MIN..500), ..Default::default() }) @@ -545,7 +545,7 @@ mod test { assert_eq!( parse_access("access write coils, address >500"), Ok(DetectModbusRust { - access_type: Some((AccessType::WRITE | AccessType::COILS)), + access_type: Some(AccessType::WRITE | AccessType::COILS), address: Some(500..std::u16::MAX), ..Default::default() }) @@ -553,7 +553,7 @@ mod test { assert_eq!( parse_access("access write holding, address 100, value <1000"), Ok(DetectModbusRust { - access_type: Some((AccessType::WRITE | AccessType::HOLDING)), + access_type: Some(AccessType::WRITE | AccessType::HOLDING), address: Some(100..100), value: Some(std::u16::MIN..1000), ..Default::default() @@ -562,7 +562,7 @@ mod test { assert_eq!( parse_access("access write holding, address 100, value 500<>1000"), Ok(DetectModbusRust { - access_type: Some((AccessType::WRITE | AccessType::HOLDING)), + access_type: Some(AccessType::WRITE | AccessType::HOLDING), address: Some(100..100), value: Some(500..1000), ..Default::default() @@ -661,7 +661,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some((AccessType::WRITE | AccessType::HOLDING)), + access_type: Some(AccessType::WRITE | AccessType::HOLDING), address: Some(15..15), value: Some(std::u16::MIN..4660), ..Default::default() @@ -674,7 +674,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some((AccessType::WRITE | AccessType::HOLDING)), + access_type: Some(AccessType::WRITE | AccessType::HOLDING), address: Some(15..15), value: Some(4661..4661), ..Default::default() @@ -687,7 +687,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some((AccessType::WRITE | AccessType::HOLDING)), + access_type: Some(AccessType::WRITE | AccessType::HOLDING), address: Some(16..16), value: Some(20000..22136), ..Default::default() @@ -700,7 +700,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some((AccessType::WRITE | AccessType::HOLDING)), + access_type: Some(AccessType::WRITE | AccessType::HOLDING), address: Some(16..16), value: Some(22136..30000), ..Default::default() @@ -713,7 +713,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some((AccessType::WRITE | AccessType::HOLDING)), + access_type: Some(AccessType::WRITE | AccessType::HOLDING), address: Some(15..15), value: Some(4660..std::u16::MAX), ..Default::default() @@ -726,7 +726,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some((AccessType::WRITE | AccessType::HOLDING)), + access_type: Some(AccessType::WRITE | AccessType::HOLDING), address: Some(16..16), value: Some(std::u16::MIN..22137), ..Default::default() @@ -739,7 +739,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some((AccessType::WRITE | AccessType::HOLDING)), + access_type: Some(AccessType::WRITE | AccessType::HOLDING), address: Some(16..16), value: Some(std::u16::MIN..22137), ..Default::default() @@ -752,7 +752,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some((AccessType::WRITE | AccessType::HOLDING)), + access_type: Some(AccessType::WRITE | AccessType::HOLDING), address: Some(17..17), value: Some(39612..39612), ..Default::default() @@ -765,7 +765,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some((AccessType::WRITE | AccessType::HOLDING)), + access_type: Some(AccessType::WRITE | AccessType::HOLDING), address: Some(17..17), value: Some(30000..39613), ..Default::default() @@ -778,7 +778,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some((AccessType::WRITE | AccessType::HOLDING)), + access_type: Some(AccessType::WRITE | AccessType::HOLDING), address: Some(15..15), value: Some(4659..5000), ..Default::default() @@ -791,7 +791,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - access_type: Some((AccessType::WRITE | AccessType::HOLDING)), + access_type: Some(AccessType::WRITE | AccessType::HOLDING), address: Some(17..17), value: Some(39611..std::u16::MAX), ..Default::default() @@ -939,7 +939,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - category: Some((CodeCategory::PUBLIC_ASSIGNED | CodeCategory::PUBLIC_UNASSIGNED)), + category: Some(CodeCategory::PUBLIC_ASSIGNED | CodeCategory::PUBLIC_UNASSIGNED), unit_id: Some(11..11), ..Default::default() } @@ -975,7 +975,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[0], &DetectModbusRust { - category: Some((CodeCategory::PUBLIC_ASSIGNED | CodeCategory::PUBLIC_UNASSIGNED)), + category: Some(CodeCategory::PUBLIC_ASSIGNED | CodeCategory::PUBLIC_UNASSIGNED), unit_id: Some(10..10), ..Default::default() } @@ -1208,7 +1208,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some((AccessType::READ | AccessType::INPUT)), + access_type: Some(AccessType::READ | AccessType::INPUT), ..Default::default() } ), @@ -1219,7 +1219,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some((AccessType::READ | AccessType::INPUT)), + access_type: Some(AccessType::READ | AccessType::INPUT), address: Some(std::u16::MIN..9), ..Default::default() } @@ -1231,7 +1231,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some((AccessType::READ | AccessType::INPUT)), + access_type: Some(AccessType::READ | AccessType::INPUT), address: Some(5..9), ..Default::default() } @@ -1243,7 +1243,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some((AccessType::READ | AccessType::INPUT)), + access_type: Some(AccessType::READ | AccessType::INPUT), address: Some(104..std::u16::MAX), ..Default::default() } @@ -1255,7 +1255,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some((AccessType::READ | AccessType::INPUT)), + access_type: Some(AccessType::READ | AccessType::INPUT), address: Some(104..110), ..Default::default() } @@ -1267,7 +1267,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some((AccessType::READ | AccessType::INPUT)), + access_type: Some(AccessType::READ | AccessType::INPUT), address: Some(9..9), ..Default::default() } @@ -1279,7 +1279,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some((AccessType::READ | AccessType::INPUT)), + access_type: Some(AccessType::READ | AccessType::INPUT), address: Some(std::u16::MIN..10), ..Default::default() } @@ -1291,7 +1291,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some((AccessType::READ | AccessType::INPUT)), + access_type: Some(AccessType::READ | AccessType::INPUT), address: Some(5..10), ..Default::default() } @@ -1303,7 +1303,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some((AccessType::READ | AccessType::INPUT)), + access_type: Some(AccessType::READ | AccessType::INPUT), address: Some(103..std::u16::MAX), ..Default::default() } @@ -1315,7 +1315,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some((AccessType::READ | AccessType::INPUT)), + access_type: Some(AccessType::READ | AccessType::INPUT), address: Some(103..110), ..Default::default() } @@ -1327,7 +1327,7 @@ mod test { rs_modbus_inspect( &modbus.transactions[5], &DetectModbusRust { - access_type: Some((AccessType::READ | AccessType::INPUT)), + access_type: Some(AccessType::READ | AccessType::INPUT), address: Some(104..104), ..Default::default() } From d0be7541e9b8671dae34898912eab34873460980 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Fri, 20 Aug 2021 11:20:49 -0600 Subject: [PATCH 18/24] rust(lint): removed unused unit () return This is code that is not needed and is a bit confusing to see. --- rust/src/ike/ikev2.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/rust/src/ike/ikev2.rs b/rust/src/ike/ikev2.rs index c6e4d24f7cb8..6a597c9e8e2c 100644 --- a/rust/src/ike/ikev2.rs +++ b/rust/src/ike/ikev2.rs @@ -209,7 +209,6 @@ pub fn handle_ikev2( } _e => { SCLogDebug!("parse_ikev2_payload_with_type: {:?}", _e); - () } } return AppLayerResult::ok(); @@ -256,7 +255,6 @@ fn add_proposals(state: &mut IKEState, prop: &Vec, direction: u8) IkeTransformAuthType::NONE => { // Note: this could be expected with an AEAD encription alg. // See rule 4 - () } IkeTransformAuthType::AUTH_HMAC_MD5_96 | IkeTransformAuthType::AUTH_HMAC_SHA1_96 From d0772e04b1483c342b3f639ef11309f342f8145b Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Fri, 20 Aug 2021 11:29:37 -0600 Subject: [PATCH 19/24] rust(lint): replace checked_mul with saturating_mul When defaulting checked_mul to u64::max, Rust has a method that does the same thing called saturating_mul. --- rust/src/asn1/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/src/asn1/mod.rs b/rust/src/asn1/mod.rs index e4086b628fab..7d33bc0a9bb8 100644 --- a/rust/src/asn1/mod.rs +++ b/rust/src/asn1/mod.rs @@ -102,7 +102,7 @@ impl<'a> Asn1<'a> { { if let BerObjectContent::BitString(bits, _v) = &obj.content { if obj.header.len > 0 - && *bits as u64 > (obj.header.len.checked_mul(8).unwrap_or(std::u64::MAX)) + && *bits as u64 > obj.header.len.saturating_mul(8) { return Some(Asn1Check::BitstringOverflow); } From d5c0962299e6a2765dd217ccc23a076fb37be9e4 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Fri, 20 Aug 2021 11:31:03 -0600 Subject: [PATCH 20/24] rust(lint): fix some usages of references - ref is discouraged for top level variables - the other borrow is not required --- rust/src/dns/log.rs | 2 +- rust/src/rdp/util.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/src/dns/log.rs b/rust/src/dns/log.rs index 22ffefa5218d..5c379ff1405b 100644 --- a/rust/src/dns/log.rs +++ b/rust/src/dns/log.rs @@ -742,7 +742,7 @@ fn dns_log_json_failure_v1(r: &DNSResponse, index: usize, flags: u64) return Ok(None); } - let ref query = r.queries[index]; + let query = &r.queries[index]; if !dns_log_rrtype_enabled(query.rrtype, flags) { return Ok(None); diff --git a/rust/src/rdp/util.rs b/rust/src/rdp/util.rs index 520c3c199d3a..dfcb8267d25c 100644 --- a/rust/src/rdp/util.rs +++ b/rust/src/rdp/util.rs @@ -53,7 +53,7 @@ pub fn le_slice_to_string(input: &[u8]) -> Result Result> { let s = match memchr(b'\0', input) { Some(end) => &input[..end], - None => &input[..], + None => input, }; match std::str::from_utf8(s) { Ok(s) => Ok(String::from(s)), From dcf57ecd961e143299a60b23d150992b49d93539 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Fri, 20 Aug 2021 11:31:48 -0600 Subject: [PATCH 21/24] rust(lint): replace push_str of single char to push() --- rust/src/rdp/log.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/src/rdp/log.rs b/rust/src/rdp/log.rs index 14d75b86a793..f8f08f5c38d3 100644 --- a/rust/src/rdp/log.rs +++ b/rust/src/rdp/log.rs @@ -381,8 +381,8 @@ fn mcs_req_to_json(mcs: &McsConnectRequest, js: &mut JsonBuilder) -> Result<(), fn version_to_string<'a>(ver: &RdpClientVersion, prefix: &'a str) -> String { let mut result = String::from(prefix); match ver { - RdpClientVersion::V4 => result.push_str("4"), - RdpClientVersion::V5_V8_1 => result.push_str("5"), + RdpClientVersion::V4 => result.push('4'), + RdpClientVersion::V5_V8_1 => result.push('5'), RdpClientVersion::V10_0 => result.push_str("10.0"), RdpClientVersion::V10_1 => result.push_str("10.1"), RdpClientVersion::V10_2 => result.push_str("10.2"), From b021726a0d3c5b6c3c2726ef61359dca35c37fdf Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Fri, 20 Aug 2021 11:38:31 -0600 Subject: [PATCH 22/24] rust(lint): map the error instead of using or_else This is the preffered style and easier to understand the meaning of the code. --- rust/src/dns/detect.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/src/dns/detect.rs b/rust/src/dns/detect.rs index 326e9c38a714..b389d2c57f11 100644 --- a/rust/src/dns/detect.rs +++ b/rust/src/dns/detect.rs @@ -42,7 +42,7 @@ fn parse_opcode(opcode: &str) -> Result { negated = true; } _ => { - let code: u8 = (&opcode[i..]).parse().or_else(|_| Err(()))?; + let code: u8 = (&opcode[i..]).parse().map_err(|_| ())?; return Ok(DetectDnsOpcode { negate: negated, opcode: code, From 91402f9fbae8b199298a761353eec2389d8f4953 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Fri, 20 Aug 2021 11:41:40 -0600 Subject: [PATCH 23/24] rust(lint): remove manual implement of map method Using `if let` expressions in these cases is better expressed by the map method, and considered idiomatic Rust for this usage. --- rust/src/ike/parser.rs | 13 ++----------- rust/src/rdp/parser.rs | 35 +++++++---------------------------- 2 files changed, 9 insertions(+), 39 deletions(-) diff --git a/rust/src/ike/parser.rs b/rust/src/ike/parser.rs index f3fe86fae325..f9cfa0831a2e 100644 --- a/rust/src/ike/parser.rs +++ b/rust/src/ike/parser.rs @@ -479,22 +479,13 @@ named! { pub parse_sa_attribute<&[u8], Vec>, numeric_value: match format.0 { 1 => Some(attribute_length_or_value as u32), 0 => { - if let Some(_numeric_variable_value) = numeric_variable_value { - Some(_numeric_variable_value) - } - else { - None - } + numeric_variable_value }, _ => None, }, hex_value: match format.0 { 0 => { - if let Some(_variable_attribute_value) = variable_attribute_value { - Some(to_hex(_variable_attribute_value)) - } else { - None - } + variable_attribute_value.map(|_variable_attribute_value| to_hex(_variable_attribute_value)) } _ => None, } diff --git a/rust/src/rdp/parser.rs b/rust/src/rdp/parser.rs index f1b27cce790c..bcd25211b30e 100644 --- a/rust/src/rdp/parser.rs +++ b/rust/src/rdp/parser.rs @@ -441,10 +441,7 @@ pub fn parse_t123_tpkt(input: &[u8]) -> IResult<&[u8], T123Tpkt, RdpError> { let opt1: Option = { match opt!(data, parse_x224_connection_request_class_0) { - Ok((_remainder, opt)) => match opt { - Some(x) => Some(T123TpktChild::X224ConnectionRequest(x)), - None => None, - }, + Ok((_remainder, opt)) => opt.map(T123TpktChild::X224ConnectionRequest), Err(e) => return Err(e), } }; @@ -452,10 +449,7 @@ pub fn parse_t123_tpkt(input: &[u8]) -> IResult<&[u8], T123Tpkt, RdpError> { let opt2: Option = match opt1 { Some(x) => Some(x), None => match opt!(data, parse_x224_connection_confirm_class_0) { - Ok((_remainder, opt)) => match opt { - Some(x) => Some(T123TpktChild::X224ConnectionConfirm(x)), - None => None, - }, + Ok((_remainder, opt)) => opt.map(T123TpktChild::X224ConnectionConfirm), Err(e) => return Err(e), }, }; @@ -463,10 +457,7 @@ pub fn parse_t123_tpkt(input: &[u8]) -> IResult<&[u8], T123Tpkt, RdpError> { let opt3: Option = match opt2 { Some(x) => Some(x), None => match opt!(data, parse_x223_data_class_0) { - Ok((_remainder, opt)) => match opt { - Some(x) => Some(T123TpktChild::Data(x)), - None => None, - }, + Ok((_remainder, opt)) => opt.map(T123TpktChild::Data), Err(e) => return Err(e), }, }; @@ -628,19 +619,13 @@ fn parse_x224_connection_confirm(input: &[u8]) -> IResult<&[u8], X224ConnectionC // it will be one of a response message or a failure message let opt1: Option = match opt!(data, parse_negotiation_response) { - Ok((_remainder, opt)) => match opt { - Some(x) => Some(NegotiationFromServer::Response(x)), - None => None, - }, + Ok((_remainder, opt)) => opt.map(NegotiationFromServer::Response), Err(e) => return Err(e), }; let opt2: Option = match opt1 { Some(x) => Some(x), None => match opt!(data, parse_negotiation_failure) { - Ok((_remainder, opt)) => match opt { - Some(x) => Some(NegotiationFromServer::Failure(x)), - None => None, - }, + Ok((_remainder, opt)) => opt.map(NegotiationFromServer::Failure), Err(e) => return Err(e), }, }; @@ -738,20 +723,14 @@ fn parse_x223_data_class_0(input: &[u8]) -> IResult<&[u8], X223Data, RdpError> { // let opt1: Option = match opt!(i3, parse_mcs_connect) { - Ok((_remainder, opt)) => match opt { - Some(x) => Some(X223DataChild::McsConnectRequest(x)), - None => None, - }, + Ok((_remainder, opt)) => opt.map(X223DataChild::McsConnectRequest), Err(e) => return Err(e), }; let opt2: Option = match opt1 { Some(x) => Some(x), None => match opt!(i3, parse_mcs_connect_response) { - Ok((_remainder, opt)) => match opt { - Some(x) => Some(X223DataChild::McsConnectResponse(x)), - None => None, - }, + Ok((_remainder, opt)) => opt.map(X223DataChild::McsConnectResponse), Err(e) => return Err(e), }, }; From cf21694ba6d535d435cfcaefcbb88517bcd4eac4 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Fri, 20 Aug 2021 11:44:17 -0600 Subject: [PATCH 24/24] rust(lint): suppress clippy lints that we should fix Suppress all remaining clippy lints that we trip. This can be fixed on a per-lint basis. --- rust/src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/rust/src/lib.rs b/rust/src/lib.rs index e563d8c9cd42..f27f76b94dd3 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -23,6 +23,50 @@ #![allow(clippy::needless_return)] #![allow(clippy::redundant_field_names)] #![allow(clippy::len_zero)] +#![allow(clippy::too_many_arguments)] +#![allow(clippy::assign_op_pattern)] +#![allow(clippy::manual_range_contains)] +#![allow(clippy::vec_init_then_push)] +#![allow(clippy::assertions_on_constants)] +#![allow(clippy::let_and_return)] +#![allow(clippy::needless_bool)] +#![allow(clippy::char_lit_as_u8)] + +// To be fixed, but remove the noise for now. +#![allow(clippy::collapsible_if)] +#![allow(clippy::collapsible_else_if)] +#![allow(clippy::unnecessary_cast)] +#![allow(clippy::redundant_static_lifetimes)] +#![allow(clippy::bool_comparison)] +#![allow(clippy::for_loops_over_fallibles)] +#![allow(clippy::needless_lifetimes)] +#![allow(clippy::single_match)] +#![allow(clippy::cmp_null)] +#![allow(clippy::upper_case_acronyms)] +#![allow(clippy::ptr_arg)] +#![allow(clippy::new_without_default)] +#![allow(clippy::needless_lifetimes)] +#![allow(clippy::match_ref_pats)] +#![allow(clippy::module_inception)] +#![allow(clippy::needless_range_loop)] +#![allow(clippy::enum_variant_names)] +#![allow(clippy::if_same_then_else)] +#![allow(clippy::match_single_binding)] +#![allow(clippy::match_like_matches_macro)] +#![allow(clippy::extra_unused_lifetimes)] +#![allow(clippy::mixed_case_hex_literals)] +#![allow(clippy::type_complexity)] +#![allow(clippy::nonminimal_bool)] +#![allow(clippy::never_loop)] +#![allow(clippy::large_enum_variant)] +#![allow(clippy::for_loops_over_fallibles)] +#![allow(clippy::explicit_counter_loop)] +#![allow(clippy::branches_sharing_code)] +#![allow(clippy::while_let_loop)] +#![allow(clippy::redundant_pattern_matching)] +#![allow(clippy::inherent_to_string)] +#![allow(clippy::field_reassign_with_default)] +#![allow(clippy::collapsible_match)] #[macro_use] extern crate nom;