Skip to content

Commit 07f577b

Browse files
committed
[nits] raise error on negative numeric timestamp
1 parent 68b82f6 commit 07f577b

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

src/log_format.cc

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,16 @@ read_json_number(yajlpp_parse_context* ypc,
13011301
jlu->jlu_exttm.et_flags |= ETF_MICROS_SET;
13021302
}
13031303
jlu->jlu_exttm.et_nsec = tv.tv_usec * 1000;
1304+
if (tv.tv_sec < 0) {
1305+
jlu->jlu_scan_error = log_format::scan_error{
1306+
fmt::format(FMT_STRING("invalid numeric timestamp: {} / {} "
1307+
"(timestamp-divisor) = {}"),
1308+
number_frag,
1309+
divisor,
1310+
tv.tv_sec),
1311+
};
1312+
return 1;
1313+
}
13041314
jlu->jlu_base_line->set_time(tv);
13051315
} else if (!jlu->jlu_format->lf_start_timestamp_field.empty()
13061316
&& jlu->jlu_format->lf_start_timestamp_field == field_name)
@@ -1312,6 +1322,17 @@ read_json_number(yajlpp_parse_context* ypc,
13121322
return 0;
13131323
}
13141324
auto ts_val = scan_res.value().value();
1325+
if (ts_val < 0) {
1326+
jlu->jlu_scan_error = log_format::scan_error{
1327+
fmt::format(
1328+
FMT_STRING("invalid numeric start-timestamp: {} / {} "
1329+
"(timestamp-divisor) = {}"),
1330+
number_frag,
1331+
divisor,
1332+
ts_val),
1333+
};
1334+
return 1;
1335+
}
13151336
jlu->jlu_start_time = std::chrono::microseconds(
13161337
static_cast<int64_t>(ts_val * 1000000.0 / divisor));
13171338
} else if (jlu->jlu_format->lf_subsecond_field == field_name) {
@@ -2779,7 +2800,8 @@ read_json_field(yajlpp_parse_context* ypc,
27792800
jlu->jlu_base_line->set_time(tv_out);
27802801
} else {
27812802
jlu->jlu_scan_error = log_format::scan_error{fmt::format(
2782-
"failed to parse timestamp '{}' in string property '{}'",
2803+
FMT_STRING(
2804+
"failed to parse timestamp '{}' in string property '{}'"),
27832805
frag,
27842806
field_name)};
27852807
}
@@ -3163,11 +3185,10 @@ external_log_format::get_subline(const log_format_file_state& lffs,
31633185
this->jlf_line_values.lvv_allocator);
31643186
}
31653187

3166-
if (jlu.jlu_start_time && jlu.jlu_end_time && !jlu.jlu_duration)
3167-
{
3188+
if (jlu.jlu_start_time && jlu.jlu_end_time && !jlu.jlu_duration) {
31683189
if (jlu.jlu_end_time.value() > jlu.jlu_start_time.value()) {
3169-
jlu.jlu_duration = jlu.jlu_end_time.value()
3170-
- jlu.jlu_start_time.value();
3190+
jlu.jlu_duration
3191+
= jlu.jlu_end_time.value() - jlu.jlu_start_time.value();
31713192
}
31723193
}
31733194

0 commit comments

Comments
 (0)