]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ldb: ldb_string_to_time reports more errors
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Wed, 14 Feb 2024 01:20:28 +0000 (14:20 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 29 Feb 2024 04:01:40 +0000 (04:01 +0000)
The underlying function should return -1 and set errno when given invalid
strings, but we were not looking and have decided on 0 for error.

It would be a pain to change this function to return -1. Apart from the
API fuss, it is sometimes used unchecked to set an unsigned number and
an unchecked 0 is better than UINT*_MAX in those contexts.

It is probably not easy to get an -1 from a timegm() -- most
implementations will happily convert overflows for you, so e.g. the
15th month would be March of the next year. But EOVERFLOW is mentioned
in the manpages.

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
lib/ldb/common/ldb_msg.c
lib/ldb/tests/python/api.py

index afddbe40ef6a6ec76404c5a26afcfdb34740157d..53f675ed18342af2c1e228ffac992d8d2050436b 100644 (file)
@@ -1597,6 +1597,7 @@ char *ldb_timestring(TALLOC_CTX *mem_ctx, time_t t)
 time_t ldb_string_to_time(const char *s)
 {
        struct tm tm;
+       time_t t;
 
        if (s == NULL) return 0;
 
@@ -1609,7 +1610,15 @@ time_t ldb_string_to_time(const char *s)
        tm.tm_year -= 1900;
        tm.tm_mon -= 1;
 
-       return timegm(&tm);
+       t = timegm(&tm);
+
+       if (t == (time_t)-1 && errno != 0) {
+               /*
+                * timegm() returns -1 on error, but also for '19691231235959.0Z'.
+                */
+               return 0;
+       }
+       return t;
 }
 
 /*
index 043b7213a49e9eb427eb719654138b45248ed3fc..77d848937ee39423605d54b280e2bd0ad2a199c7 100755 (executable)
@@ -59,6 +59,7 @@ class NoContextTests(TestCase):
 
     def test_string_to_time(self):
         self.assertEqual(0, ldb.string_to_time("19700101000000.0Z"))
+        self.assertEqual(-1, ldb.string_to_time("19691231235959.0Z"))
         self.assertEqual(1195499412, ldb.string_to_time("20071119191012.0Z"))
 
         self.assertEqual(-62167219200, ldb.string_to_time("00000101000000.0Z"))