]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
frames: address coverity issue
authorVictor Julien <vjulien@oisf.net>
Wed, 9 Feb 2022 07:23:19 +0000 (08:23 +0100)
committerVictor Julien <vjulien@oisf.net>
Wed, 9 Feb 2022 07:24:39 +0000 (08:24 +0100)
Minor cleanups to assist coverity.

Bug: #5065.

src/output-json-alert.c
src/output-json-frame.c
src/output-json-frame.h

index c14cfa43edce485ad24318205263abc3b25202b8..0465184fb690f018a52238809664a82a2d3947e6 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2013-2021 Open Information Security Foundation
+/* Copyright (C) 2013-2022 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
@@ -610,18 +610,19 @@ static void AlertAddFrame(const Packet *p, JsonBuilder *jb, const int64_t frame_
             stream = &ssn->server;
             frames = &frames_container->toclient;
         }
+        Frame *frame = FrameGetById(frames, frame_id);
+        if (frame != NULL) {
+            FrameJsonLogOneFrame(IPPROTO_TCP, frame, p->flow, stream, p, jb);
+        }
     } else if (p->proto == IPPROTO_UDP) {
         if (PKT_IS_TOSERVER(p)) {
             frames = &frames_container->toserver;
         } else {
             frames = &frames_container->toclient;
         }
-    }
-
-    if (frames) {
         Frame *frame = FrameGetById(frames, frame_id);
         if (frame != NULL) {
-            FrameJsonLogOneFrame(frame, p->flow, stream, p, jb);
+            FrameJsonLogOneFrame(IPPROTO_UDP, frame, p->flow, NULL, p, jb);
         }
     }
 }
index 4cbde9ce00314ee56fcac2f1d04b61451fd2fcb7..e0caf1de03099e399600af0d96e8fa592662f3c2 100644 (file)
@@ -212,20 +212,26 @@ static void FrameAddPayloadUDP(JsonBuilder *js, const Packet *p, const Frame *fr
 }
 
 // TODO separate between stream_offset and frame_offset
-void FrameJsonLogOneFrame(const Frame *frame, const Flow *f, const TcpStream *stream,
-        const Packet *p, JsonBuilder *jb)
+/** \brief log a single frame
+ *  \note ipproto argument is passed to assist static code analyzers
+ */
+void FrameJsonLogOneFrame(const uint8_t ipproto, const Frame *frame, const Flow *f,
+        const TcpStream *stream, const Packet *p, JsonBuilder *jb)
 {
+    DEBUG_VALIDATE_BUG_ON(ipproto != p->proto);
+    DEBUG_VALIDATE_BUG_ON(ipproto != f->proto);
+
     jb_open_object(jb, "frame");
-    jb_set_string(jb, "type", AppLayerParserGetFrameNameById(f->proto, f->alproto, frame->type));
+    jb_set_string(jb, "type", AppLayerParserGetFrameNameById(ipproto, f->alproto, frame->type));
     jb_set_uint(jb, "id", frame->id);
     jb_set_string(jb, "direction", PKT_IS_TOSERVER(p) ? "toserver" : "toclient");
 
-    if (f->proto == IPPROTO_TCP) {
+    if (ipproto == IPPROTO_TCP) {
         DEBUG_VALIDATE_BUG_ON(stream == NULL);
         int64_t abs_offset = frame->rel_offset + (int64_t)STREAM_BASE_OFFSET(stream);
         jb_set_uint(jb, "stream_offset", (uint64_t)abs_offset);
 
-        if (f->proto == IPPROTO_TCP && frame->len < 0) {
+        if (frame->len < 0) {
             uint64_t usable = StreamTcpGetUsable(stream, true);
             uint64_t len = usable - abs_offset;
             jb_set_uint(jb, "length", len);
@@ -270,7 +276,7 @@ static int FrameJsonUdp(
             return TM_ECODE_OK;
 
         jb_set_string(jb, "app_proto", AppProtoToString(f->alproto));
-        FrameJsonLogOneFrame(frame, p->flow, NULL, p, jb);
+        FrameJsonLogOneFrame(IPPROTO_UDP, frame, p->flow, NULL, p, jb);
         OutputJsonBuilderBuffer(jb, aft->ctx);
         jb_free(jb);
         frame->flags |= FRAME_FLAG_LOGGED;
@@ -345,7 +351,7 @@ static int FrameJson(ThreadVars *tv, JsonFrameLogThread *aft, const Packet *p)
                 return TM_ECODE_OK;
 
             jb_set_string(jb, "app_proto", AppProtoToString(p->flow->alproto));
-            FrameJsonLogOneFrame(frame, p->flow, stream, p, jb);
+            FrameJsonLogOneFrame(IPPROTO_TCP, frame, p->flow, stream, p, jb);
             OutputJsonBuilderBuffer(jb, aft->ctx);
             jb_free(jb);
             frame->flags |= FRAME_FLAG_LOGGED;
index 73c07ba92716fdabbe35b1b982ce864574761850..5560a07eea533615927b12a664fe2d8a13ced352 100644 (file)
@@ -27,8 +27,8 @@
 #ifndef __OUTPUT_JSON_FRAME_H__
 #define __OUTPUT_JSON_FRAME_H__
 
-void FrameJsonLogOneFrame(const Frame *frame, const Flow *f, const TcpStream *stream,
-        const Packet *p, JsonBuilder *jb);
+void FrameJsonLogOneFrame(const uint8_t ipproto, const Frame *frame, const Flow *f,
+        const TcpStream *stream, const Packet *p, JsonBuilder *jb);
 void JsonFrameLogRegister(void);
 
 #endif /* __OUTPUT_JSON_FRAME_H__ */