From 51fded0c5daa6d153695c20e7b5cc15a835d3d4a Mon Sep 17 00:00:00 2001 From: Peter Haag Date: Mon, 29 Jul 2024 13:47:48 +0200 Subject: [PATCH] Cleanup some output code --- src/nfdump/nfdump.c | 1 + src/output/output_csv_fast.c | 12 +++++++++++- src/output/output_fmt.c | 20 ++++++++++++-------- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/nfdump/nfdump.c b/src/nfdump/nfdump.c index edfbd29e..73165b57 100644 --- a/src/nfdump/nfdump.c +++ b/src/nfdump/nfdump.c @@ -1286,6 +1286,7 @@ int main(int argc, char **argv) { nfprof_print(&profile_data, stdout); break; case MODE_CSV: + case MODE_CSV_FAST: break; case MODE_JSON: break; diff --git a/src/output/output_csv_fast.c b/src/output/output_csv_fast.c index 1bcf0c1d..9e6a385b 100644 --- a/src/output/output_csv_fast.c +++ b/src/output/output_csv_fast.c @@ -29,6 +29,7 @@ */ #include +#include #include #include #include @@ -72,12 +73,17 @@ static uint32_t recordCount; *streamPtr++ = ','; \ } while (0) +#define BUFFSIZE 1014 static char *buff = NULL; void csv_prolog_fast(void) { // empty prolog recordCount = 0; - buff = malloc(4096); + buff = malloc(BUFFSIZE); + if (!buff) { + LogError("malloc() error in %s line %d: %s", __FILE__, __LINE__, strerror(errno)); + exit(EXIT_FAILURE); + } buff[0] = '\0'; printf("cnt,af,firstSeen,lastSeen,proto,srcAddr,srcPort,dstAddr,dstPort,srcAS,dstAS,input,output,flags,srcTos,packets,bytes\n"); } // End of csv_prolog_fast @@ -148,6 +154,10 @@ void csv_record_fast(FILE *stream, recordHandle_t *recordHandle, int tag) { *--streamPtr = '\n'; *++streamPtr = '\0'; + if (unlikely((streamPtr - buff) > BUFFSIZE)) { + LogError("csv_record_fast() error in %s line %d: %s", __FILE__, __LINE__, "memory corruption"); + exit(EXIT_FAILURE); + } fputs(buff, stream); } // End of csv_record_fast diff --git a/src/output/output_fmt.c b/src/output/output_fmt.c index 8c737346..358b23de 100755 --- a/src/output/output_fmt.c +++ b/src/output/output_fmt.c @@ -878,9 +878,10 @@ static void String_FirstSeen(FILE *stream, recordHandle_t *recordHandle) { if (msecFirst) { time_t tt = msecFirst / 1000LL; - struct tm *ts = localtime(&tt); + struct tm ts; + localtime_r(&tt, &ts); char s[128]; - strftime(s, 128, "%Y-%m-%d %H:%M:%S", ts); + strftime(s, 128, "%Y-%m-%d %H:%M:%S", &ts); s[127] = '\0'; fprintf(stream, "%s.%03u", s, (unsigned)(msecFirst % 1000LL)); } else { @@ -896,9 +897,10 @@ static void String_LastSeen(FILE *stream, recordHandle_t *recordHandle) { if (msecLast) { time_t tt = msecLast / 1000LL; - struct tm *ts = localtime(&tt); + struct tm ts; + localtime_r(&tt, &ts); char s[128]; - strftime(s, 128, "%Y-%m-%d %H:%M:%S", ts); + strftime(s, 128, "%Y-%m-%d %H:%M:%S", &ts); s[127] = '\0'; fprintf(stream, "%s.%03u", s, (unsigned)(msecLast % 1000LL)); } else { @@ -914,9 +916,10 @@ static void String_Received(FILE *stream, recordHandle_t *recordHandle) { if (msecReceived) { time_t tt = msecReceived / 1000LL; - struct tm *ts = localtime(&tt); + struct tm ts; + localtime_r(&tt, &ts); char s[128]; - strftime(s, 128, "%Y-%m-%d %H:%M:%S", ts); + strftime(s, 128, "%Y-%m-%d %H:%M:%S", &ts); s[127] = '\0'; fprintf(stream, "%s.%03llu", s, msecReceived % 1000LL); } else { @@ -1214,9 +1217,10 @@ static void String_EventTime(FILE *stream, recordHandle_t *recordHandle) { if (msecEvent) { time_t tt = msecEvent / 1000LL; - struct tm *ts = localtime(&tt); + struct tm ts; + localtime_r(&tt, &ts); char s[128]; - strftime(s, 128, "%Y-%m-%d %H:%M:%S", ts); + strftime(s, 128, "%Y-%m-%d %H:%M:%S", &ts); s[127] = '\0'; fprintf(stream, "%s.%03llu", s, msecEvent % 1000LL); } else {