From e7e510dccf390c87b602cfc45c4d8b3f888a5015 Mon Sep 17 00:00:00 2001 From: Sergey Zhidkih Date: Mon, 1 Dec 2025 16:07:37 +0300 Subject: [PATCH] hyperscan: move error logging for hs_compile Move error logging from util-mpm-hs to reuse it in util-spm-hs as it has proper hs_compile error handling. Bug: #8146. (cherry picked from commit 11073a019084f8900b2f4e8f030f92136221f9d2) --- src/util-hyperscan.c | 18 ++++++++++++++++++ src/util-hyperscan.h | 6 ++++++ src/util-mpm-hs.c | 13 +------------ src/util-spm-hs.c | 6 +----- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/util-hyperscan.c b/src/util-hyperscan.c index c49012b6b2..c5156e9cb5 100644 --- a/src/util-hyperscan.c +++ b/src/util-hyperscan.c @@ -26,6 +26,9 @@ #include "suricata-common.h" #include "suricata.h" +#include "util-debug.h" +#include "util-mem.h" + #ifdef BUILD_HYPERSCAN #include "util-hyperscan.h" @@ -56,4 +59,19 @@ char *HSRenderPattern(const uint8_t *pat, uint16_t pat_len) return str; } +void HSLogCompileError(const char *expr, hs_compile_error_t *compile_err, hs_error_t err) +{ + if (compile_err) { + SCLogError("Unable to compile '%s' with Hyperscan, " + "returned %d." + "Hyperscan compile error: %s", + expr, err, compile_err->message); + hs_free_compile_error(compile_err); + } else { + SCLogError("Unable to compile '%s' with Hyperscan, " + "returned %d.", + expr, err); + } +} + #endif /* BUILD_HYPERSCAN */ diff --git a/src/util-hyperscan.h b/src/util-hyperscan.h index 661055a260..2f14d8c238 100644 --- a/src/util-hyperscan.h +++ b/src/util-hyperscan.h @@ -26,6 +26,12 @@ #ifndef SURICATA_UTIL_HYPERSCAN__H #define SURICATA_UTIL_HYPERSCAN__H +#ifdef BUILD_HYPERSCAN +#include + char *HSRenderPattern(const uint8_t *pat, uint16_t pat_len); +void HSLogCompileError(const char *expr, hs_compile_error_t *compile_err, hs_error_t err); + +#endif /* BUILD_HYPERSCAN */ #endif /* SURICATA_UTIL_HYPERSCAN__H */ diff --git a/src/util-mpm-hs.c b/src/util-mpm-hs.c index 5d340b0b1c..210a77b645 100644 --- a/src/util-mpm-hs.c +++ b/src/util-mpm-hs.c @@ -49,8 +49,6 @@ #ifdef BUILD_HYPERSCAN -#include - void SCHSInitCtx(MpmCtx *); void SCHSInitThreadCtx(MpmCtx *, MpmThreadCtx *); void SCHSDestroyCtx(MpmCtx *); @@ -607,15 +605,6 @@ static int HSGlobalPatternDatabaseInit(void) return 0; } -static void HSLogCompileError(hs_compile_error_t *compile_err) -{ - SCLogError("failed to compile hyperscan database"); - if (compile_err) { - SCLogError("compile error: %s", compile_err->message); - hs_free_compile_error(compile_err); - } -} - static int HSScratchAlloc(const hs_database_t *db) { SCMutexLock(&g_scratch_proto_mutex); @@ -737,7 +726,7 @@ static int PatternDatabaseCompile(PatternDatabase *pd, SCHSCompileData *cd) (const hs_expr_ext_t *const *)cd->ext, cd->pattern_cnt, HS_MODE_BLOCK, NULL, &pd->hs_db, &compile_err); if (err != HS_SUCCESS) { - HSLogCompileError(compile_err); + HSLogCompileError("multiple patterns", compile_err, err); return -1; } diff --git a/src/util-spm-hs.c b/src/util-spm-hs.c index 0cb003d856..e84c66951a 100644 --- a/src/util-spm-hs.c +++ b/src/util-spm-hs.c @@ -32,8 +32,6 @@ #ifdef BUILD_HYPERSCAN -#include - /** * \internal * \brief Hyperscan match callback, called by hs_scan. @@ -82,9 +80,7 @@ static int HSBuildDatabase(const uint8_t *needle, uint16_t needle_len, hs_error_t err = hs_compile(expr, flags, HS_MODE_BLOCK, NULL, &db, &compile_err); if (err != HS_SUCCESS) { - SCLogError("Unable to compile '%s' with Hyperscan, " - "returned %d.", - expr, err); + HSLogCompileError(expr, compile_err, err); return -1; }