Skip to content

Commit c41c3a8

Browse files
authored
CDRIVER-4596 Reducing Warnings - MSVC and MinGW Warnings in libbson (#1221)
* bson-check-depth.c: address C4018 signedness mismatch warnings * bson-decimal128.c: address C4018 signedness mismatch warnings * bson-decimal128.c: address C4267 size_t conversion warnings * bson-iter.c: address C4267 size_t conversion warnings * bson-json.c: address C4018 signedness mismatch warnings * bson-json.c: address C4267 size_t conversion warnings * bson-json.c: address C4146 unsigned unary negation warnings * bson-metrics.c: address C4244 narrowing conversion warnings * bson-timegm.c: address C4028 parameter inconsistency warnings * bson-timegm.c: address C4244 narrowing conversion warnings * bson.c: address C4018 signedness mismatch warnings * test-b64.c: address C4267 size_t conversion warnings * test-iso8601.c: address C4267 size_t conversion warnings * test-json.c: address C4244 narrowing conversion warnings * test-json.c: address C4267 size_t conversion warnings * bson-memory.c: address -Wunused-result warnings * mongoc-util.c: address scan-build unix.cstring.NullArg warnings * Disable C4756 when compiling with VS 2013 or older * Remove obsolete checks for INFINITY and NAN * bson-timegm.c: remove use of register for parameters and variables
1 parent e040ceb commit c41c3a8

File tree

13 files changed

+194
-142
lines changed

13 files changed

+194
-142
lines changed

src/libbson/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ set (HEADERS_FORWARDING
210210
)
211211

212212
add_library (bson_shared SHARED ${SOURCES} ${HEADERS} ${HEADERS_FORWARDING})
213+
if (MSVC AND MSVC_VERSION VERSION_LESS 1900)
214+
message (STATUS "Disabling warning C4756 for VS 2013 and older")
215+
# Macro constant INFINITY triggers constant arithmetic overflow warnings in
216+
# VS 2013, but VS 2013 doesn't support inline warning suppression.
217+
# Remove once support for VS 2013 is dropped.
218+
target_compile_options(bson_shared PRIVATE /wd4756)
219+
endif ()
213220
set (CMAKE_CXX_VISIBILITY_PRESET hidden)
214221
target_compile_definitions (bson_shared
215222
PRIVATE
@@ -278,6 +285,13 @@ endif ()
278285

279286
if (MONGOC_ENABLE_STATIC_BUILD)
280287
add_library (bson_static STATIC ${SOURCES} ${HEADERS} ${HEADERS_FORWARDING})
288+
if (MSVC AND MSVC_VERSION VERSION_LESS 1900)
289+
message (STATUS "Disabling warning C4756 for VS 2013 and older")
290+
# Macro constant INFINITY triggers constant arithmetic overflow warnings in
291+
# VS 2013, but VS 2013 doesn't support inline warning suppression.
292+
# Remove once support for VS 2013 is dropped.
293+
target_compile_options(bson_static PRIVATE /wd4756)
294+
endif ()
281295
target_compile_definitions (bson_static
282296
PUBLIC
283297
BSON_STATIC

src/libbson/examples/bson-check-depth.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
#include <bson/bson.h>
2020

2121
#include <assert.h>
22+
#include <inttypes.h>
2223
#include <stdio.h>
2324
#include <stdlib.h>
2425

2526
typedef struct {
2627
uint32_t depth;
27-
int max_depth;
28+
uint32_t max_depth;
2829
bool valid;
2930
} check_depth_t;
3031

@@ -74,7 +75,7 @@ _check_depth_document (const bson_iter_t *iter,
7475
}
7576

7677
void
77-
check_depth (const bson_t *bson, int max_depth)
78+
check_depth (const bson_t *bson, uint32_t max_depth)
7879
{
7980
bson_iter_t iter;
8081
check_depth_t state = {0};
@@ -87,7 +88,8 @@ check_depth (const bson_t *bson, int max_depth)
8788
state.max_depth = max_depth;
8889
_check_depth_document (&iter, NULL, bson, &state);
8990
if (!state.valid) {
90-
printf ("document exceeds maximum depth of %d\n", state.max_depth);
91+
printf ("document exceeds maximum depth of %" PRIu32 "\n",
92+
state.max_depth);
9193
} else {
9294
char *as_json = bson_as_canonical_extended_json (bson, NULL);
9395
printf ("document %s ", as_json);
@@ -102,26 +104,27 @@ main (int argc, char **argv)
102104
bson_reader_t *bson_reader;
103105
const bson_t *bson;
104106
bool reached_eof;
105-
char *filename;
106107
bson_error_t error;
107-
int max_depth;
108108

109109
if (argc != 3) {
110110
fprintf (stderr, "usage: %s FILE MAX_DEPTH\n", argv[0]);
111111
fprintf (stderr, "Checks that the depth of the BSON contained in FILE\n");
112112
fprintf (stderr, "does not exceed MAX_DEPTH\n");
113113
}
114114

115-
filename = argv[1];
116-
max_depth = atoi (argv[2]);
115+
const char *const filename = argv[1];
116+
const int max_depth = atoi (argv[2]);
117+
117118
bson_reader = bson_reader_new_from_file (filename, &error);
118119
if (!bson_reader) {
119120
printf ("could not read %s: %s\n", filename, error.message);
120121
return 1;
121122
}
122123

124+
BSON_ASSERT (bson_in_range_signed (uint32_t, max_depth));
125+
123126
while ((bson = bson_reader_read (bson_reader, &reached_eof))) {
124-
check_depth (bson, max_depth);
127+
check_depth (bson, (uint32_t) max_depth);
125128
}
126129

127130
if (!reached_eof) {

src/libbson/examples/bson-metrics.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,12 @@ static bson_metrics_state_t state;
8181
static int
8282
compar_bson_type_metrics (const void *a, const void *b)
8383
{
84-
return (((bson_type_metrics_t *) b)->count -
85-
((bson_type_metrics_t *) a)->count);
84+
const uint64_t a_count = ((bson_type_metrics_t *) a)->count;
85+
const uint64_t b_count = ((bson_type_metrics_t *) b)->count;
86+
if (a_count == b_count) {
87+
return 0;
88+
}
89+
return a_count < b_count ? -1 : 1;
8690
}
8791

8892
/*

src/libbson/src/bson/bson-decimal128.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <string.h>
2020
#include <ctype.h>
2121

22+
#include "bson-cmp.h"
2223
#include "bson-decimal128.h"
2324
#include "bson-types.h"
2425
#include "bson-macros.h"
@@ -155,8 +156,6 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */
155156
uint8_t significand_msb; /* the most signifcant significand bits (50-46) */
156157
_bson_uint128_t
157158
significand128; /* temporary storage for significand decoding */
158-
size_t i; /* indexing variables */
159-
int j, k;
160159

161160
memset (significand_str, 0, sizeof (significand_str));
162161

@@ -212,7 +211,7 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */
212211
*/
213212
is_zero = true;
214213
} else {
215-
for (k = 3; k >= 0; k--) {
214+
for (int k = 3; k >= 0; k--) {
216215
uint32_t least_digits = 0;
217216
_bson_uint128_divide1B (
218217
significand128, &significand128, &least_digits);
@@ -223,7 +222,7 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */
223222
continue;
224223
}
225224

226-
for (j = 8; j >= 0; j--) {
225+
for (int j = 8; j >= 0; j--) {
227226
significand[k * 9 + j] = least_digits % 10;
228227
least_digits /= 10;
229228
}
@@ -264,7 +263,8 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */
264263
*(str_out++) = '.';
265264
}
266265

267-
for (i = 0; i < significand_digits && (str_out - str) < 36; i++) {
266+
for (uint32_t i = 0; i < significand_digits && (str_out - str) < 36;
267+
i++) {
268268
*(str_out++) = *(significand_read++) + '0';
269269
}
270270
/* Exponent */
@@ -273,15 +273,16 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */
273273
} else {
274274
/* Regular format with no decimal place */
275275
if (exponent >= 0) {
276-
for (i = 0; i < significand_digits && (str_out - str) < 36; i++) {
276+
for (uint32_t i = 0; i < significand_digits && (str_out - str) < 36;
277+
i++) {
277278
*(str_out++) = *(significand_read++) + '0';
278279
}
279280
*str_out = '\0';
280281
} else {
281282
int32_t radix_position = significand_digits + exponent;
282283

283284
if (radix_position > 0) { /* non-zero digits before radix */
284-
for (i = 0;
285+
for (int32_t i = 0;
285286
i < radix_position && (str_out - str) < BSON_DECIMAL128_STRING;
286287
i++) {
287288
*(str_out++) = *(significand_read++) + '0';
@@ -295,8 +296,9 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */
295296
*(str_out++) = '0';
296297
}
297298

298-
for (i = 0;
299-
(i < significand_digits - BSON_MAX (radix_position - 1, 0)) &&
299+
for (uint32_t i = 0;
300+
bson_cmp_greater_us (significand_digits - i,
301+
BSON_MAX (radix_position - 1, 0)) &&
300302
(str_out - str) < BSON_DECIMAL128_STRING;
301303
i++) {
302304
*(str_out++) = *(significand_read++) + '0';
@@ -623,10 +625,12 @@ bson_decimal128_from_string_w_len (const char *string, /* IN */
623625
/* to represent user input */
624626

625627
/* Overflow prevention */
626-
if (exponent <= radix_position && radix_position - exponent > (1 << 14)) {
628+
if (bson_cmp_less_equal_su (exponent, radix_position) &&
629+
bson_cmp_greater_us (radix_position, exponent + (1 << 14))) {
627630
exponent = BSON_DECIMAL128_EXPONENT_MIN;
628631
} else {
629-
exponent -= radix_position;
632+
BSON_ASSERT (bson_in_range_unsigned (int32_t, radix_position));
633+
exponent -= (int32_t) radix_position;
630634
}
631635

632636
/* Attempt to normalize the exponent */

src/libbson/src/bson/bson-iter.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,13 @@ bson_iter_init_from_data (bson_iter_t *iter, /* OUT */
110110
return false;
111111
}
112112

113+
if (BSON_UNLIKELY (!bson_in_range_unsigned (uint32_t, length))) {
114+
memset (iter, 0, sizeof *iter);
115+
return false;
116+
}
117+
113118
iter->raw = (uint8_t *) data;
114-
iter->len = length;
119+
iter->len = (uint32_t) length;
115120
iter->off = 0;
116121
iter->type = 0;
117122
iter->key = 0;

src/libbson/src/bson/bson-json.c

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,13 @@ _bson_json_read_integer (bson_json_reader_t *reader, uint64_t val, int64_t sign)
628628
bson_append_int32 (
629629
STACK_BSON_CHILD, key, (int) len, (int) (val * sign));
630630
} else if (sign == -1) {
631+
#if defined(_WIN32) && !defined(__MINGW32__)
632+
// Unary negation of unsigned integer is deliberate.
633+
#pragma warning(suppress : 4146)
631634
bson_append_int64 (STACK_BSON_CHILD, key, (int) len, (int64_t) -val);
635+
#else
636+
bson_append_int64 (STACK_BSON_CHILD, key, (int) len, (int64_t) -val);
637+
#endif // defined(_WIN32) && !defined(__MINGW32__)
632638
} else {
633639
bson_append_int64 (STACK_BSON_CHILD, key, (int) len, (int64_t) val);
634640
}
@@ -735,33 +741,19 @@ _bson_json_parse_double (bson_json_reader_t *reader,
735741
*d = strtod (val, NULL);
736742

737743
#ifdef _MSC_VER
744+
const double pos_inf = INFINITY;
745+
const double neg_inf = -pos_inf;
746+
738747
/* Microsoft's strtod parses "NaN", "Infinity", "-Infinity" as 0 */
739748
if (*d == 0.0) {
740749
if (!_strnicmp (val, "nan", vlen)) {
741-
#ifdef NAN
742750
*d = NAN;
743-
#else
744-
/* Visual Studio 2010 doesn't define NAN or INFINITY
745-
* https://msdn.microsoft.com/en-us/library/w22adx1s(v=vs.100).aspx */
746-
unsigned long nan[2] = {0xffffffff, 0x7fffffff};
747-
*d = *(double *) nan;
748-
#endif
749751
return true;
750752
} else if (!_strnicmp (val, "infinity", vlen)) {
751-
#ifdef INFINITY
752-
*d = INFINITY;
753-
#else
754-
unsigned long inf[2] = {0x00000000, 0x7ff00000};
755-
*d = *(double *) inf;
756-
#endif
753+
*d = pos_inf;
757754
return true;
758755
} else if (!_strnicmp (val, "-infinity", vlen)) {
759-
#ifdef INFINITY
760-
*d = -INFINITY;
761-
#else
762-
unsigned long inf[2] = {0x00000000, 0xfff00000};
763-
*d = *(double *) inf;
764-
#endif
756+
*d = neg_inf;
765757
return true;
766758
}
767759
}
@@ -831,7 +823,7 @@ static bool
831823
_unhexlify_uuid (const char *uuid, uint8_t *out, size_t max)
832824
{
833825
unsigned int byte;
834-
int x = 0;
826+
size_t x = 0;
835827
int i = 0;
836828

837829
BSON_ASSERT (strlen (uuid) == 32);
@@ -1186,8 +1178,9 @@ _bson_json_read_start_map (bson_json_reader_t *reader) /* IN */
11861178
* expected a legacy Binary format. now we see the second "{", so
11871179
* backtrack and parse $type query operator. */
11881180
bson->read_state = BSON_JSON_IN_START_MAP;
1181+
BSON_ASSERT (bson_in_range_unsigned (int, len));
11891182
STACK_PUSH_DOC (bson_append_document_begin (
1190-
STACK_BSON_PARENT, key, len, STACK_BSON_CHILD));
1183+
STACK_BSON_PARENT, key, (int) len, STACK_BSON_CHILD));
11911184
_bson_json_save_map_key (bson, (const uint8_t *) "$type", 5);
11921185
break;
11931186
case BSON_JSON_LF_CODE:
@@ -2233,8 +2226,11 @@ bson_json_reader_read (bson_json_reader_t *reader, /* IN */
22332226

22342227
/* accumulate a key or string value */
22352228
if (reader->json_text_pos != -1) {
2236-
if (reader->json_text_pos < reader->json->pos) {
2237-
accum = BSON_MIN (reader->json->pos - reader->json_text_pos, r);
2229+
if (bson_cmp_less_su (reader->json_text_pos, reader->json->pos)) {
2230+
BSON_ASSERT (
2231+
bson_in_range_unsigned (ssize_t, reader->json->pos));
2232+
accum = BSON_MIN (
2233+
(ssize_t) reader->json->pos - reader->json_text_pos, r);
22382234
/* if this chunk stopped mid-token, buf_offset is how far into
22392235
* our current chunk the token begins. */
22402236
buf_offset = AT_LEAST_0 (reader->json_text_pos - start_pos);

src/libbson/src/bson/bson-memory.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ _aligned_alloc_impl (size_t alignment, size_t num_bytes)
4040
#elif defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
4141
{
4242
void *mem = NULL;
43-
(void) posix_memalign (&mem, alignment, num_bytes);
43+
44+
// Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425.
45+
BSON_MAYBE_UNUSED int ret = posix_memalign (&mem, alignment, num_bytes);
46+
4447
return mem;
4548
}
4649
#else

0 commit comments

Comments
 (0)