Skip to content

CDRIVER-4596 Reducing Warnings - MSVC and MinGW Warnings in libbson #1221

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 23 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1711bed
bson-check-depth.c: address C4018 signedness mismatch warnings
eramongodb Mar 7, 2023
2b24521
bson-decimal128.c: address C4018 signedness mismatch warnings
eramongodb Mar 8, 2023
3e3d494
bson-decimal128.c: address C4267 size_t conversion warnings
eramongodb Mar 7, 2023
ea1d539
bson-iter.c: address C4267 size_t conversion warnings
eramongodb Mar 7, 2023
e5d82c7
bson-json.c: address C4018 signedness mismatch warnings
eramongodb Mar 7, 2023
01eff66
bson-json.c: address C4267 size_t conversion warnings
eramongodb Mar 7, 2023
825c2dd
bson-json.c: address C4146 unsigned unary negation warnings
eramongodb Mar 7, 2023
4296e45
bson-metrics.c: address C4244 narrowing conversion warnings
eramongodb Mar 7, 2023
3a970f4
bson-timegm.c: address C4028 parameter inconsistency warnings
eramongodb Mar 8, 2023
515adc6
bson-timegm.c: address C4244 narrowing conversion warnings
eramongodb Mar 7, 2023
51c7295
bson.c: address C4018 signedness mismatch warnings
eramongodb Mar 7, 2023
93e0999
test-b64.c: address C4267 size_t conversion warnings
eramongodb Mar 7, 2023
a4d808f
test-iso8601.c: address C4267 size_t conversion warnings
eramongodb Mar 7, 2023
151a9ee
test-json.c: address C4244 narrowing conversion warnings
eramongodb Mar 7, 2023
872d53d
test-json.c: address C4267 size_t conversion warnings
eramongodb Mar 7, 2023
8caabb7
bson-memory.c: address -Wunused-result warnings
eramongodb Mar 10, 2023
bba1e86
mongoc-util.c: address scan-build unix.cstring.NullArg warnings
eramongodb Mar 24, 2023
a5d295b
Disable C4756 when compiling with VS 2013 or older
eramongodb Mar 13, 2023
8ef6a84
Remove obsolete checks for INFINITY and NAN
eramongodb Mar 24, 2023
591e498
bson-timegm.c: remove use of register for parameters and variables
eramongodb Mar 24, 2023
02e819a
Format bson-timegm.c
eramongodb Mar 24, 2023
0267a32
Format mongoc-util.c
eramongodb Mar 24, 2023
ad9a023
Fix order of parameters in casts
eramongodb Mar 27, 2023
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
14 changes: 14 additions & 0 deletions src/libbson/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 11 additions & 8 deletions src/libbson/examples/bson-check-depth.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
#include <bson/bson.h>

#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct {
uint32_t depth;
int max_depth;
uint32_t max_depth;
bool valid;
} check_depth_t;

Expand Down Expand Up @@ -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};
Expand All @@ -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);
Expand All @@ -102,26 +104,27 @@ 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]);
fprintf (stderr, "Checks that the depth of the BSON contained in FILE\n");
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) {
Expand Down
8 changes: 6 additions & 2 deletions src/libbson/examples/bson-metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/*
Expand Down
26 changes: 15 additions & 11 deletions src/libbson/src/bson/bson-decimal128.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <string.h>
#include <ctype.h>

#include "bson-cmp.h"
#include "bson-decimal128.h"
#include "bson-types.h"
#include "bson-macros.h"
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 */
Expand All @@ -273,15 +273,16 @@ 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';
} else {
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';
Expand All @@ -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';
Expand Down Expand Up @@ -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 */
Expand Down
7 changes: 6 additions & 1 deletion src/libbson/src/bson/bson-iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
42 changes: 19 additions & 23 deletions src/libbson/src/bson/bson-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/libbson/src/bson/bson-memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading