summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2017-02-24 02:10:27 +0100
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2017-02-24 02:10:27 +0100
commit14fe3ad8c9f9e9ecf63e352b23dafe9da22a80ab (patch)
treea45836f011c6f09152949b86b3d19ea97f0fec81
parentf5bd6063fc16c4f008716833dc1209796caf39d8 (diff)
downloadpsycopg2-14fe3ad8c9f9e9ecf63e352b23dafe9da22a80ab.tar.gz
Fixed integer overflow in interval typecaster
Close #512.
-rw-r--r--NEWS1
-rw-r--r--psycopg/typecast_datetime.c8
-rwxr-xr-xtests/test_dates.py4
3 files changed, 9 insertions, 4 deletions
diff --git a/NEWS b/NEWS
index 52868e9..8fc14cd 100644
--- a/NEWS
+++ b/NEWS
@@ -47,6 +47,7 @@ Bug fixes:
- Fixed error caused by missing decoding `~psycopg2.extras.LoggingConnection`
(:ticket:`#483`).
+- Fixed integer overflow in :sql:`interval` seconds (:ticket:`#512`).
Other changes:
diff --git a/psycopg/typecast_datetime.c b/psycopg/typecast_datetime.c
index ad74101..6b94175 100644
--- a/psycopg/typecast_datetime.c
+++ b/psycopg/typecast_datetime.c
@@ -220,10 +220,10 @@ typecast_PYTIME_cast(const char *str, Py_ssize_t len, PyObject *curs)
static PyObject *
typecast_PYINTERVAL_cast(const char *str, Py_ssize_t len, PyObject *curs)
{
- long years = 0, months = 0, days = 0;
+ long years = 0, months = 0, days = 0, sec;
double hours = 0.0, minutes = 0.0, seconds = 0.0, hundredths = 0.0;
double v = 0.0, sign = 1.0, denominator = 1.0;
- int part = 0, sec;
+ int part = 0;
double micro;
if (str == NULL) { Py_RETURN_NONE; }
@@ -318,8 +318,8 @@ typecast_PYINTERVAL_cast(const char *str, Py_ssize_t len, PyObject *curs)
days += years*365 + months*30;
micro = (seconds - floor(seconds)) * 1000000.0;
- sec = (int)floor(seconds);
- return PyObject_CallFunction((PyObject*)PyDateTimeAPI->DeltaType, "iii",
+ sec = (long)floor(seconds);
+ return PyObject_CallFunction((PyObject*)PyDateTimeAPI->DeltaType, "lli",
days, sec, (int)round(micro));
}
diff --git a/tests/test_dates.py b/tests/test_dates.py
index 3463d00..8bb03c7 100755
--- a/tests/test_dates.py
+++ b/tests/test_dates.py
@@ -333,6 +333,10 @@ class DatetimeTests(ConnectingTestCase, CommonDatetimeTestsMixin):
t = self.execute("select '24:00+05:30'::timetz;")
self.assertEqual(t, time(0, 0, tzinfo=FixedOffsetTimezone(330)))
+ def test_large_interval(self):
+ t = self.execute("select '999999:00:00'::interval")
+ self.assertEqual(t.days * 24 + t.seconds / 60.0 / 60, 999999)
+
# Only run the datetime tests if psycopg was compiled with support.
if not hasattr(psycopg2.extensions, 'PYDATETIME'):