]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect/integers: harmonize parser return handling
authorPhilippe Antoine <pantoine@oisf.net>
Mon, 15 Jul 2024 07:23:06 +0000 (09:23 +0200)
committerVictor Julien <victor@inliniac.net>
Wed, 7 Aug 2024 06:31:24 +0000 (08:31 +0200)
Ticket: 7172

When parsing an integer for a rule keyword fails, we return error
straight away, without bothering to try to free the NULL pointer.

On the way, remove some one-line wrapper around DetectUxParse

(cherry picked from commit daad7f2d417bb730e51df142fb837d216938089f)

src/detect-bsize.c
src/detect-dsize.c
src/detect-filesize.c
src/detect-icode.c
src/detect-itype.c
src/detect-rfb-sectype.c
src/tests/detect-bsize.c

index 3b3efe87b7ff841e1a5fdf06d93fe9f830a45b6e..8d8b34ea4be5115927c1850a0fe4f0c081df5c65 100644 (file)
@@ -157,20 +157,6 @@ int DetectBsizeMatch(const SigMatchCtx *ctx, const uint64_t buffer_size, bool eo
     return 0;
 }
 
-/**
- * \brief This function is used to parse bsize options passed via bsize: keyword
- *
- * \param bsizestr Pointer to the user provided bsize options
- *
- * \retval bsized pointer to DetectU64Data on success
- * \retval NULL on failure
- */
-
-static DetectU64Data *DetectBsizeParse(const char *str)
-{
-    return DetectU64Parse(str);
-}
-
 static int SigParseGetMaxBsize(DetectU64Data *bsz)
 {
     switch (bsz->mode) {
@@ -208,9 +194,9 @@ static int DetectBsizeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *
     if (list == DETECT_SM_LIST_NOTSET)
         SCReturnInt(-1);
 
-    DetectU64Data *bsz = DetectBsizeParse(sizestr);
+    DetectU64Data *bsz = DetectU64Parse(sizestr);
     if (bsz == NULL)
-        goto error;
+        SCReturnInt(-1);
 
     sm = SigMatchAlloc();
     if (sm == NULL)
index 4336e3546b9fa66334491cb7f819e06b72c4c290..7dd6b4012e3bf51584406b79d2c130e3c4e10bc4 100644 (file)
@@ -124,7 +124,7 @@ static int DetectDsizeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *
     if (DetectGetLastSMFromLists(s, DETECT_DSIZE, -1)) {
         SCLogError("Can't use 2 or more dsizes in "
                    "the same sig.  Invalidating signature.");
-        goto error;
+        return -1;
     }
 
     SCLogDebug("\'%s\'", rawstr);
@@ -132,7 +132,7 @@ static int DetectDsizeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *
     dd = DetectU16Parse(rawstr);
     if (dd == NULL) {
         SCLogError("Parsing \'%s\' failed", rawstr);
-        goto error;
+        return -1;
     }
 
     /* Okay so far so good, lets get this into a SigMatch
@@ -141,7 +141,7 @@ static int DetectDsizeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *
     if (sm == NULL){
         SCLogError("Failed to allocate memory for SigMatch");
         rs_detect_u16_free(dd);
-        goto error;
+        return -1;
     }
 
     sm->type = DETECT_DSIZE;
@@ -160,9 +160,6 @@ static int DetectDsizeSetup (DetectEngineCtx *de_ctx, Signature *s, const char *
     }
 
     return 0;
-
-error:
-    return -1;
 }
 
 /**
index 05caba8568c19d97c9c03f1840cc402a8c374e10..616ff9dbd94197d48b8fe158447578e6d7773ee4 100644 (file)
@@ -127,7 +127,7 @@ static int DetectFilesizeSetup (DetectEngineCtx *de_ctx, Signature *s, const cha
 
     fsd = DetectU64Parse(str);
     if (fsd == NULL)
-        goto error;
+        SCReturnInt(-1);
 
     sm = SigMatchAlloc();
     if (sm == NULL)
index 7758bd9bae05bb6879c5d6c95463fc45c9c22f52..e9616873ca9d388b87658f58cd2460c62c6e1387 100644 (file)
@@ -121,7 +121,8 @@ static int DetectICodeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *i
     SigMatch *sm = NULL;
 
     icd = DetectU8Parse(icodestr);
-    if (icd == NULL) goto error;
+    if (icd == NULL)
+        return -1;
 
     sm = SigMatchAlloc();
     if (sm == NULL) goto error;
index ac067ab355b530c3acf1d2b85e9383d0a1f24433..a5abcc97004279059704ea4f61c3a73ab42cf438 100644 (file)
@@ -101,20 +101,6 @@ static int DetectITypeMatch (DetectEngineThreadCtx *det_ctx, Packet *p,
     return DetectU8Match(pitype, itd);
 }
 
-/**
- * \brief This function is used to parse itype options passed via itype: keyword
- *
- * \param de_ctx Pointer to the detection engine context
- * \param itypestr Pointer to the user provided itype options
- *
- * \retval itd pointer to DetectU8Data on success
- * \retval NULL on failure
- */
-static DetectU8Data *DetectITypeParse(DetectEngineCtx *de_ctx, const char *itypestr)
-{
-    return DetectU8Parse(itypestr);
-}
-
 /**
  * \brief this function is used to add the parsed itype data into the current signature
  *
@@ -131,8 +117,9 @@ static int DetectITypeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *i
     DetectU8Data *itd = NULL;
     SigMatch *sm = NULL;
 
-    itd = DetectITypeParse(de_ctx, itypestr);
-    if (itd == NULL) goto error;
+    itd = DetectU8Parse(itypestr);
+    if (itd == NULL)
+        return -1;
 
     sm = SigMatchAlloc();
     if (sm == NULL) goto error;
@@ -221,7 +208,7 @@ static bool PrefilterITypeIsPrefilterable(const Signature *s)
 static int DetectITypeParseTest01(void)
 {
     DetectU8Data *itd = NULL;
-    itd = DetectITypeParse(NULL, "8");
+    itd = DetectU8Parse("8");
     FAIL_IF_NULL(itd);
     FAIL_IF_NOT(itd->arg1 == 8);
     FAIL_IF_NOT(itd->mode == DETECT_UINT_EQ);
@@ -237,7 +224,7 @@ static int DetectITypeParseTest01(void)
 static int DetectITypeParseTest02(void)
 {
     DetectU8Data *itd = NULL;
-    itd = DetectITypeParse(NULL, ">8");
+    itd = DetectU8Parse(">8");
     FAIL_IF_NULL(itd);
     FAIL_IF_NOT(itd->arg1 == 8);
     FAIL_IF_NOT(itd->mode == DETECT_UINT_GT);
@@ -253,7 +240,7 @@ static int DetectITypeParseTest02(void)
 static int DetectITypeParseTest03(void)
 {
     DetectU8Data *itd = NULL;
-    itd = DetectITypeParse(NULL, "<8");
+    itd = DetectU8Parse("<8");
     FAIL_IF_NULL(itd);
     FAIL_IF_NOT(itd->arg1 == 8);
     FAIL_IF_NOT(itd->mode == DETECT_UINT_LT);
@@ -269,7 +256,7 @@ static int DetectITypeParseTest03(void)
 static int DetectITypeParseTest04(void)
 {
     DetectU8Data *itd = NULL;
-    itd = DetectITypeParse(NULL, "8<>20");
+    itd = DetectU8Parse("8<>20");
     FAIL_IF_NULL(itd);
     FAIL_IF_NOT(itd->arg1 == 8);
     FAIL_IF_NOT(itd->arg2 == 20);
@@ -286,7 +273,7 @@ static int DetectITypeParseTest04(void)
 static int DetectITypeParseTest05(void)
 {
     DetectU8Data *itd = NULL;
-    itd = DetectITypeParse(NULL, "   8 ");
+    itd = DetectU8Parse("   8 ");
     FAIL_IF_NULL(itd);
     FAIL_IF_NOT(itd->arg1 == 8);
     FAIL_IF_NOT(itd->mode == DETECT_UINT_EQ);
@@ -302,7 +289,7 @@ static int DetectITypeParseTest05(void)
 static int DetectITypeParseTest06(void)
 {
     DetectU8Data *itd = NULL;
-    itd = DetectITypeParse(NULL, "  >  8  ");
+    itd = DetectU8Parse("  >  8  ");
     FAIL_IF_NULL(itd);
     FAIL_IF_NOT(itd->arg1 == 8);
     FAIL_IF_NOT(itd->mode == DETECT_UINT_GT);
@@ -318,7 +305,7 @@ static int DetectITypeParseTest06(void)
 static int DetectITypeParseTest07(void)
 {
     DetectU8Data *itd = NULL;
-    itd = DetectITypeParse(NULL, "  8  <> 20  ");
+    itd = DetectU8Parse("  8  <> 20  ");
     FAIL_IF_NULL(itd);
     FAIL_IF_NOT(itd->arg1 == 8);
     FAIL_IF_NOT(itd->arg2 == 20);
@@ -334,7 +321,7 @@ static int DetectITypeParseTest07(void)
 static int DetectITypeParseTest08(void)
 {
     DetectU8Data *itd = NULL;
-    itd = DetectITypeParse(NULL, "> 8 <> 20");
+    itd = DetectU8Parse("> 8 <> 20");
     FAIL_IF_NOT_NULL(itd);
 
     PASS;
index 400ee5cb087c7b51e54ead7fe6bed13fe26db72f..476ea3fd3c06d48c423e19c45557ce9a5984ac5a 100644 (file)
@@ -90,20 +90,6 @@ static int DetectRfbSectypeMatch (DetectEngineThreadCtx *det_ctx,
     SCReturnInt(0);
 }
 
-/**
- * \internal
- * \brief Function to parse options passed via rfb.sectype keywords.
- *
- * \param rawstr Pointer to the user provided options.
- *
- * \retval dd pointer to DetectU32Data on success.
- * \retval NULL on failure.
- */
-static DetectU32Data *DetectRfbSectypeParse(const char *rawstr)
-{
-    return DetectU32Parse(rawstr);
-}
-
 /**
  * \brief Function to add the parsed RFB security type field into the current signature.
  *
@@ -119,10 +105,10 @@ static int DetectRfbSectypeSetup (DetectEngineCtx *de_ctx, Signature *s, const c
     if (DetectSignatureSetAppProto(s, ALPROTO_RFB) != 0)
         return -1;
 
-    DetectU32Data *dd = DetectRfbSectypeParse(rawstr);
+    DetectU32Data *dd = DetectU32Parse(rawstr);
     if (dd == NULL) {
         SCLogError("Parsing \'%s\' failed", rawstr);
-        goto error;
+        return -1;
     }
 
     /* okay so far so good, lets get this into a SigMatch
index 2fcd656589907f9a9d92650ffec5195705cbd595..f0b13a8944a6ea2ba8eb9d036cf44b1f641bdd66 100644 (file)
@@ -19,7 +19,7 @@
 
 #define TEST_OK(str, m, lo, hi)                                                                    \
     {                                                                                              \
-        DetectU64Data *bsz = DetectBsizeParse((str));                                              \
+        DetectU64Data *bsz = DetectU64Parse((str));                                                \
         FAIL_IF_NULL(bsz);                                                                         \
         FAIL_IF_NOT(bsz->mode == (m));                                                             \
         DetectBsizeFree(NULL, bsz);                                                                \
@@ -27,7 +27,7 @@
     }
 #define TEST_FAIL(str)                                                                             \
     {                                                                                              \
-        DetectU64Data *bsz = DetectBsizeParse((str));                                              \
+        DetectU64Data *bsz = DetectU64Parse((str));                                                \
         FAIL_IF_NOT_NULL(bsz);                                                                     \
     }