Skip to content

Commit 4164c92

Browse files
jnthntatumcopybara-github
authored andcommitted
Check for timestamp out of range for timestamp(int) function.
PiperOrigin-RevId: 733373276
1 parent 000d841 commit 4164c92

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

runtime/standard/type_conversion_functions.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,21 @@ absl::Status RegisterTimeConversionFunctions(FunctionRegistry& registry,
317317
(UnaryFunctionAdapter<Value, const StringValue&>::RegisterGlobalOverload(
318318
cel::builtin::kDuration, CreateDurationFromString, registry)));
319319

320+
bool enable_timestamp_duration_overflow_errors =
321+
options.enable_timestamp_duration_overflow_errors;
322+
320323
// timestamp conversion from int.
321324
CEL_RETURN_IF_ERROR(
322325
(UnaryFunctionAdapter<Value, int64_t>::RegisterGlobalOverload(
323326
cel::builtin::kTimestamp,
324-
[](int64_t epoch_seconds) -> Value {
325-
return TimestampValue(absl::FromUnixSeconds(epoch_seconds));
327+
[=](int64_t epoch_seconds) -> Value {
328+
absl::Time ts = absl::FromUnixSeconds(epoch_seconds);
329+
if (enable_timestamp_duration_overflow_errors) {
330+
if (ts < MinTimestamp() || ts > MaxTimestamp()) {
331+
return ErrorValue(absl::OutOfRangeError("timestamp overflow"));
332+
}
333+
}
334+
return UnsafeTimestampValue(ts);
326335
},
327336
registry)));
328337

@@ -341,8 +350,6 @@ absl::Status RegisterTimeConversionFunctions(FunctionRegistry& registry,
341350
registry)));
342351

343352
// timestamp() conversion from string.
344-
bool enable_timestamp_duration_overflow_errors =
345-
options.enable_timestamp_duration_overflow_errors;
346353
return UnaryFunctionAdapter<Value, const StringValue&>::
347354
RegisterGlobalOverload(
348355
cel::builtin::kTimestamp,

0 commit comments

Comments
 (0)