Skip to content

Commit 7637fbc

Browse files
committed
Reject out-of-range int inputs for timedelta
Backport of: #13973
1 parent dfdbfcb commit 7637fbc

2 files changed

Lines changed: 33 additions & 14 deletions

File tree

‎pydantic-core/src/input/datetime.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,10 @@ pub fn bytes_as_timedelta<'py>(
709709
pub fn int_as_duration(input: impl ToErrorValue, total_seconds: i64) -> ValResult<Duration> {
710710
let positive = total_seconds >= 0;
711711
let total_seconds = total_seconds.unsigned_abs();
712-
// we can safely unwrap here since we've guaranteed seconds and microseconds can't cause overflow
713-
let days = (total_seconds / 86400) as u32;
712+
// The number of days can exceed `u32::MAX` (and casting would silently wrap it):
713+
let Ok(days) = u32::try_from(total_seconds / 86400) else {
714+
return Err(map_timedelta_err(input, ParseError::DurationDaysTooLarge));
715+
};
714716
let seconds = (total_seconds % 86400) as u32;
715717
Duration::new(positive, days, seconds, 0).map_err(|err| map_timedelta_err(input, err))
716718
}

‎tests/test_datetime.py‎

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,9 @@ class Model(BaseModel):
355355
]
356356

357357

358-
@pytest.fixture(scope='module', name='TimedeltaModel')
359-
def timedelta_model_fixture():
360-
class TimedeltaModel(BaseModel):
361-
d: timedelta
362-
363-
return TimedeltaModel
358+
@pytest.fixture(scope='module', name='timedelta_ta')
359+
def timedelta_ta_fixture() -> TypeAdapter[timedelta]:
360+
return TypeAdapter(timedelta)
364361

365362

366363
@pytest.mark.parametrize(
@@ -375,9 +372,9 @@ class TimedeltaModel(BaseModel):
375372
timedelta(seconds=30), # seconds
376373
],
377374
)
378-
def test_parse_python_format(TimedeltaModel, delta):
379-
assert TimedeltaModel(d=delta).d == delta
380-
# assert TimedeltaModel(d=str(delta)).d == delta
375+
def test_parse_python_format(timedelta_ta: TypeAdapter[timedelta], delta):
376+
assert timedelta_ta.validate_python(delta) == delta
377+
# assert timedelta_ta.validate_python(str(delta)) == delta
381378

382379

383380
@pytest.mark.parametrize(
@@ -426,12 +423,32 @@ def test_parse_python_format(TimedeltaModel, delta):
426423
(b'PT0.000005S', timedelta(microseconds=5)),
427424
],
428425
)
429-
def test_parse_durations(TimedeltaModel, value, result):
426+
def test_parse_durations(timedelta_ta: TypeAdapter[timedelta], value, result):
430427
if isinstance(result, Err):
431428
with pytest.raises(ValidationError, match=result.message_escaped()):
432-
TimedeltaModel(d=value)
429+
timedelta_ta.validate_python(value)
433430
else:
434-
assert TimedeltaModel(d=value).d == result
431+
assert timedelta_ta.validate_python(value) == result
432+
433+
434+
@pytest.mark.parametrize(
435+
'value',
436+
[
437+
999_999_999 * 86400 + 86400,
438+
86400 * 2**32,
439+
86400 * (2**32 + 5),
440+
-86400 * (2**32 + 5),
441+
],
442+
)
443+
def test_parse_durations_large_int(timedelta_ta: TypeAdapter[timedelta], value: int) -> None:
444+
"""https://github.com/pydantic/pydantic/issues/13969"""
445+
446+
assert timedelta_ta.validate_python(999_999_999 * 86400) == timedelta(days=999_999_999)
447+
448+
with pytest.raises(ValidationError, match='durations may not exceed 999,999,999 days'):
449+
timedelta_ta.validate_python(value)
450+
with pytest.raises(ValidationError, match='durations may not exceed 999,999,999 days'):
451+
timedelta_ta.validate_json(str(value))
435452

436453

437454
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)