From 70d31c1a92a3832eea5b11d0f2b30d65f99b5772 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 12 Jan 2023 12:21:09 -0600 Subject: [PATCH 1/4] Format bson-timegm.c --- src/libbson/src/bson/bson-timegm.c | 33 +++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/libbson/src/bson/bson-timegm.c b/src/libbson/src/bson/bson-timegm.c index 0f7090073f2..7475a88a290 100644 --- a/src/libbson/src/bson/bson-timegm.c +++ b/src/libbson/src/bson/bson-timegm.c @@ -34,7 +34,7 @@ #if !defined _Noreturn && \ (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112) #if 2 < __GNUC__ + (8 <= __GNUC_MINOR__) -#define _Noreturn __attribute__((__noreturn__)) +#define _Noreturn __attribute__ ((__noreturn__)) #else #define _Noreturn #endif @@ -181,7 +181,7 @@ struct ttinfo { /* time type information */ }; struct lsinfo { /* leap second information */ - int64_t ls_trans; /* transition time */ + int64_t ls_trans; /* transition time */ int_fast64_t ls_corr; /* correction to apply */ }; @@ -241,16 +241,22 @@ static int64_t normalize_overflow (int64_t *tensptr, int64_t *unitsptr, int64_t base); static int64_t time1 (struct bson_tm *tmp, - struct bson_tm *(*funcp) (const int64_t *, int_fast32_t, struct bson_tm *), + struct bson_tm *(*funcp) (const int64_t *, + int_fast32_t, + struct bson_tm *), int_fast32_t offset); static int64_t time2 (struct bson_tm *tmp, - struct bson_tm *(*funcp) (const int64_t *, int_fast32_t, struct bson_tm *), + struct bson_tm *(*funcp) (const int64_t *, + int_fast32_t, + struct bson_tm *), int_fast32_t offset, int64_t *okayp); static int64_t time2sub (struct bson_tm *tmp, - struct bson_tm *(*funcp) (const int64_t *, int_fast32_t, struct bson_tm *), + struct bson_tm *(*funcp) (const int64_t *, + int_fast32_t, + struct bson_tm *), int_fast32_t offset, int64_t *okayp, int64_t do_norm_secs); @@ -490,7 +496,9 @@ increment_overflow32 (int_fast32_t *const lp, int64_t const m) } static int64_t -normalize_overflow (int64_t *const tensptr, int64_t *const unitsptr, const int64_t base) +normalize_overflow (int64_t *const tensptr, + int64_t *const unitsptr, + const int64_t base) { register int64_t tensdelta; @@ -531,7 +539,9 @@ tmcomp (register const struct bson_tm *const atmp, static int64_t time2sub (struct bson_tm *const tmp, - struct bson_tm *(*const funcp) (const int64_t *, int_fast32_t, struct bson_tm *), + struct bson_tm *(*const funcp) (const int64_t *, + int_fast32_t, + struct bson_tm *), const int_fast32_t offset, int64_t *const okayp, const int64_t do_norm_secs) @@ -700,7 +710,9 @@ time2sub (struct bson_tm *const tmp, static int64_t time2 (struct bson_tm *const tmp, - struct bson_tm *(*const funcp) (const int64_t *, int_fast32_t, struct bson_tm *), + struct bson_tm *(*const funcp) (const int64_t *, + int_fast32_t, + struct bson_tm *), const int_fast32_t offset, int64_t *const okayp) { @@ -717,7 +729,9 @@ time2 (struct bson_tm *const tmp, static int64_t time1 (struct bson_tm *const tmp, - struct bson_tm *(*const funcp) (const int64_t *, int_fast32_t, struct bson_tm *), + struct bson_tm *(*const funcp) (const int64_t *, + int_fast32_t, + struct bson_tm *), const int_fast32_t offset) { register int64_t t; @@ -792,4 +806,3 @@ _bson_timegm (struct bson_tm *const tmp) tmp->tm_isdst = 0; return time1 (tmp, gmtsub, 0L); } - From 8d44c663c67c30c8fc1ad8a1ac1026f16729b776 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 12 Jan 2023 12:21:25 -0600 Subject: [PATCH 2/4] Add bound to tmp->tm_mon to address scan-build warnings --- src/libbson/src/bson/bson-timegm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libbson/src/bson/bson-timegm.c b/src/libbson/src/bson/bson-timegm.c index 7475a88a290..818d049c104 100644 --- a/src/libbson/src/bson/bson-timegm.c +++ b/src/libbson/src/bson/bson-timegm.c @@ -440,7 +440,8 @@ timesub (const int64_t *const timep, */ tmp->tm_sec = (int64_t) (rem % SECSPERMIN) + hit; ip = mon_lengths[isleap (y)]; - for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon)) + for (tmp->tm_mon = 0; tmp->tm_mon < MONSPERYEAR && idays >= ip[tmp->tm_mon]; + ++(tmp->tm_mon)) idays -= ip[tmp->tm_mon]; tmp->tm_mday = (int64_t) (idays + 1); tmp->tm_isdst = 0; From 39ef18326749cf6e414a7f1e2c8f474fcc954c44 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 12 Jan 2023 12:38:41 -0600 Subject: [PATCH 3/4] Use BSON_ASSERT to express precondition instead --- src/libbson/src/bson/bson-timegm.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/libbson/src/bson/bson-timegm.c b/src/libbson/src/bson/bson-timegm.c index 818d049c104..ba14c5b1dc6 100644 --- a/src/libbson/src/bson/bson-timegm.c +++ b/src/libbson/src/bson/bson-timegm.c @@ -440,9 +440,11 @@ timesub (const int64_t *const timep, */ tmp->tm_sec = (int64_t) (rem % SECSPERMIN) + hit; ip = mon_lengths[isleap (y)]; - for (tmp->tm_mon = 0; tmp->tm_mon < MONSPERYEAR && idays >= ip[tmp->tm_mon]; - ++(tmp->tm_mon)) - idays -= ip[tmp->tm_mon]; + tmp->tm_mon = 0; + while (idays >= ip[tmp->tm_mon]) { + BSON_ASSERT (tmp->tm_mon < MONSPERYEAR); + idays -= ip[tmp->tm_mon++]; + } tmp->tm_mday = (int64_t) (idays + 1); tmp->tm_isdst = 0; #ifdef TM_GMTOFF From 2d9088da0c9ffe47d931fc151d02cd9ed546248d Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 12 Jan 2023 12:42:53 -0600 Subject: [PATCH 4/4] Fix placement of assertion to come after increment --- src/libbson/src/bson/bson-timegm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libbson/src/bson/bson-timegm.c b/src/libbson/src/bson/bson-timegm.c index ba14c5b1dc6..01f88e7f3c9 100644 --- a/src/libbson/src/bson/bson-timegm.c +++ b/src/libbson/src/bson/bson-timegm.c @@ -442,8 +442,8 @@ timesub (const int64_t *const timep, ip = mon_lengths[isleap (y)]; tmp->tm_mon = 0; while (idays >= ip[tmp->tm_mon]) { - BSON_ASSERT (tmp->tm_mon < MONSPERYEAR); idays -= ip[tmp->tm_mon++]; + BSON_ASSERT (tmp->tm_mon < MONSPERYEAR); } tmp->tm_mday = (int64_t) (idays + 1); tmp->tm_isdst = 0;