]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Merge pull request #2682 in SNORT/snort3 from ~OSHUMEIK/snort3:log_buffered to master
authorBhagya Tholpady (bbantwal) <bbantwal@cisco.com>
Tue, 12 Jan 2021 17:59:34 +0000 (17:59 +0000)
committerBhagya Tholpady (bbantwal) <bbantwal@cisco.com>
Tue, 12 Jan 2021 17:59:34 +0000 (17:59 +0000)
Squashed commit of the following:

commit 640bdaa5a20b77c4ba8db4d571f1a7e9a52a48b9
Author: Oleksii Shumeiko <oshumeik@cisco.com>
Date:   Wed Dec 16 17:54:34 2020 +0200

    log: reuse TextLog buffer for a large data

    Thanks to Chris White for reporting the issue.

src/log/text_log.cc

index 3228d95cef40ff293ef7f549d944b840023fc2eb..86c2efa22778b803862f0f2499430402c28cd78d 100644 (file)
@@ -33,6 +33,7 @@
 
 #include <sys/stat.h>
 
+#include <algorithm>
 #include <cstdarg>
 
 #include "utils/util.h"
@@ -220,19 +221,26 @@ bool TextLog_Putc(TextLog* const txt, char c)
  */
 bool TextLog_Write(TextLog* const txt, const char* str, int len)
 {
-    int avail = TextLog_Avail(txt);
-
-    if ( len >= avail )
+    do
     {
-        TextLog_Flush(txt);
-        avail = TextLog_Avail(txt);
+        int avail = TextLog_Avail(txt);
+        int n = snprintf(txt->buf+txt->pos, avail, "%.*s", len, str);
+        if ( n < avail and n < len )
+            return false;
+
+        // actual bytes written:
+        // 1) if avail is a limit, auto-appended '\0' should be truncated
+        // 2) avail could be zero from the start, keep it as 0
+        int l = std::min(n, avail > 0 ? avail - 1 : 0);
+        txt->pos += l;
+        str += l;
+        len -= l;
+
+        if ( n >= avail )
+            TextLog_Flush(txt);
     }
-    int n = snprintf(txt->buf+txt->pos, avail, "%.*s", len, str);
-
-    if ( n != len )
-        return false;
+    while ( len > 0 );
 
-    txt->pos += len;
     return true;
 }
 
@@ -269,39 +277,37 @@ bool TextLog_Print(TextLog* const txt, const char* fmt, ...)
     {
         return false;
     }
+
     txt->pos += len;
+
     return true;
 }
 
 /*-------------------------------------------------------------------
  * TextLog_Quote: write string escaping quotes
- * TBD could be smarter by counting required escapes instead of
- * checking for 3
  *-------------------------------------------------------------------
  */
 bool TextLog_Quote(TextLog* const txt, const char* qs)
 {
-    int pos = txt->pos;
+    TextLog_Putc(txt, '"');
 
-    if ( TextLog_Avail(txt) < 3 )
+    do
     {
-        TextLog_Flush(txt);
-    }
-    txt->buf[pos++] = '"';
+        int len = strlen(qs);
+        int pre = strcspn(qs, "\"\\");
 
-    while ( *qs && (txt->maxBuf - pos > 2) )
-    {
-        if ( *qs == '"' || *qs == '\\' )
+        TextLog_Write(txt, qs, pre);
+        qs += pre;
+
+        if ( pre < len )
         {
-            txt->buf[pos++] = '\\';
+            TextLog_Putc(txt, '\\');
+            TextLog_Putc(txt, *qs++);
         }
-        txt->buf[pos++] = *qs++;
     }
-    if ( *qs )
-        return false;
+    while ( *qs );
 
-    txt->buf[pos++] = '"';
-    txt->pos = pos;
+    TextLog_Putc(txt, '"');
 
     return true;
 }