Skip to content

Assert bounds of tmp->tm_mon to address scan-build warnings #1178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions src/libbson/src/bson/bson-timegm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */
};

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -434,8 +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; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
idays -= ip[tmp->tm_mon];
tmp->tm_mon = 0;
while (idays >= ip[tmp->tm_mon]) {
idays -= ip[tmp->tm_mon++];
BSON_ASSERT (tmp->tm_mon < MONSPERYEAR);
}
tmp->tm_mday = (int64_t) (idays + 1);
tmp->tm_isdst = 0;
#ifdef TM_GMTOFF
Expand Down Expand Up @@ -490,7 +499,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;

Expand Down Expand Up @@ -531,7 +542,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)
Expand Down Expand Up @@ -700,7 +713,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)
{
Expand All @@ -717,7 +732,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;
Expand Down Expand Up @@ -792,4 +809,3 @@ _bson_timegm (struct bson_tm *const tmp)
tmp->tm_isdst = 0;
return time1 (tmp, gmtsub, 0L);
}