Skip to content

Commit 51fded0

Browse files
committed
Cleanup some output code
1 parent 9d3cdd9 commit 51fded0

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

Diff for: src/nfdump/nfdump.c

+1
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,7 @@ int main(int argc, char **argv) {
12861286
nfprof_print(&profile_data, stdout);
12871287
break;
12881288
case MODE_CSV:
1289+
case MODE_CSV_FAST:
12891290
break;
12901291
case MODE_JSON:
12911292
break;

Diff for: src/output/output_csv_fast.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030

3131
#include <arpa/inet.h>
32+
#include <errno.h>
3233
#include <inttypes.h>
3334
#include <netinet/in.h>
3435
#include <stddef.h>
@@ -72,12 +73,17 @@ static uint32_t recordCount;
7273
*streamPtr++ = ','; \
7374
} while (0)
7475

76+
#define BUFFSIZE 1014
7577
static char *buff = NULL;
7678

7779
void csv_prolog_fast(void) {
7880
// empty prolog
7981
recordCount = 0;
80-
buff = malloc(4096);
82+
buff = malloc(BUFFSIZE);
83+
if (!buff) {
84+
LogError("malloc() error in %s line %d: %s", __FILE__, __LINE__, strerror(errno));
85+
exit(EXIT_FAILURE);
86+
}
8187
buff[0] = '\0';
8288
printf("cnt,af,firstSeen,lastSeen,proto,srcAddr,srcPort,dstAddr,dstPort,srcAS,dstAS,input,output,flags,srcTos,packets,bytes\n");
8389
} // End of csv_prolog_fast
@@ -148,6 +154,10 @@ void csv_record_fast(FILE *stream, recordHandle_t *recordHandle, int tag) {
148154
*--streamPtr = '\n';
149155
*++streamPtr = '\0';
150156

157+
if (unlikely((streamPtr - buff) > BUFFSIZE)) {
158+
LogError("csv_record_fast() error in %s line %d: %s", __FILE__, __LINE__, "memory corruption");
159+
exit(EXIT_FAILURE);
160+
}
151161
fputs(buff, stream);
152162

153163
} // End of csv_record_fast

Diff for: src/output/output_fmt.c

+12-8
Original file line numberDiff line numberDiff line change
@@ -878,9 +878,10 @@ static void String_FirstSeen(FILE *stream, recordHandle_t *recordHandle) {
878878

879879
if (msecFirst) {
880880
time_t tt = msecFirst / 1000LL;
881-
struct tm *ts = localtime(&tt);
881+
struct tm ts;
882+
localtime_r(&tt, &ts);
882883
char s[128];
883-
strftime(s, 128, "%Y-%m-%d %H:%M:%S", ts);
884+
strftime(s, 128, "%Y-%m-%d %H:%M:%S", &ts);
884885
s[127] = '\0';
885886
fprintf(stream, "%s.%03u", s, (unsigned)(msecFirst % 1000LL));
886887
} else {
@@ -896,9 +897,10 @@ static void String_LastSeen(FILE *stream, recordHandle_t *recordHandle) {
896897

897898
if (msecLast) {
898899
time_t tt = msecLast / 1000LL;
899-
struct tm *ts = localtime(&tt);
900+
struct tm ts;
901+
localtime_r(&tt, &ts);
900902
char s[128];
901-
strftime(s, 128, "%Y-%m-%d %H:%M:%S", ts);
903+
strftime(s, 128, "%Y-%m-%d %H:%M:%S", &ts);
902904
s[127] = '\0';
903905
fprintf(stream, "%s.%03u", s, (unsigned)(msecLast % 1000LL));
904906
} else {
@@ -914,9 +916,10 @@ static void String_Received(FILE *stream, recordHandle_t *recordHandle) {
914916

915917
if (msecReceived) {
916918
time_t tt = msecReceived / 1000LL;
917-
struct tm *ts = localtime(&tt);
919+
struct tm ts;
920+
localtime_r(&tt, &ts);
918921
char s[128];
919-
strftime(s, 128, "%Y-%m-%d %H:%M:%S", ts);
922+
strftime(s, 128, "%Y-%m-%d %H:%M:%S", &ts);
920923
s[127] = '\0';
921924
fprintf(stream, "%s.%03llu", s, msecReceived % 1000LL);
922925
} else {
@@ -1214,9 +1217,10 @@ static void String_EventTime(FILE *stream, recordHandle_t *recordHandle) {
12141217

12151218
if (msecEvent) {
12161219
time_t tt = msecEvent / 1000LL;
1217-
struct tm *ts = localtime(&tt);
1220+
struct tm ts;
1221+
localtime_r(&tt, &ts);
12181222
char s[128];
1219-
strftime(s, 128, "%Y-%m-%d %H:%M:%S", ts);
1223+
strftime(s, 128, "%Y-%m-%d %H:%M:%S", &ts);
12201224
s[127] = '\0';
12211225
fprintf(stream, "%s.%03llu", s, msecEvent % 1000LL);
12221226
} else {

0 commit comments

Comments
 (0)