Skip to content

Commit 2c17536

Browse files
authored
Assert bounds of tmp->tm_mon to address scan-build warnings (#1178)
* Format bson-timegm.c * Add BSON_ASSERT of tmp->tm_mon to address scan-build warnings
1 parent 2127545 commit 2c17536

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

src/libbson/src/bson/bson-timegm.c

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#if !defined _Noreturn && \
3535
(!defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112)
3636
#if 2 < __GNUC__ + (8 <= __GNUC_MINOR__)
37-
#define _Noreturn __attribute__((__noreturn__))
37+
#define _Noreturn __attribute__ ((__noreturn__))
3838
#else
3939
#define _Noreturn
4040
#endif
@@ -181,7 +181,7 @@ struct ttinfo { /* time type information */
181181
};
182182

183183
struct lsinfo { /* leap second information */
184-
int64_t ls_trans; /* transition time */
184+
int64_t ls_trans; /* transition time */
185185
int_fast64_t ls_corr; /* correction to apply */
186186
};
187187

@@ -241,16 +241,22 @@ static int64_t
241241
normalize_overflow (int64_t *tensptr, int64_t *unitsptr, int64_t base);
242242
static int64_t
243243
time1 (struct bson_tm *tmp,
244-
struct bson_tm *(*funcp) (const int64_t *, int_fast32_t, struct bson_tm *),
244+
struct bson_tm *(*funcp) (const int64_t *,
245+
int_fast32_t,
246+
struct bson_tm *),
245247
int_fast32_t offset);
246248
static int64_t
247249
time2 (struct bson_tm *tmp,
248-
struct bson_tm *(*funcp) (const int64_t *, int_fast32_t, struct bson_tm *),
250+
struct bson_tm *(*funcp) (const int64_t *,
251+
int_fast32_t,
252+
struct bson_tm *),
249253
int_fast32_t offset,
250254
int64_t *okayp);
251255
static int64_t
252256
time2sub (struct bson_tm *tmp,
253-
struct bson_tm *(*funcp) (const int64_t *, int_fast32_t, struct bson_tm *),
257+
struct bson_tm *(*funcp) (const int64_t *,
258+
int_fast32_t,
259+
struct bson_tm *),
254260
int_fast32_t offset,
255261
int64_t *okayp,
256262
int64_t do_norm_secs);
@@ -434,8 +440,11 @@ timesub (const int64_t *const timep,
434440
*/
435441
tmp->tm_sec = (int64_t) (rem % SECSPERMIN) + hit;
436442
ip = mon_lengths[isleap (y)];
437-
for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
438-
idays -= ip[tmp->tm_mon];
443+
tmp->tm_mon = 0;
444+
while (idays >= ip[tmp->tm_mon]) {
445+
idays -= ip[tmp->tm_mon++];
446+
BSON_ASSERT (tmp->tm_mon < MONSPERYEAR);
447+
}
439448
tmp->tm_mday = (int64_t) (idays + 1);
440449
tmp->tm_isdst = 0;
441450
#ifdef TM_GMTOFF
@@ -490,7 +499,9 @@ increment_overflow32 (int_fast32_t *const lp, int64_t const m)
490499
}
491500

492501
static int64_t
493-
normalize_overflow (int64_t *const tensptr, int64_t *const unitsptr, const int64_t base)
502+
normalize_overflow (int64_t *const tensptr,
503+
int64_t *const unitsptr,
504+
const int64_t base)
494505
{
495506
register int64_t tensdelta;
496507

@@ -531,7 +542,9 @@ tmcomp (register const struct bson_tm *const atmp,
531542

532543
static int64_t
533544
time2sub (struct bson_tm *const tmp,
534-
struct bson_tm *(*const funcp) (const int64_t *, int_fast32_t, struct bson_tm *),
545+
struct bson_tm *(*const funcp) (const int64_t *,
546+
int_fast32_t,
547+
struct bson_tm *),
535548
const int_fast32_t offset,
536549
int64_t *const okayp,
537550
const int64_t do_norm_secs)
@@ -700,7 +713,9 @@ time2sub (struct bson_tm *const tmp,
700713

701714
static int64_t
702715
time2 (struct bson_tm *const tmp,
703-
struct bson_tm *(*const funcp) (const int64_t *, int_fast32_t, struct bson_tm *),
716+
struct bson_tm *(*const funcp) (const int64_t *,
717+
int_fast32_t,
718+
struct bson_tm *),
704719
const int_fast32_t offset,
705720
int64_t *const okayp)
706721
{
@@ -717,7 +732,9 @@ time2 (struct bson_tm *const tmp,
717732

718733
static int64_t
719734
time1 (struct bson_tm *const tmp,
720-
struct bson_tm *(*const funcp) (const int64_t *, int_fast32_t, struct bson_tm *),
735+
struct bson_tm *(*const funcp) (const int64_t *,
736+
int_fast32_t,
737+
struct bson_tm *),
721738
const int_fast32_t offset)
722739
{
723740
register int64_t t;
@@ -792,4 +809,3 @@ _bson_timegm (struct bson_tm *const tmp)
792809
tmp->tm_isdst = 0;
793810
return time1 (tmp, gmtsub, 0L);
794811
}
795-

0 commit comments

Comments
 (0)