diff --git a/src/libbson/CMakeLists.txt b/src/libbson/CMakeLists.txt index d6df66f8657..8aa6fbc3ded 100644 --- a/src/libbson/CMakeLists.txt +++ b/src/libbson/CMakeLists.txt @@ -210,6 +210,13 @@ set (HEADERS_FORWARDING ) add_library (bson_shared SHARED ${SOURCES} ${HEADERS} ${HEADERS_FORWARDING}) +if (MSVC AND MSVC_VERSION VERSION_LESS 1900) + message (STATUS "Disabling warning C4756 for VS 2013 and older") + # Macro constant INFINITY triggers constant arithmetic overflow warnings in + # VS 2013, but VS 2013 doesn't support inline warning suppression. + # Remove once support for VS 2013 is dropped. + target_compile_options(bson_shared PRIVATE /wd4756) +endif () set (CMAKE_CXX_VISIBILITY_PRESET hidden) target_compile_definitions (bson_shared PRIVATE @@ -278,6 +285,13 @@ endif () if (MONGOC_ENABLE_STATIC_BUILD) add_library (bson_static STATIC ${SOURCES} ${HEADERS} ${HEADERS_FORWARDING}) + if (MSVC AND MSVC_VERSION VERSION_LESS 1900) + message (STATUS "Disabling warning C4756 for VS 2013 and older") + # Macro constant INFINITY triggers constant arithmetic overflow warnings in + # VS 2013, but VS 2013 doesn't support inline warning suppression. + # Remove once support for VS 2013 is dropped. + target_compile_options(bson_static PRIVATE /wd4756) + endif () target_compile_definitions (bson_static PUBLIC BSON_STATIC diff --git a/src/libbson/examples/bson-check-depth.c b/src/libbson/examples/bson-check-depth.c index bd0f734f138..032001a32d8 100644 --- a/src/libbson/examples/bson-check-depth.c +++ b/src/libbson/examples/bson-check-depth.c @@ -19,12 +19,13 @@ #include #include +#include #include #include typedef struct { uint32_t depth; - int max_depth; + uint32_t max_depth; bool valid; } check_depth_t; @@ -74,7 +75,7 @@ _check_depth_document (const bson_iter_t *iter, } void -check_depth (const bson_t *bson, int max_depth) +check_depth (const bson_t *bson, uint32_t max_depth) { bson_iter_t iter; check_depth_t state = {0}; @@ -87,7 +88,8 @@ check_depth (const bson_t *bson, int max_depth) state.max_depth = max_depth; _check_depth_document (&iter, NULL, bson, &state); if (!state.valid) { - printf ("document exceeds maximum depth of %d\n", state.max_depth); + printf ("document exceeds maximum depth of %" PRIu32 "\n", + state.max_depth); } else { char *as_json = bson_as_canonical_extended_json (bson, NULL); printf ("document %s ", as_json); @@ -102,9 +104,7 @@ main (int argc, char **argv) bson_reader_t *bson_reader; const bson_t *bson; bool reached_eof; - char *filename; bson_error_t error; - int max_depth; if (argc != 3) { fprintf (stderr, "usage: %s FILE MAX_DEPTH\n", argv[0]); @@ -112,16 +112,19 @@ main (int argc, char **argv) fprintf (stderr, "does not exceed MAX_DEPTH\n"); } - filename = argv[1]; - max_depth = atoi (argv[2]); + const char *const filename = argv[1]; + const int max_depth = atoi (argv[2]); + bson_reader = bson_reader_new_from_file (filename, &error); if (!bson_reader) { printf ("could not read %s: %s\n", filename, error.message); return 1; } + BSON_ASSERT (bson_in_range_signed (uint32_t, max_depth)); + while ((bson = bson_reader_read (bson_reader, &reached_eof))) { - check_depth (bson, max_depth); + check_depth (bson, (uint32_t) max_depth); } if (!reached_eof) { diff --git a/src/libbson/examples/bson-metrics.c b/src/libbson/examples/bson-metrics.c index a1cac482d47..2cd4acd6dc8 100644 --- a/src/libbson/examples/bson-metrics.c +++ b/src/libbson/examples/bson-metrics.c @@ -81,8 +81,12 @@ static bson_metrics_state_t state; static int compar_bson_type_metrics (const void *a, const void *b) { - return (((bson_type_metrics_t *) b)->count - - ((bson_type_metrics_t *) a)->count); + const uint64_t a_count = ((bson_type_metrics_t *) a)->count; + const uint64_t b_count = ((bson_type_metrics_t *) b)->count; + if (a_count == b_count) { + return 0; + } + return a_count < b_count ? -1 : 1; } /* diff --git a/src/libbson/src/bson/bson-decimal128.c b/src/libbson/src/bson/bson-decimal128.c index 9f4e9639f74..9293d19cbf1 100644 --- a/src/libbson/src/bson/bson-decimal128.c +++ b/src/libbson/src/bson/bson-decimal128.c @@ -19,6 +19,7 @@ #include #include +#include "bson-cmp.h" #include "bson-decimal128.h" #include "bson-types.h" #include "bson-macros.h" @@ -155,8 +156,6 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */ uint8_t significand_msb; /* the most signifcant significand bits (50-46) */ _bson_uint128_t significand128; /* temporary storage for significand decoding */ - size_t i; /* indexing variables */ - int j, k; memset (significand_str, 0, sizeof (significand_str)); @@ -212,7 +211,7 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */ */ is_zero = true; } else { - for (k = 3; k >= 0; k--) { + for (int k = 3; k >= 0; k--) { uint32_t least_digits = 0; _bson_uint128_divide1B ( significand128, &significand128, &least_digits); @@ -223,7 +222,7 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */ continue; } - for (j = 8; j >= 0; j--) { + for (int j = 8; j >= 0; j--) { significand[k * 9 + j] = least_digits % 10; least_digits /= 10; } @@ -264,7 +263,8 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */ *(str_out++) = '.'; } - for (i = 0; i < significand_digits && (str_out - str) < 36; i++) { + for (uint32_t i = 0; i < significand_digits && (str_out - str) < 36; + i++) { *(str_out++) = *(significand_read++) + '0'; } /* Exponent */ @@ -273,7 +273,8 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */ } else { /* Regular format with no decimal place */ if (exponent >= 0) { - for (i = 0; i < significand_digits && (str_out - str) < 36; i++) { + for (uint32_t i = 0; i < significand_digits && (str_out - str) < 36; + i++) { *(str_out++) = *(significand_read++) + '0'; } *str_out = '\0'; @@ -281,7 +282,7 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */ int32_t radix_position = significand_digits + exponent; if (radix_position > 0) { /* non-zero digits before radix */ - for (i = 0; + for (int32_t i = 0; i < radix_position && (str_out - str) < BSON_DECIMAL128_STRING; i++) { *(str_out++) = *(significand_read++) + '0'; @@ -295,8 +296,9 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */ *(str_out++) = '0'; } - for (i = 0; - (i < significand_digits - BSON_MAX (radix_position - 1, 0)) && + for (uint32_t i = 0; + bson_cmp_greater_us (significand_digits - i, + BSON_MAX (radix_position - 1, 0)) && (str_out - str) < BSON_DECIMAL128_STRING; i++) { *(str_out++) = *(significand_read++) + '0'; @@ -623,10 +625,12 @@ bson_decimal128_from_string_w_len (const char *string, /* IN */ /* to represent user input */ /* Overflow prevention */ - if (exponent <= radix_position && radix_position - exponent > (1 << 14)) { + if (bson_cmp_less_equal_su (exponent, radix_position) && + bson_cmp_greater_us (radix_position, exponent + (1 << 14))) { exponent = BSON_DECIMAL128_EXPONENT_MIN; } else { - exponent -= radix_position; + BSON_ASSERT (bson_in_range_unsigned (int32_t, radix_position)); + exponent -= (int32_t) radix_position; } /* Attempt to normalize the exponent */ diff --git a/src/libbson/src/bson/bson-iter.c b/src/libbson/src/bson/bson-iter.c index 7b4630f94c3..00aa5c5d1d3 100644 --- a/src/libbson/src/bson/bson-iter.c +++ b/src/libbson/src/bson/bson-iter.c @@ -110,8 +110,13 @@ bson_iter_init_from_data (bson_iter_t *iter, /* OUT */ return false; } + if (BSON_UNLIKELY (!bson_in_range_unsigned (uint32_t, length))) { + memset (iter, 0, sizeof *iter); + return false; + } + iter->raw = (uint8_t *) data; - iter->len = length; + iter->len = (uint32_t) length; iter->off = 0; iter->type = 0; iter->key = 0; diff --git a/src/libbson/src/bson/bson-json.c b/src/libbson/src/bson/bson-json.c index e1f0fa72a9e..28f36abe948 100644 --- a/src/libbson/src/bson/bson-json.c +++ b/src/libbson/src/bson/bson-json.c @@ -628,7 +628,13 @@ _bson_json_read_integer (bson_json_reader_t *reader, uint64_t val, int64_t sign) bson_append_int32 ( STACK_BSON_CHILD, key, (int) len, (int) (val * sign)); } else if (sign == -1) { +#if defined(_WIN32) && !defined(__MINGW32__) + // Unary negation of unsigned integer is deliberate. +#pragma warning(suppress : 4146) bson_append_int64 (STACK_BSON_CHILD, key, (int) len, (int64_t) -val); +#else + bson_append_int64 (STACK_BSON_CHILD, key, (int) len, (int64_t) -val); +#endif // defined(_WIN32) && !defined(__MINGW32__) } else { bson_append_int64 (STACK_BSON_CHILD, key, (int) len, (int64_t) val); } @@ -735,33 +741,19 @@ _bson_json_parse_double (bson_json_reader_t *reader, *d = strtod (val, NULL); #ifdef _MSC_VER + const double pos_inf = INFINITY; + const double neg_inf = -pos_inf; + /* Microsoft's strtod parses "NaN", "Infinity", "-Infinity" as 0 */ if (*d == 0.0) { if (!_strnicmp (val, "nan", vlen)) { -#ifdef NAN *d = NAN; -#else - /* Visual Studio 2010 doesn't define NAN or INFINITY - * https://msdn.microsoft.com/en-us/library/w22adx1s(v=vs.100).aspx */ - unsigned long nan[2] = {0xffffffff, 0x7fffffff}; - *d = *(double *) nan; -#endif return true; } else if (!_strnicmp (val, "infinity", vlen)) { -#ifdef INFINITY - *d = INFINITY; -#else - unsigned long inf[2] = {0x00000000, 0x7ff00000}; - *d = *(double *) inf; -#endif + *d = pos_inf; return true; } else if (!_strnicmp (val, "-infinity", vlen)) { -#ifdef INFINITY - *d = -INFINITY; -#else - unsigned long inf[2] = {0x00000000, 0xfff00000}; - *d = *(double *) inf; -#endif + *d = neg_inf; return true; } } @@ -831,7 +823,7 @@ static bool _unhexlify_uuid (const char *uuid, uint8_t *out, size_t max) { unsigned int byte; - int x = 0; + size_t x = 0; int i = 0; BSON_ASSERT (strlen (uuid) == 32); @@ -1186,8 +1178,9 @@ _bson_json_read_start_map (bson_json_reader_t *reader) /* IN */ * expected a legacy Binary format. now we see the second "{", so * backtrack and parse $type query operator. */ bson->read_state = BSON_JSON_IN_START_MAP; + BSON_ASSERT (bson_in_range_unsigned (int, len)); STACK_PUSH_DOC (bson_append_document_begin ( - STACK_BSON_PARENT, key, len, STACK_BSON_CHILD)); + STACK_BSON_PARENT, key, (int) len, STACK_BSON_CHILD)); _bson_json_save_map_key (bson, (const uint8_t *) "$type", 5); break; case BSON_JSON_LF_CODE: @@ -2233,8 +2226,11 @@ bson_json_reader_read (bson_json_reader_t *reader, /* IN */ /* accumulate a key or string value */ if (reader->json_text_pos != -1) { - if (reader->json_text_pos < reader->json->pos) { - accum = BSON_MIN (reader->json->pos - reader->json_text_pos, r); + if (bson_cmp_less_su (reader->json_text_pos, reader->json->pos)) { + BSON_ASSERT ( + bson_in_range_unsigned (ssize_t, reader->json->pos)); + accum = BSON_MIN ( + (ssize_t) reader->json->pos - reader->json_text_pos, r); /* if this chunk stopped mid-token, buf_offset is how far into * our current chunk the token begins. */ buf_offset = AT_LEAST_0 (reader->json_text_pos - start_pos); diff --git a/src/libbson/src/bson/bson-memory.c b/src/libbson/src/bson/bson-memory.c index 2b6fef7041e..e1aaea69a24 100644 --- a/src/libbson/src/bson/bson-memory.c +++ b/src/libbson/src/bson/bson-memory.c @@ -40,7 +40,10 @@ _aligned_alloc_impl (size_t alignment, size_t num_bytes) #elif defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L { void *mem = NULL; - (void) posix_memalign (&mem, alignment, num_bytes); + + // Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425. + BSON_MAYBE_UNUSED int ret = posix_memalign (&mem, alignment, num_bytes); + return mem; } #else diff --git a/src/libbson/src/bson/bson-timegm.c b/src/libbson/src/bson/bson-timegm.c index a7838e7c159..a8f20cff37a 100644 --- a/src/libbson/src/bson/bson-timegm.c +++ b/src/libbson/src/bson/bson-timegm.c @@ -225,47 +225,53 @@ struct rule { */ static void -gmtload (struct state *sp); +gmtload (struct state *const sp); static struct bson_tm * -gmtsub (const int64_t *timep, int_fast32_t offset, struct bson_tm *tmp); +gmtsub (const int64_t *const timep, + const int_fast32_t offset, + struct bson_tm *const tmp); static int64_t -increment_overflow (int64_t *number, int64_t delta); +increment_overflow (int64_t *const ip, int64_t j); static int64_t -leaps_thru_end_of (int64_t y) ATTRIBUTE_PURE; +leaps_thru_end_of (const int64_t y) ATTRIBUTE_PURE; static int64_t -increment_overflow32 (int_fast32_t *number, int64_t delta); +increment_overflow32 (int_fast32_t *const lp, int64_t const m); static int64_t -normalize_overflow32 (int_fast32_t *tensptr, int64_t *unitsptr, int64_t base); +normalize_overflow32 (int_fast32_t *const tensptr, + int64_t *const unitsptr, + const int64_t base); static int64_t -normalize_overflow (int64_t *tensptr, int64_t *unitsptr, int64_t base); +normalize_overflow (int64_t *const tensptr, + int64_t *const unitsptr, + const int64_t base); static int64_t -time1 (struct bson_tm *tmp, - struct bson_tm *(*funcp) (const int64_t *, - int_fast32_t, - struct bson_tm *), - int_fast32_t offset); +time1 (struct bson_tm *const tmp, + struct bson_tm *(*const funcp) (const int64_t *, + int_fast32_t, + struct bson_tm *), + const 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 *), - int_fast32_t offset, - int64_t *okayp); +time2 (struct bson_tm *const tmp, + struct bson_tm *(*const funcp) (const int64_t *, + int_fast32_t, + struct bson_tm *), + const int_fast32_t offset, + int64_t *const okayp); static int64_t -time2sub (struct bson_tm *tmp, - 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); +time2sub (struct bson_tm *const tmp, + 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); static struct bson_tm * -timesub (const int64_t *timep, - int_fast32_t offset, - const struct state *sp, - struct bson_tm *tmp); +timesub (const int64_t *const timep, + const int_fast32_t offset, + const struct state *const sp, + struct bson_tm *const tmp); static int64_t -tmcomp (const struct bson_tm *atmp, const struct bson_tm *btmp); +tmcomp (const struct bson_tm *const atmp, const struct bson_tm *const btmp); static struct state gmtmem; #define gmtptr (&gmtmem) @@ -298,7 +304,7 @@ gmtsub (const int64_t *const timep, const int_fast32_t offset, struct bson_tm *const tmp) { - register struct bson_tm *result; + struct bson_tm *result; if (!gmt_is_set) { gmt_is_set = true; @@ -322,7 +328,7 @@ gmtsub (const int64_t *const timep, */ static int64_t -leaps_thru_end_of (register const int64_t y) +leaps_thru_end_of (const int64_t y) { return (y >= 0) ? (y / 4 - y / 100 + y / 400) : -(leaps_thru_end_of (-(y + 1)) + 1); @@ -331,18 +337,18 @@ leaps_thru_end_of (register const int64_t y) static struct bson_tm * timesub (const int64_t *const timep, const int_fast32_t offset, - register const struct state *const sp, - register struct bson_tm *const tmp) + const struct state *const sp, + struct bson_tm *const tmp) { - register const struct lsinfo *lp; - register int64_t tdays; - register int64_t idays; /* unsigned would be so 2003 */ - register int_fast64_t rem; + const struct lsinfo *lp; + int64_t tdays; + int64_t idays; /* unsigned would be so 2003 */ + int_fast64_t rem; int64_t y; - register const int (*ip)[MONSPERYEAR]; - register int_fast64_t corr; - register int64_t hit; - register int64_t i; + const int (*ip)[MONSPERYEAR]; + int_fast64_t corr; + int64_t hit; + int64_t i; corr = 0; hit = 0; @@ -370,9 +376,9 @@ timesub (const int64_t *const timep, rem = *timep - tdays * SECSPERDAY; while (tdays < 0 || tdays >= year_lengths[isleap (y)]) { int64_t newy; - register int64_t tdelta; - register int64_t idelta; - register int64_t leapdays; + int64_t tdelta; + int64_t idelta; + int64_t leapdays; tdelta = tdays / DAYSPERLYEAR; idelta = tdelta; @@ -387,7 +393,7 @@ timesub (const int64_t *const timep, y = newy; } { - register int_fast32_t seconds; + int_fast32_t seconds; seconds = (int_fast32_t) (tdays * SECSPERDAY); tdays = seconds / SECSPERDAY; @@ -472,7 +478,7 @@ timesub (const int64_t *const timep, static int64_t increment_overflow (int64_t *const ip, int64_t j) { - register int64_t const i = *ip; + int64_t const i = *ip; /* ** If i >= 0 there can only be overflow if i + j > INT_MAX @@ -489,11 +495,11 @@ increment_overflow (int64_t *const ip, int64_t j) static int64_t increment_overflow32 (int_fast32_t *const lp, int64_t const m) { - register int_fast32_t const l = *lp; + int_fast32_t const l = *lp; if ((l >= 0) ? (m > INT_FAST32_MAX - l) : (m < INT_FAST32_MIN - l)) return true; - *lp += m; + *lp += (int_fast32_t) m; return false; } @@ -502,7 +508,7 @@ normalize_overflow (int64_t *const tensptr, int64_t *const unitsptr, const int64_t base) { - register int64_t tensdelta; + int64_t tensdelta; tensdelta = (*unitsptr >= 0) ? (*unitsptr / base) : (-1 - (-1 - *unitsptr) / base); @@ -515,7 +521,7 @@ normalize_overflow32 (int_fast32_t *const tensptr, int64_t *const unitsptr, const int64_t base) { - register int64_t tensdelta; + int64_t tensdelta; tensdelta = (*unitsptr >= 0) ? (*unitsptr / base) : (-1 - (-1 - *unitsptr) / base); @@ -524,10 +530,9 @@ normalize_overflow32 (int_fast32_t *const tensptr, } static int64_t -tmcomp (register const struct bson_tm *const atmp, - register const struct bson_tm *const btmp) +tmcomp (const struct bson_tm *const atmp, const struct bson_tm *const btmp) { - register int64_t result; + int64_t result; if (atmp->tm_year != btmp->tm_year) return atmp->tm_year < btmp->tm_year ? -1 : 1; @@ -548,13 +553,13 @@ time2sub (struct bson_tm *const tmp, int64_t *const okayp, const int64_t do_norm_secs) { - register const struct state *sp; - register int64_t dir; - register int64_t i, j; - register int64_t saved_seconds; - register int_fast32_t li; - register int64_t lo; - register int64_t hi; + const struct state *sp; + int64_t dir; + int64_t i, j; + int64_t saved_seconds; + int_fast32_t li; + int64_t lo; + int64_t hi; int_fast32_t y; int64_t newt; int64_t t; @@ -736,12 +741,12 @@ time1 (struct bson_tm *const tmp, struct bson_tm *), const int_fast32_t offset) { - register int64_t t; - register const struct state *sp; - register int64_t samei, otheri; - register int64_t sameind, otherind; - register int64_t i; - register int64_t nseen; + int64_t t; + const struct state *sp; + int64_t samei, otheri; + int64_t sameind, otherind; + int64_t i; + int64_t nseen; int64_t seen[TZ_MAX_TYPES]; int64_t types[TZ_MAX_TYPES]; int64_t okay; diff --git a/src/libbson/src/bson/bson.c b/src/libbson/src/bson/bson.c index cad9e6127b7..3adeba0e6fe 100644 --- a/src/libbson/src/bson/bson.c +++ b/src/libbson/src/bson/bson.c @@ -2993,12 +2993,13 @@ _bson_as_json_visit_after (const bson_iter_t *iter, const char *key, void *data) return false; } - if (state->str->len >= state->max_len) { + if (bson_cmp_greater_equal_us (state->str->len, state->max_len)) { state->max_len_reached = true; - if (state->str->len > state->max_len) { + if (bson_cmp_greater_us (state->str->len, state->max_len)) { + BSON_ASSERT (bson_in_range_signed (uint32_t, state->max_len)); /* Truncate string to maximum length */ - bson_string_truncate (state->str, state->max_len); + bson_string_truncate (state->str, (uint32_t) state->max_len); } return true; @@ -3106,7 +3107,8 @@ _bson_as_json_visit_codewscope (const bson_iter_t *iter, /* Encode scope with the same mode */ if (state->max_len != BSON_MAX_LEN_UNLIMITED) { - max_scope_len = BSON_MAX (0, state->max_len - state->str->len); + BSON_ASSERT (bson_in_range_unsigned (int32_t, state->str->len)); + max_scope_len = BSON_MAX (0, state->max_len - (int32_t) state->str->len); } scope = _bson_as_json_visit_all (v_scope, NULL, state->mode, max_scope_len); @@ -3165,7 +3167,9 @@ _bson_as_json_visit_document (const bson_iter_t *iter, child_state.mode = state->mode; child_state.max_len = BSON_MAX_LEN_UNLIMITED; if (state->max_len != BSON_MAX_LEN_UNLIMITED) { - child_state.max_len = BSON_MAX (0, state->max_len - state->str->len); + BSON_ASSERT (bson_in_range_unsigned (int32_t, state->str->len)); + child_state.max_len = + BSON_MAX (0, state->max_len - (int32_t) state->str->len); } child_state.max_len_reached = child_state.max_len == 0; @@ -3216,7 +3220,9 @@ _bson_as_json_visit_array (const bson_iter_t *iter, child_state.mode = state->mode; child_state.max_len = BSON_MAX_LEN_UNLIMITED; if (state->max_len != BSON_MAX_LEN_UNLIMITED) { - child_state.max_len = BSON_MAX (0, state->max_len - state->str->len); + BSON_ASSERT (bson_in_range_unsigned (int32_t, state->str->len)); + child_state.max_len = + BSON_MAX (0, state->max_len - (int32_t) state->str->len); } child_state.max_len_reached = child_state.max_len == 0; diff --git a/src/libbson/tests/test-b64.c b/src/libbson/tests/test-b64.c index 851eaa2860a..4be8ecb96f3 100644 --- a/src/libbson/tests/test-b64.c +++ b/src/libbson/tests/test-b64.c @@ -36,7 +36,7 @@ _test_encode_helper (char *input, ASSERT_CMPSIZE_T (target_size, ==, (size_t) expected_output_len + 1); /* returned value does not count trailing NULL. */ ret = mcommon_b64_ntop ((uint8_t *) input, input_len, output, target_size); - ASSERT_CMPINT (target_size - 1, ==, ret); + ASSERT (bson_cmp_equal_us (target_size - 1u, ret)); ASSERT_CMPSTR (output, expected_output); bson_free (output); } diff --git a/src/libbson/tests/test-iso8601.c b/src/libbson/tests/test-iso8601.c index 91bb67f792d..81d5e540075 100644 --- a/src/libbson/tests/test-iso8601.c +++ b/src/libbson/tests/test-iso8601.c @@ -11,7 +11,11 @@ test_date (const char *str, int64_t millis) int64_t v; bson_error_t error; - if (!_bson_iso8601_date_parse (str, strlen (str), &v, &error)) { + const size_t len = strlen (str); + + BSON_ASSERT (bson_in_range_unsigned (int32_t, len)); + + if (!_bson_iso8601_date_parse (str, (int32_t) len, &v, &error)) { fprintf (stderr, "could not parse (%s)\n", str); abort (); } @@ -56,7 +60,11 @@ test_date_should_fail (const char *str) int64_t v; bson_error_t error; - if (_bson_iso8601_date_parse (str, strlen (str), &v, &error)) { + const size_t len = strlen (str); + + BSON_ASSERT (bson_in_range_unsigned (int32_t, len)); + + if (_bson_iso8601_date_parse (str, (int32_t) len, &v, &error)) { fprintf (stderr, "should not be able to parse (%s)\n", str); abort (); } diff --git a/src/libbson/tests/test-json.c b/src/libbson/tests/test-json.c index 08b0e93cc62..a81ef909181 100644 --- a/src/libbson/tests/test-json.c +++ b/src/libbson/tests/test-json.c @@ -377,7 +377,6 @@ test_bson_as_json_double (void) } -#if defined(NAN) && defined(INFINITY) static void test_bson_as_json_double_nonfinite (void) { @@ -386,10 +385,13 @@ test_bson_as_json_double_nonfinite (void) char *str; char *expected; + const double pos_inf = INFINITY; + const double neg_inf = -pos_inf; + b = bson_new (); BSON_ASSERT (bson_append_double (b, "nan", -1, NAN)); - BSON_ASSERT (bson_append_double (b, "pos_inf", -1, INFINITY)); - BSON_ASSERT (bson_append_double (b, "neg_inf", -1, -INFINITY)); + BSON_ASSERT (bson_append_double (b, "pos_inf", -1, pos_inf)); + BSON_ASSERT (bson_append_double (b, "neg_inf", -1, neg_inf)); str = bson_as_json (b, &len); expected = bson_strdup_printf ("{" @@ -397,8 +399,8 @@ test_bson_as_json_double_nonfinite (void) " \"pos_inf\" : %.20g," " \"neg_inf\" : %.20g }", NAN, - INFINITY, - -INFINITY); + pos_inf, + neg_inf); ASSERT_CMPSTR (str, expected); @@ -406,7 +408,6 @@ test_bson_as_json_double_nonfinite (void) bson_free (str); bson_destroy (b); } -#endif static void @@ -2679,13 +2680,13 @@ test_bson_array_as_json (void) str = bson_array_as_json (&d, &len); ASSERT_CMPSTR (str, "[ ]"); - ASSERT_CMPINT (len, ==, 3); + ASSERT_CMPSIZE_T (len, ==, 3u); bson_free (str); BSON_APPEND_INT32 (&d, "0", 1); str = bson_array_as_json (&d, &len); ASSERT_CMPSTR (str, "[ 1 ]"); - ASSERT_CMPINT (len, ==, 5); + ASSERT_CMPSIZE_T (len, ==, 5u); bson_free (str); /* test corrupted bson */ @@ -2909,10 +2910,11 @@ test_bson_as_json_with_opts (bson_t *bson, char *str = bson_as_json_with_opts (bson, &json_len, opts); ASSERT_CMPSTR (str, expected); - ASSERT_CMPINT (json_len, ==, strlen (expected)); + ASSERT_CMPSIZE_T (json_len, ==, strlen (expected)); if (max_len != BSON_MAX_LEN_UNLIMITED) { - ASSERT_CMPINT (json_len, <=, max_len); + ASSERT (bson_in_range_signed (size_t, max_len)); + ASSERT_CMPSIZE_T (json_len, <=, (size_t) max_len); } bson_free (str); @@ -2936,13 +2938,17 @@ run_bson_as_json_with_opts_tests (bson_t *bson, bson_json_mode_t mode, const char *expected) { - size_t len = strlen (expected); + const size_t ulen = strlen (expected); char *truncated; - size_t i; + + BSON_ASSERT (bson_in_range_unsigned (int, ulen)); + const int len = (int) ulen; /* Test with 0 length (empty string). */ test_bson_as_json_with_opts (bson, mode, 0, ""); + BSON_ASSERT (INT_MAX - 2 >= len); + /* Test with a limit that does not truncate the string. */ test_bson_as_json_with_opts (bson, mode, len + 2, expected); @@ -2950,8 +2956,8 @@ run_bson_as_json_with_opts_tests (bson_t *bson, test_bson_as_json_with_opts (bson, mode, BSON_MAX_LEN_UNLIMITED, expected); /* Test every possible limit from 0 to length. */ - for (i = 0; i < len; i++) { - truncated = truncate_string (expected, i); + for (int i = 0; i < len; i++) { + truncated = truncate_string (expected, (size_t) i); test_bson_as_json_with_opts (bson, mode, i, truncated); bson_free (truncated); } @@ -3044,7 +3050,7 @@ test_bson_as_json_with_opts_array (void) static void test_bson_as_json_with_opts_binary (void) { - const uint8_t data[] = {1, 2.0, 3, 4}; + const uint8_t data[] = {1, 2, 3, 4}; bson_t *b; b = bson_new (); @@ -3459,11 +3465,9 @@ test_json_install (TestSuite *suite) TestSuite_Add (suite, "/bson/as_json/int32", test_bson_as_json_int32); TestSuite_Add (suite, "/bson/as_json/int64", test_bson_as_json_int64); TestSuite_Add (suite, "/bson/as_json/double", test_bson_as_json_double); -#if defined(NAN) && defined(INFINITY) TestSuite_Add (suite, "/bson/as_json/double/nonfinite", test_bson_as_json_double_nonfinite); -#endif TestSuite_Add (suite, "/bson/as_json/code", test_bson_as_json_code); TestSuite_Add ( suite, "/bson/as_json/date_time", test_bson_as_json_date_time); diff --git a/src/libmongoc/src/mongoc/mongoc-util.c b/src/libmongoc/src/mongoc/mongoc-util.c index 56eb729b547..e9e8fad157d 100644 --- a/src/libmongoc/src/mongoc/mongoc-util.c +++ b/src/libmongoc/src/mongoc/mongoc-util.c @@ -675,9 +675,9 @@ _mongoc_getenv (const char *name) return NULL; } #else - - if (getenv (name) && strlen (getenv (name))) { - return bson_strdup (getenv (name)); + char *const var = getenv (name); + if (var && strlen (var)) { + return bson_strdup (var); } else { return NULL; }