Skip to content

Commit c1b28df

Browse files
author
Erik Karlsson
committed
in_kmsg: fix /dev/kmsg parsing
* use strtoul for priority since it is unsigned * check for overflow correctly by comparing against ULONG_MAX * fail if no digits were consumed or next character is not comma * calculate tv.tv_usec correctly in case tv.tv_sec 32-bit * remove redundant errno = 0 added in 3254b9a * remove erroneous time.h added with 72d9dc8 Signed-off-by: Erik Karlsson <erik.karlsson@iopsys.eu>
1 parent 1132279 commit c1b28df

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

plugins/in_kmsg/in_kmsg.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include <sys/stat.h>
3737
#include <sys/time.h>
3838
#include <inttypes.h>
39-
#include <time.h>
4039

4140
#include "in_kmsg.h"
4241

@@ -114,7 +113,6 @@ static inline int process_line(const char *line,
114113
struct timeval tv; /* time value */
115114
int line_len;
116115
uint64_t val;
117-
long pri_val;
118116
const char *p = line;
119117
char *end = NULL;
120118
struct flb_time ts;
@@ -124,14 +122,19 @@ static inline int process_line(const char *line,
124122
ctx->buffer_id++;
125123

126124
errno = 0;
127-
pri_val = strtol(p, &end, 10);
128-
if ((errno == ERANGE && (pri_val == INT_MAX || pri_val == INT_MIN))
129-
|| (errno != 0 && pri_val == 0)) {
125+
val = strtoul(p, &end, 10);
126+
if ((errno == ERANGE && val == ULONG_MAX)
127+
|| (errno != 0 && val == 0)) {
128+
goto fail;
129+
}
130+
131+
/* ensure something was consumed */
132+
if (end == p) {
130133
goto fail;
131134
}
132135

133136
/* Priority */
134-
priority = FLB_KLOG_PRI(pri_val);
137+
priority = FLB_KLOG_PRI(val);
135138

136139
if (priority > ctx->prio_level) {
137140
/* Drop line */
@@ -145,13 +148,18 @@ static inline int process_line(const char *line,
145148
}
146149
p++;
147150

148-
errno = 0;
149151
val = strtoull(p, &end, 10);
150152
if ((errno == ERANGE && val == ULLONG_MAX)
151153
|| (errno != 0 && val == 0)) {
152154
goto fail;
153155
}
154156

157+
/* make sure strtoull consumed something */
158+
/* after the sequence number, the next char must be ',' */
159+
if (end == p || *end != ',') {
160+
goto fail;
161+
}
162+
155163
sequence = val;
156164
p = ++end;
157165

@@ -162,8 +170,13 @@ static inline int process_line(const char *line,
162170
goto fail;
163171
}
164172

173+
/* ensure something was consumed */
174+
if (end == p) {
175+
goto fail;
176+
}
177+
165178
tv.tv_sec = val/1000000;
166-
tv.tv_usec = val - (tv.tv_sec * 1000000);
179+
tv.tv_usec = val - ((uint64_t)tv.tv_sec * 1000000);
167180

168181
flb_time_set(&ts, ctx->boot_time.tv_sec + tv.tv_sec, tv.tv_usec * 1000);
169182

0 commit comments

Comments
 (0)