Skip to content

Commit c5ffb5e

Browse files
Internal change
PiperOrigin-RevId: 823424318
1 parent 89fb5eb commit c5ffb5e

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/google/protobuf/util/time_util.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ Timestamp CreateNormalized(int64_t seconds, int32_t nanos) {
5252

5353
// Make sure nanos is in the range.
5454
if (nanos <= -kNanosPerSecond || nanos >= kNanosPerSecond) {
55-
seconds += nanos / kNanosPerSecond;
55+
int64_t seconds_offset = nanos / kNanosPerSecond;
56+
ABSL_DCHECK(!((seconds_offset > 0 &&
57+
seconds > TimeUtil::kTimestampMaxSeconds - seconds_offset) ||
58+
(seconds_offset < 0 &&
59+
seconds < TimeUtil::kTimestampMinSeconds - seconds_offset)))
60+
<< "Timestamp seconds overflow when normalizing nanos.";
61+
seconds += seconds_offset;
5662
nanos = nanos % kNanosPerSecond;
5763
}
5864
// For Timestamp nanos should be in the range [0, 999999999]
@@ -80,7 +86,13 @@ Duration CreateNormalized(int64_t seconds, int32_t nanos) {
8086

8187
// Make sure nanos is in the range.
8288
if (nanos <= -kNanosPerSecond || nanos >= kNanosPerSecond) {
83-
seconds += nanos / kNanosPerSecond;
89+
int64_t seconds_offset = nanos / kNanosPerSecond;
90+
ABSL_DCHECK(!((seconds_offset > 0 &&
91+
seconds > TimeUtil::kTimestampMaxSeconds - seconds_offset) ||
92+
(seconds_offset < 0 &&
93+
seconds < TimeUtil::kTimestampMinSeconds - seconds_offset)))
94+
<< "Duration seconds overflow when normalizing nanos.";
95+
seconds += seconds_offset;
8496
nanos = nanos % kNanosPerSecond;
8597
}
8698
// nanos should have the same sign as seconds.

0 commit comments

Comments
 (0)