]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
log-stats: expand membuffer if necessary 1234/head
authorVictor Julien <victor@inliniac.net>
Wed, 3 Dec 2014 12:22:46 +0000 (13:22 +0100)
committerVictor Julien <victor@inliniac.net>
Wed, 3 Dec 2014 12:22:46 +0000 (13:22 +0100)
Many threads could lead to a membuffer size requirement bigger than
64k. So use the expansion call to grow the buffer as needed.

src/log-stats.c

index fafd4c285281c931d204381a0b7247805c9876c5..1ae4246208e937c1b9a67a3ae6aa2fd4101dc2e5 100644 (file)
@@ -47,7 +47,7 @@
 
 #define DEFAULT_LOG_FILENAME "stats.log"
 #define MODULE_NAME "LogStatsLog"
-#define OUTPUT_BUFFER_SIZE 65535
+#define OUTPUT_BUFFER_SIZE 16384
 
 TmEcode LogStatsLogThreadInit(ThreadVars *, void *, void **);
 TmEcode LogStatsLogThreadDeinit(ThreadVars *, void *);
@@ -102,8 +102,18 @@ int LogStatsLogger(ThreadVars *tv, void *thread_data, const StatsTable *st)
     for (u = 0; u < st->nstats; u++) {
         if (st->stats[u].name == NULL)
             break;
-        MemBufferWriteString(aft->buffer, "%-25s | %-25s | %-" PRIu64 "\n",
-                st->stats[u].name, st->stats[u].tm_name, st->stats[u].value);
+
+        char line[1024];
+        size_t len = snprintf(line, sizeof(line), "%-25s | %-25s | %-" PRIu64 "\n",
+                  st->stats[u].name, st->stats[u].tm_name, st->stats[u].value);
+
+        /* since we can have many threads, the buffer might not be big enough.
+         * Expand if necessary. */
+        if (MEMBUFFER_OFFSET(aft->buffer) + len > MEMBUFFER_SIZE(aft->buffer)) {
+            MemBufferExpand(&aft->buffer, OUTPUT_BUFFER_SIZE);
+        }
+
+        MemBufferWriteString(aft->buffer, "%s", line);
     }
 
     SCMutexLock(&aft->statslog_ctx->file_ctx->fp_mutex);