From 1711bed930c7d5225a225246ac6d0f6d6ee0ae61 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 7 Mar 2023 13:51:40 -0600 Subject: [PATCH 01/23] bson-check-depth.c: address C4018 signedness mismatch warnings --- src/libbson/examples/bson-check-depth.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) 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) { From 2b2452143bd7d6dc9c7cafc137e38d1e909c10b8 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 7 Mar 2023 19:29:06 -0600 Subject: [PATCH 02/23] bson-decimal128.c: address C4018 signedness mismatch warnings --- src/libbson/src/bson/bson-decimal128.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/libbson/src/bson/bson-decimal128.c b/src/libbson/src/bson/bson-decimal128.c index 9f4e9639f74..5454e2e3d71 100644 --- a/src/libbson/src/bson/bson-decimal128.c +++ b/src/libbson/src/bson/bson-decimal128.c @@ -155,8 +155,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 +210,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 +221,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 +262,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 +272,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 +281,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 +295,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,7 +624,8 @@ 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; From 3e3d4944a3f74218cb5ac83a2d769f82b79a5bbf Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 7 Mar 2023 15:41:27 -0600 Subject: [PATCH 03/23] bson-decimal128.c: address C4267 size_t conversion warnings --- src/libbson/src/bson/bson-decimal128.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libbson/src/bson/bson-decimal128.c b/src/libbson/src/bson/bson-decimal128.c index 5454e2e3d71..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" @@ -628,7 +629,8 @@ bson_decimal128_from_string_w_len (const char *string, /* IN */ 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 */ From ea1d539d45eea97fe87ef6a458e80e2b2a5466d2 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 7 Mar 2023 15:51:09 -0600 Subject: [PATCH 04/23] bson-iter.c: address C4267 size_t conversion warnings --- src/libbson/src/bson/bson-iter.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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; From e5d82c79ae2910b99c3c97211e522308887e4f29 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 7 Mar 2023 15:54:30 -0600 Subject: [PATCH 05/23] bson-json.c: address C4018 signedness mismatch warnings --- src/libbson/src/bson/bson-json.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/libbson/src/bson/bson-json.c b/src/libbson/src/bson/bson-json.c index e1f0fa72a9e..48354ec1b4f 100644 --- a/src/libbson/src/bson/bson-json.c +++ b/src/libbson/src/bson/bson-json.c @@ -831,7 +831,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); @@ -2233,8 +2233,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); From 01eff66fd04788afafe4295f19f3aa42da0d325d Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 7 Mar 2023 16:10:10 -0600 Subject: [PATCH 06/23] bson-json.c: address C4267 size_t conversion warnings --- src/libbson/src/bson/bson-json.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libbson/src/bson/bson-json.c b/src/libbson/src/bson/bson-json.c index 48354ec1b4f..a2a2009306b 100644 --- a/src/libbson/src/bson/bson-json.c +++ b/src/libbson/src/bson/bson-json.c @@ -1186,8 +1186,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: From 825c2dd52cccc0e632c069b20b980a7a25d0eca0 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 7 Mar 2023 16:06:21 -0600 Subject: [PATCH 07/23] bson-json.c: address C4146 unsigned unary negation warnings --- src/libbson/src/bson/bson-json.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/libbson/src/bson/bson-json.c b/src/libbson/src/bson/bson-json.c index a2a2009306b..f9775cdec20 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); } From 4296e45c6dabac5438a4d95e952bc3e80c2919ae Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 7 Mar 2023 15:33:54 -0600 Subject: [PATCH 08/23] bson-metrics.c: address C4244 narrowing conversion warnings --- src/libbson/examples/bson-metrics.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/libbson/examples/bson-metrics.c b/src/libbson/examples/bson-metrics.c index a1cac482d47..94e50b14fd4 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 *) b)->count; + const uint64_t b_count = ((bson_type_metrics_t *) a)->count; + if (a_count == b_count) { + return 0; + } + return a_count < b_count ? -1 : 1; } /* From 3a970f45a2d2a53ec7414d5063f0a793da0ad390 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 7 Mar 2023 19:39:06 -0600 Subject: [PATCH 09/23] bson-timegm.c: address C4028 parameter inconsistency warnings --- src/libbson/src/bson/bson-timegm.c | 67 +++++++++++++++++------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/src/libbson/src/bson/bson-timegm.c b/src/libbson/src/bson/bson-timegm.c index a7838e7c159..0a3c4f1201f 100644 --- a/src/libbson/src/bson/bson-timegm.c +++ b/src/libbson/src/bson/bson-timegm.c @@ -225,47 +225,54 @@ 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 (register 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, + register const struct state *const sp, + register struct bson_tm *const tmp); static int64_t -tmcomp (const struct bson_tm *atmp, const struct bson_tm *btmp); +tmcomp (register const struct bson_tm *const atmp, + register const struct bson_tm *const btmp); static struct state gmtmem; #define gmtptr (&gmtmem) From 515adc6e8e07f7d3defc2bd130bc183c34127519 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 7 Mar 2023 16:11:11 -0600 Subject: [PATCH 10/23] bson-timegm.c: address C4244 narrowing conversion warnings --- 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 0a3c4f1201f..1c6efcecd81 100644 --- a/src/libbson/src/bson/bson-timegm.c +++ b/src/libbson/src/bson/bson-timegm.c @@ -500,7 +500,7 @@ increment_overflow32 (int_fast32_t *const lp, int64_t const m) if ((l >= 0) ? (m > INT_FAST32_MAX - l) : (m < INT_FAST32_MIN - l)) return true; - *lp += m; + *lp += (int_fast32_t) m; return false; } From 51c7295897e58861464b6bb482645c017e2a25db Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 7 Mar 2023 16:13:40 -0600 Subject: [PATCH 11/23] bson.c: address C4018 signedness mismatch warnings --- src/libbson/src/bson/bson.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) 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; From 93e0999fce6bc397208de1a34c1ea76c9a447ad5 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 7 Mar 2023 16:32:41 -0600 Subject: [PATCH 12/23] test-b64.c: address C4267 size_t conversion warnings --- src/libbson/tests/test-b64.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); } From a4d808f4e7b032c228e018b726ba75354c3fcf25 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 7 Mar 2023 16:35:00 -0600 Subject: [PATCH 13/23] test-iso8601.c: address C4267 size_t conversion warnings --- src/libbson/tests/test-iso8601.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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 (); } From 151a9eea661d31833e3b7972ff190b3165ca7291 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 7 Mar 2023 16:36:36 -0600 Subject: [PATCH 14/23] test-json.c: address C4244 narrowing conversion warnings --- src/libbson/tests/test-json.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libbson/tests/test-json.c b/src/libbson/tests/test-json.c index 08b0e93cc62..6db4bf830f2 100644 --- a/src/libbson/tests/test-json.c +++ b/src/libbson/tests/test-json.c @@ -3044,7 +3044,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 (); From 872d53df613f82b8a6f5ddfd9719c90da77fd4a9 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 7 Mar 2023 16:50:04 -0600 Subject: [PATCH 15/23] test-json.c: address C4267 size_t conversion warnings --- src/libbson/tests/test-json.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/libbson/tests/test-json.c b/src/libbson/tests/test-json.c index 6db4bf830f2..d8315ec0f44 100644 --- a/src/libbson/tests/test-json.c +++ b/src/libbson/tests/test-json.c @@ -2679,13 +2679,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 +2909,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 +2937,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 +2955,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); } From 8caabb7682f54f035bfc8ad6f8af7a3912c8a3a2 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Fri, 10 Mar 2023 17:10:24 -0600 Subject: [PATCH 16/23] bson-memory.c: address -Wunused-result warnings --- src/libbson/src/bson/bson-memory.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 From bba1e863fc7db075c29f87f5bf4d85a435927b59 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Fri, 24 Mar 2023 10:23:07 -0500 Subject: [PATCH 17/23] mongoc-util.c: address scan-build unix.cstring.NullArg warnings --- src/libmongoc/src/mongoc/mongoc-util.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libmongoc/src/mongoc/mongoc-util.c b/src/libmongoc/src/mongoc/mongoc-util.c index 56eb729b547..d0814309604 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; } From a5d295bdd103d53279c559587aa9e6522fd0054d Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Mon, 13 Mar 2023 12:36:36 -0500 Subject: [PATCH 18/23] Disable C4756 when compiling with VS 2013 or older --- src/libbson/CMakeLists.txt | 14 ++++++++++++++ src/libbson/src/bson/bson-json.c | 23 +++++++++++------------ src/libbson/tests/test-json.c | 11 +++++++---- 3 files changed, 32 insertions(+), 16 deletions(-) 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/src/bson/bson-json.c b/src/libbson/src/bson/bson-json.c index f9775cdec20..5c3be9bcf83 100644 --- a/src/libbson/src/bson/bson-json.c +++ b/src/libbson/src/bson/bson-json.c @@ -741,6 +741,15 @@ _bson_json_parse_double (bson_json_reader_t *reader, *d = strtod (val, NULL); #ifdef _MSC_VER +#ifdef INFINITY + const double pos_inf = INFINITY; + const double neg_inf = -pos_inf; +#else + const unsigned long inf_rep[2] = {0x00000000, 0x7ff00000}; + const double pos_inf = *(double *) inf; + const double neg_inf = -pos_inf; +#endif + /* Microsoft's strtod parses "NaN", "Infinity", "-Infinity" as 0 */ if (*d == 0.0) { if (!_strnicmp (val, "nan", vlen)) { @@ -754,20 +763,10 @@ _bson_json_parse_double (bson_json_reader_t *reader, #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; } } diff --git a/src/libbson/tests/test-json.c b/src/libbson/tests/test-json.c index d8315ec0f44..2841ca4a4d2 100644 --- a/src/libbson/tests/test-json.c +++ b/src/libbson/tests/test-json.c @@ -386,10 +386,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 +400,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); From 8ef6a84f1fa762be168ff779a6abdf690d21f72e Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Fri, 24 Mar 2023 13:26:30 -0500 Subject: [PATCH 19/23] Remove obsolete checks for INFINITY and NAN --- src/libbson/src/bson/bson-json.c | 13 ------------- src/libbson/tests/test-json.c | 4 ---- 2 files changed, 17 deletions(-) diff --git a/src/libbson/src/bson/bson-json.c b/src/libbson/src/bson/bson-json.c index 5c3be9bcf83..28f36abe948 100644 --- a/src/libbson/src/bson/bson-json.c +++ b/src/libbson/src/bson/bson-json.c @@ -741,26 +741,13 @@ _bson_json_parse_double (bson_json_reader_t *reader, *d = strtod (val, NULL); #ifdef _MSC_VER -#ifdef INFINITY const double pos_inf = INFINITY; const double neg_inf = -pos_inf; -#else - const unsigned long inf_rep[2] = {0x00000000, 0x7ff00000}; - const double pos_inf = *(double *) inf; - const double neg_inf = -pos_inf; -#endif /* 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)) { *d = pos_inf; diff --git a/src/libbson/tests/test-json.c b/src/libbson/tests/test-json.c index 2841ca4a4d2..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) { @@ -409,7 +408,6 @@ test_bson_as_json_double_nonfinite (void) bson_free (str); bson_destroy (b); } -#endif static void @@ -3467,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); From 591e49880714f1d8175c3c5887585f9b803f6656 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Fri, 24 Mar 2023 13:35:30 -0500 Subject: [PATCH 20/23] bson-timegm.c: remove use of register for parameters and variables --- src/libbson/src/bson/bson-timegm.c | 82 +++++++++++++++--------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/src/libbson/src/bson/bson-timegm.c b/src/libbson/src/bson/bson-timegm.c index 1c6efcecd81..1c44398b159 100644 --- a/src/libbson/src/bson/bson-timegm.c +++ b/src/libbson/src/bson/bson-timegm.c @@ -233,7 +233,7 @@ gmtsub (const int64_t *const timep, static int64_t increment_overflow (int64_t *const ip, int64_t j); static int64_t -leaps_thru_end_of (register const int64_t y) ATTRIBUTE_PURE; +leaps_thru_end_of (const int64_t y) ATTRIBUTE_PURE; static int64_t increment_overflow32 (int_fast32_t *const lp, int64_t const m); static int64_t @@ -268,11 +268,11 @@ time2sub (struct bson_tm *const tmp, 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); 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); static struct state gmtmem; #define gmtptr (&gmtmem) @@ -305,7 +305,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; @@ -329,7 +329,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); @@ -338,18 +338,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; @@ -377,9 +377,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; @@ -394,7 +394,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; @@ -479,7 +479,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 @@ -496,7 +496,7 @@ 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; @@ -509,7 +509,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); @@ -522,7 +522,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); @@ -531,10 +531,10 @@ 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; @@ -555,13 +555,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; @@ -743,12 +743,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; From 02e819a24f6cd3fd36aebe1ae3e446da2dfda579 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Fri, 24 Mar 2023 14:12:11 -0500 Subject: [PATCH 21/23] Format bson-timegm.c --- src/libbson/src/bson/bson-timegm.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/libbson/src/bson/bson-timegm.c b/src/libbson/src/bson/bson-timegm.c index 1c44398b159..a8f20cff37a 100644 --- a/src/libbson/src/bson/bson-timegm.c +++ b/src/libbson/src/bson/bson-timegm.c @@ -271,8 +271,7 @@ timesub (const int64_t *const timep, const struct state *const sp, struct bson_tm *const tmp); static int64_t -tmcomp (const struct bson_tm *const atmp, - const struct bson_tm *const btmp); +tmcomp (const struct bson_tm *const atmp, const struct bson_tm *const btmp); static struct state gmtmem; #define gmtptr (&gmtmem) @@ -531,8 +530,7 @@ normalize_overflow32 (int_fast32_t *const tensptr, } static int64_t -tmcomp (const struct bson_tm *const atmp, - const struct bson_tm *const btmp) +tmcomp (const struct bson_tm *const atmp, const struct bson_tm *const btmp) { int64_t result; From 0267a32d3e83d208500b8412092f225126c4dab6 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Fri, 24 Mar 2023 14:12:54 -0500 Subject: [PATCH 22/23] Format mongoc-util.c --- src/libmongoc/src/mongoc/mongoc-util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libmongoc/src/mongoc/mongoc-util.c b/src/libmongoc/src/mongoc/mongoc-util.c index d0814309604..e9e8fad157d 100644 --- a/src/libmongoc/src/mongoc/mongoc-util.c +++ b/src/libmongoc/src/mongoc/mongoc-util.c @@ -675,7 +675,7 @@ _mongoc_getenv (const char *name) return NULL; } #else - char* const var = getenv (name); + char *const var = getenv (name); if (var && strlen (var)) { return bson_strdup (var); } else { From ad9a023b8c6dc874a35d3815cda02c81f5547abe Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Mon, 27 Mar 2023 11:49:37 -0500 Subject: [PATCH 23/23] Fix order of parameters in casts --- src/libbson/examples/bson-metrics.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libbson/examples/bson-metrics.c b/src/libbson/examples/bson-metrics.c index 94e50b14fd4..2cd4acd6dc8 100644 --- a/src/libbson/examples/bson-metrics.c +++ b/src/libbson/examples/bson-metrics.c @@ -81,8 +81,8 @@ static bson_metrics_state_t state; static int compar_bson_type_metrics (const void *a, const void *b) { - const uint64_t a_count = ((bson_type_metrics_t *) b)->count; - const uint64_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; }