Skip to content

Commit

Permalink
output/log: Improve error handling
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jlucovsky committed Dec 11, 2024
1 parent 38d7900 commit 320f06b
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/util-logopenfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,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)
Expand Down Expand Up @@ -712,6 +715,7 @@ LogFileCtx *LogFileEnsureExists(ThreadId thread_id, 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, thread_id);
Expand All @@ -721,12 +725,13 @@ LogFileCtx *LogFileEnsureExists(ThreadId thread_id, LogFileCtx *parent_ctx)

bool new = entry->isopen;
/* has it been opened yet? */
if (!entry->isopen) {
if (!new) {
SCLogDebug("%s: Opening new file for thread/id %d to file %s [ctx %p]", t_thread_name,
thread_id, 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);
Expand All @@ -742,7 +747,7 @@ LogFileCtx *LogFileEnsureExists(ThreadId thread_id, LogFileCtx *parent_ctx)
}
}

return entry->ctx;
return ret_ctx;
}

/** \brief LogFileThreadedName() Create file name for threaded EVE storage
Expand Down

0 comments on commit 320f06b

Please sign in to comment.