fluent-bit: fix kmsg plugin issues

* improved type conversion
* improved error checking while string parsing
* misc improvements
This commit is contained in:
Mohd Husaam Mehdi 2025-08-19 17:14:44 +05:30 committed by Vivek Dutta
parent b713b68045
commit 596df801e6

View file

@ -0,0 +1,73 @@
diff --git a/plugins/in_kmsg/in_kmsg.c b/plugins/in_kmsg/in_kmsg.c
index cd5c4cd17..15f105451 100644
--- a/plugins/in_kmsg/in_kmsg.c
+++ b/plugins/in_kmsg/in_kmsg.c
@@ -36,7 +36,6 @@
#include <sys/stat.h>
#include <sys/time.h>
#include <inttypes.h>
-#include <time.h>
#include "in_kmsg.h"
@@ -123,12 +122,17 @@ static inline int process_line(const char *line,
ctx->buffer_id++;
errno = 0;
- val = strtol(p, &end, 10);
- if ((errno == ERANGE && (val == INT_MAX || val == INT_MIN))
+ val = strtoul(p, &end, 10);
+ if ((errno == ERANGE && val == ULONG_MAX)
|| (errno != 0 && val == 0)) {
goto fail;
}
+ /* ensure something was consumed */
+ if (end == p) {
+ goto fail;
+ }
+
/* Priority */
priority = FLB_KLOG_PRI(val);
@@ -144,24 +148,35 @@ static inline int process_line(const char *line,
}
p++;
- val = strtoul(p, &end, 10);
- if ((errno == ERANGE && (val == INT_MAX || val == INT_MIN))
+ val = strtoull(p, &end, 10);
+ if ((errno == ERANGE && val == ULLONG_MAX)
|| (errno != 0 && val == 0)) {
goto fail;
}
+ /* make sure strtoull consumed something */
+ /* after the sequence number, the next char must be ',' */
+ if (end == p || *end != ',') {
+ goto fail;
+ }
+
sequence = val;
p = ++end;
/* Timestamp */
- val = strtoul(p, &end, 10);
- if ((errno == ERANGE && (val == INT_MAX || val == INT_MIN))
+ val = strtoull(p, &end, 10);
+ if ((errno == ERANGE && val == ULLONG_MAX)
|| (errno != 0 && val == 0)) {
goto fail;
}
+ /* ensure something was consumed */
+ if (end == p) {
+ goto fail;
+ }
+
tv.tv_sec = val/1000000;
- tv.tv_usec = val - (tv.tv_sec * 1000000);
+ tv.tv_usec = val - ((uint64_t)tv.tv_sec * 1000000);
flb_time_set(&ts, ctx->boot_time.tv_sec + tv.tv_sec, tv.tv_usec * 1000);