1
0
mirror of https://github.com/samba-team/samba.git synced 2025-02-23 09:57:40 +03:00

pyldb: catch potential overflow error in py_timestring

Pair-Programmed-With: Björn Baumbach <bb@sernet.de>

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Signed-off-by: Björn Baumbach <bb@sernet.de>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
This commit is contained in:
Stefan Metzmacher 2021-01-19 16:53:55 +01:00 committed by Andrew Bartlett
parent fdc44a14e4
commit 71e8b24b8a
3 changed files with 27 additions and 0 deletions

View File

@ -1270,6 +1270,7 @@ char *ldb_timestring(TALLOC_CTX *mem_ctx, time_t t)
if (r != 17) {
talloc_free(ts);
errno = EOVERFLOW;
return NULL;
}

View File

@ -4192,6 +4192,13 @@ static PyObject *py_timestring(PyObject *module, PyObject *args)
if (!PyArg_ParseTuple(args, "l", &t_val))
return NULL;
tresult = ldb_timestring(NULL, (time_t) t_val);
if (tresult == NULL) {
/*
* Most likely EOVERFLOW from gmtime()
*/
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
ret = PyUnicode_FromString(tresult);
talloc_free(tresult);
return ret;

View File

@ -5,10 +5,12 @@
import os
from unittest import TestCase
import sys
sys.path.insert(0, "bin/python")
import gc
import time
import ldb
import shutil
import errno
TDB_PREFIX = "tdb://"
@ -41,10 +43,27 @@ class NoContextTests(TestCase):
self.assertEqual("19700101000000.0Z", ldb.timestring(0))
self.assertEqual("20071119191012.0Z", ldb.timestring(1195499412))
self.assertEqual("00000101000000.0Z", ldb.timestring(-62167219200))
self.assertEqual("99991231235959.0Z", ldb.timestring(253402300799))
# should result with OSError EOVERFLOW from gmtime()
with self.assertRaises(OSError) as err:
ldb.timestring(-62167219201)
self.assertEqual(err.exception.errno, errno.EOVERFLOW)
with self.assertRaises(OSError) as err:
ldb.timestring(253402300800)
self.assertEqual(err.exception.errno, errno.EOVERFLOW)
with self.assertRaises(OSError) as err:
ldb.timestring(0x7fffffffffffffff)
self.assertEqual(err.exception.errno, errno.EOVERFLOW)
def test_string_to_time(self):
self.assertEqual(0, ldb.string_to_time("19700101000000.0Z"))
self.assertEqual(1195499412, ldb.string_to_time("20071119191012.0Z"))
self.assertEqual(-62167219200, ldb.string_to_time("00000101000000.0Z"))
self.assertEqual(253402300799, ldb.string_to_time("99991231235959.0Z"))
def test_binary_encode(self):
encoded = ldb.binary_encode(b'test\\x')
decoded = ldb.binary_decode(encoded)