Skip to content

Commit b955118

Browse files
committed
Merge branch 'fix-interval-overflow' into maint-3.1
2 parents 4036168 + e42bd73 commit b955118

3 files changed

Lines changed: 22 additions & 14 deletions

File tree

‎docs/news.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Psycopg 3.1.18 (unreleased)
1414
^^^^^^^^^^^^^^^^^^^^^^^^^^^
1515

1616
- Fix possible deadlock on pipeline exit (:ticket:`685`).
17+
- Fix overflow loading large intervals in C module (:ticket:`719`).
1718

1819

1920
Current release

‎psycopg_c/psycopg_c/types/datetime.pyx‎

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Cython adapters for date/time types.
44

55
# Copyright (C) 2021 The Psycopg Team
66

7+
from libc.stdint cimport int64_t
78
from libc.string cimport memset, strchr
89
from cpython cimport datetime as cdt
910
from cpython.dict cimport PyDict_GetItem
@@ -391,7 +392,7 @@ cdef class DateLoader(CLoader):
391392
if length != 10:
392393
self._error_date(data, "unexpected length")
393394

394-
cdef int vals[3]
395+
cdef int64_t vals[3]
395396
memset(vals, 0, sizeof(vals))
396397

397398
cdef const char *ptr
@@ -437,7 +438,7 @@ cdef class TimeLoader(CLoader):
437438

438439
cdef object cload(self, const char *data, size_t length):
439440

440-
cdef int vals[3]
441+
cdef int64_t vals[3]
441442
memset(vals, 0, sizeof(vals))
442443
cdef const char *ptr
443444
cdef const char *end = data + length
@@ -494,7 +495,7 @@ cdef class TimetzLoader(CLoader):
494495

495496
cdef object cload(self, const char *data, size_t length):
496497

497-
cdef int vals[3]
498+
cdef int64_t vals[3]
498499
memset(vals, 0, sizeof(vals))
499500
cdef const char *ptr
500501
cdef const char *end = data + length
@@ -581,7 +582,7 @@ cdef class TimestampLoader(CLoader):
581582
if self._order == ORDER_PGDM or self._order == ORDER_PGMD:
582583
return self._cload_pg(data, end)
583584

584-
cdef int vals[6]
585+
cdef int64_t vals[6]
585586
memset(vals, 0, sizeof(vals))
586587
cdef const char *ptr
587588

@@ -611,7 +612,7 @@ cdef class TimestampLoader(CLoader):
611612
raise _get_timestamp_load_error(self._pgconn, data, ex) from None
612613

613614
cdef object _cload_pg(self, const char *data, const char *end):
614-
cdef int vals[4]
615+
cdef int64_t vals[4]
615616
memset(vals, 0, sizeof(vals))
616617
cdef const char *ptr
617618

@@ -721,7 +722,7 @@ cdef class TimestamptzLoader(_BaseTimestamptzLoader):
721722
if end[-1] == b'C': # ends with BC
722723
raise _get_timestamp_load_error(self._pgconn, data) from None
723724

724-
cdef int vals[6]
725+
cdef int64_t vals[6]
725726
memset(vals, 0, sizeof(vals))
726727

727728
# Parse the first 6 groups of digits (date and time)
@@ -862,9 +863,10 @@ cdef class IntervalLoader(CLoader):
862863
if self._style == INTERVALSTYLE_OTHERS:
863864
return self._cload_notimpl(data, length)
864865

865-
cdef int days = 0, secs = 0, us = 0
866+
cdef int days = 0, us = 0
867+
cdef int64_t secs = 0
866868
cdef char sign
867-
cdef int val
869+
cdef int64_t val
868870
cdef const char *ptr = data
869871
cdef const char *sep
870872
cdef const char *end = ptr + length
@@ -908,7 +910,7 @@ cdef class IntervalLoader(CLoader):
908910
break
909911

910912
# Parse the time part. An eventual sign was already consumed in the loop
911-
cdef int vals[3]
913+
cdef int64_t vals[3]
912914
memset(vals, 0, sizeof(vals))
913915
if ptr != NULL:
914916
ptr = _parse_date_values(ptr, end, vals, ARRAYSIZE(vals))
@@ -918,6 +920,10 @@ cdef class IntervalLoader(CLoader):
918920

919921
secs = vals[2] + 60 * (vals[1] + 60 * vals[0])
920922

923+
if secs > 86_400:
924+
days += secs // 86_400
925+
secs %= 86_400
926+
921927
if ptr[0] == b'.':
922928
ptr = _parse_micros(ptr + 1, &us)
923929

@@ -966,11 +972,11 @@ cdef class IntervalBinaryLoader(CLoader):
966972
# Work only with positive values as the cdivision behaves differently
967973
# with negative values, and cdivision=False adds overhead.
968974
cdef int64_t aval = val if val >= 0 else -val
969-
cdef int us, ussecs, usdays
975+
cdef int64_t us, ussecs, usdays
970976

971-
# Group the micros in biggers stuff or timedelta_new might overflow
977+
# Group the micros in bigger stuff or timedelta_new might overflow
972978
with cython.cdivision(True):
973-
ussecs = <int>(aval // 1_000_000)
979+
ussecs = <int64_t>(aval // 1_000_000)
974980
us = aval % 1_000_000
975981

976982
usdays = ussecs // 86_400
@@ -988,7 +994,7 @@ cdef class IntervalBinaryLoader(CLoader):
988994

989995

990996
cdef const char *_parse_date_values(
991-
const char *ptr, const char *end, int *vals, int nvals
997+
const char *ptr, const char *end, int64_t *vals, int nvals
992998
):
993999
"""
9941000
Parse *nvals* numeric values separated by non-numeric chars.
@@ -1046,7 +1052,7 @@ cdef int _parse_timezone_to_seconds(const char **bufptr, const char *end):
10461052
cdef char sgn = ptr[0]
10471053

10481054
# Parse at most three groups of digits
1049-
cdef int vals[3]
1055+
cdef int64_t vals[3]
10501056
memset(vals, 0, sizeof(vals))
10511057

10521058
ptr = _parse_date_values(ptr + 1, end, vals, ARRAYSIZE(vals))

‎tests/types/test_datetime.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ def test_dump_interval_binary(self, conn, val, expr):
713713
("-90d", "-3 month"),
714714
("186d", "6 mons 6 days"),
715715
("736d", "2 years 6 days"),
716+
("83063d,81640s,447000m", "1993534:40:40.447"),
716717
],
717718
)
718719
@pytest.mark.parametrize("fmt_out", pq.Format)

0 commit comments

Comments
 (0)