8000 Fix overflow in unpacking timestamp to datetime (#452) · realsby/msgpack-python@753b370 · GitHub
[go: up one dir, main page]

Skip to content

Commit 753b370

Browse files
authored
Fix overflow in unpacking timestamp to datetime (msgpack#452)
1 parent 8029f95 commit 753b370

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

msgpack/unpack.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,21 +342,21 @@ static int unpack_callback_ext(unpack_user* u, const char* base, const char* pos
342342
py = PyObject_CallFunction(u->timestamp_t, "(Lk)", ts.tv_sec, ts.tv_nsec);
343343
}
344344
else if (u->timestamp == 3) { // datetime
345-
// Calculate datetime using epoch + delta
345+
// Calculate datetime using epoch + delta
346346
// due to limitations PyDateTime_FromTimestamp on Windows with negative timestamps
347347
PyObject *epoch = PyDateTimeAPI->DateTime_FromDateAndTime(1970, 1, 1, 0, 0, 0, 0, u->utc, PyDateTimeAPI->DateTimeType);
348348
if (epoch == NULL) {
349349
return -1;
350350
}
351351

352-
PyObject* d = PyDelta_FromDSU(0, ts.tv_sec, ts.tv_nsec / 1000);
352+
PyObject* d = PyDelta_FromDSU(ts.tv_sec/(24*3600), ts.tv_sec%(24*3600), ts.tv_nsec / 1000);
353353
if (d == NULL) {
354354
Py_DECREF(epoch);
355355
return -1;
356356
}
357357

358358
py = PyNumber_Add(epoch, d);
359-
359+
360360
Py_DECREF(epoch);
361361
Py_DECREF(d);
362362
}

test/test_timestamp.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,14 @@ def test_pack_datetime():
129129
assert x
130130
assert x[0] == dt
131131
assert msgpack.unpackb(packed) is None
132+
133+
134+
@pytest.mark.skipif(sys.version_info[0] == 2, reason="datetime support is PY3+ only")
135+
def test_issue451():
136+
# https://github.com/msgpack/msgpack-python/issues/451
137+
dt = datetime.datetime(2100, 1, 1, 1, 1, tzinfo=_utc)
138+
packed = msgpack.packb(dt, datetime=True)
139+
assert packed == b"\xd6\xff\xf4\x86eL"
140+
141+
unpacked = msgpack.unpackb(packed, timestamp=3)
142+
assert dt == unpacked

0 commit comments

Comments
 (0)
0