From: Jeff Lucovsky Date: Wed, 11 Dec 2024 13:26:01 +0000 (-0500) Subject: output/log: Improve error handling X-Git-Tag: suricata-7.0.9~50 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=583a4b946a06cbe2b80b8b215cba2a3f39636eb1;p=thirdparty%2Fsuricata.git output/log: Improve error handling This commit improves error handling for cases when file(s) cannot be opened. - Return NULL if file object can't be opened - checks whether the file object has been opened before dereferencing the per-file context. Issue: 7447 (cherry picked from commit e72fc39f831e5c03d7884caa447eb758a345d1c8) --- diff --git a/src/util-logopenfile.c b/src/util-logopenfile.c index feca63f44a..5206365bda 100644 --- a/src/util-logopenfile.c +++ b/src/util-logopenfile.c @@ -347,14 +347,17 @@ static void ThreadLogFileHashFreeFunc(void *data) BUG_ON(data == NULL); ThreadLogFileHashEntry *thread_ent = (ThreadLogFileHashEntry *)data; - if (thread_ent) { + if (!thread_ent) + return; + + if (thread_ent->isopen) { LogFileCtx *lf_ctx = thread_ent->ctx; /* Free the leaf log file entries */ if (!lf_ctx->threaded) { LogFileFreeCtx(lf_ctx); } - SCFree(thread_ent); } + SCFree(thread_ent); } bool SCLogOpenThreadedFile(const char *log_path, const char *append, LogFileCtx *parent_ctx) @@ -711,6 +714,7 @@ LogFileCtx *LogFileEnsureExists(LogFileCtx *parent_ctx) if (!parent_ctx->threaded) return parent_ctx; + LogFileCtx *ret_ctx = NULL; SCMutexLock(&parent_ctx->threads->mutex); /* Find this thread's entry */ ThreadLogFileHashEntry *entry = LogFileThread2Slot(parent_ctx->threads); @@ -719,12 +723,13 @@ LogFileCtx *LogFileEnsureExists(LogFileCtx *parent_ctx) bool new = entry->isopen; /* has it been opened yet? */ - if (!entry->isopen) { - SCLogDebug("Opening new file for thread/slot %d to file %s [ctx %p]", entry->slot_number, - parent_ctx->filename, parent_ctx); + if (!new) { + SCLogDebug("%s: Opening new file for thread/id %ld to file %s [ctx %p]", t_thread_name, + SCGetThreadIdLong(), parent_ctx->filename, parent_ctx); if (LogFileNewThreadedCtx( parent_ctx, parent_ctx->filename, parent_ctx->threads->append, entry)) { entry->isopen = true; + ret_ctx = entry->ctx; } else { SCLogError( "Unable to open slot %d for file %s", entry->slot_number, parent_ctx->filename); @@ -740,7 +745,7 @@ LogFileCtx *LogFileEnsureExists(LogFileCtx *parent_ctx) } } - return entry->ctx; + return ret_ctx; } /** \brief LogFileThreadedName() Create file name for threaded EVE storage