]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
mpm/ac: return only unique match count
authorVictor Julien <vjulien@oisf.net>
Tue, 28 Nov 2023 11:08:20 +0000 (12:08 +0100)
committerVictor Julien <victor@inliniac.net>
Fri, 1 Dec 2023 13:55:41 +0000 (14:55 +0100)
Bring implementation in line with Hyperscan, which only counts unique matches.

Update test to reflect the new behavior.

src/util-mpm-ac.c

index 22347d6fef2314332db96c9f11d78155be7a0950..6d0fc050b99ab5acd91cebe11972db8753883528 100644 (file)
@@ -930,7 +930,7 @@ void SCACDestroyCtx(MpmCtx *mpm_ctx)
  * \param buf            Buffer to be searched.
  * \param buflen         Buffer length.
  *
- * \retval matches Match count.
+ * \retval matches Match count: counts unique matches per pattern.
  */
 uint32_t SCACSearch(const MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx,
                     PrefilterRuleStore *pmq, const uint8_t *buf, uint32_t buflen)
@@ -975,8 +975,8 @@ uint32_t SCACSearch(const MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx,
                         } else {
                             bitarray[(lower_pid) / 8] |= (1 << ((lower_pid) % 8));
                             PrefilterAddSids(pmq, pat->sids, pat->sids_size);
+                            matches++;
                         }
-                        matches++;
                     } else {
                         const SCACPatternList *pat = &pid_pat_list[pids[k]];
                         const int offset = i - pat->patlen + 1;
@@ -989,8 +989,8 @@ uint32_t SCACSearch(const MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx,
                         } else {
                             bitarray[pids[k] / 8] |= (1 << (pids[k] % 8));
                             PrefilterAddSids(pmq, pat->sids, pat->sids_size);
+                            matches++;
                         }
-                        matches++;
                     }
                     //loop1:
                     //;
@@ -1026,8 +1026,8 @@ uint32_t SCACSearch(const MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx,
                         } else {
                             bitarray[(lower_pid) / 8] |= (1 << ((lower_pid) % 8));
                             PrefilterAddSids(pmq, pat->sids, pat->sids_size);
+                            matches++;
                         }
-                        matches++;
                     } else {
                         const SCACPatternList *pat = &pid_pat_list[pids[k]];
                         const int offset = i - pat->patlen + 1;
@@ -1040,8 +1040,8 @@ uint32_t SCACSearch(const MpmCtx *mpm_ctx, MpmThreadCtx *mpm_thread_ctx,
                         } else {
                             bitarray[pids[k] / 8] |= (1 << (pids[k] % 8));
                             PrefilterAddSids(pmq, pat->sids, pat->sids_size);
+                            matches++;
                         }
-                        matches++;
                     }
                     //loop1:
                     //;
@@ -1343,7 +1343,6 @@ static int SCACTest06(void)
 
 static int SCACTest07(void)
 {
-    int result = 0;
     MpmCtx mpm_ctx;
     MpmThreadCtx mpm_thread_ctx;
     PrefilterRuleStore pmq;
@@ -1366,22 +1365,18 @@ static int SCACTest07(void)
     MpmAddPatternCS(&mpm_ctx, (uint8_t *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
                      30, 0, 0, 5, 0, 0);
     PmqSetup(&pmq);
-    /* total matches: 135 */
+    /* total matches: 135: unique matches: 6 */
 
     SCACPreparePatterns(&mpm_ctx);
 
     const char *buf = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
     uint32_t cnt = SCACSearch(&mpm_ctx, &mpm_thread_ctx, &pmq,
                                (uint8_t *)buf, strlen(buf));
-
-    if (cnt == 135)
-        result = 1;
-    else
-        printf("135 != %" PRIu32 " ",cnt);
+    FAIL_IF_NOT(cnt == 6);
 
     SCACDestroyCtx(&mpm_ctx);
     PmqFree(&pmq);
-    return result;
+    PASS;
 }
 
 static int SCACTest08(void)