From: Bhagya Tholpady (bbantwal) Date: Tue, 12 Jan 2021 17:59:34 +0000 (+0000) Subject: Merge pull request #2682 in SNORT/snort3 from ~OSHUMEIK/snort3:log_buffered to master X-Git-Tag: 3.1.0.0~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=159947458476b39588b771fba471f7fa9e8c8ca5;p=thirdparty%2Fsnort3.git Merge pull request #2682 in SNORT/snort3 from ~OSHUMEIK/snort3:log_buffered to master Squashed commit of the following: commit 640bdaa5a20b77c4ba8db4d571f1a7e9a52a48b9 Author: Oleksii Shumeiko 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. --- diff --git a/src/log/text_log.cc b/src/log/text_log.cc index 3228d95ce..86c2efa22 100644 --- a/src/log/text_log.cc +++ b/src/log/text_log.cc @@ -33,6 +33,7 @@ #include +#include #include #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; }