Skip to content

Commit 67a62bf

Browse files
rcsanchez97vector-of-boolkevinAlbs
authored
CDRIVER-5756 Coverity fixes (#1867)
* check return value of bson_init_static CID: 100057 * check return value of malloc CID: 100087 * add bounds check to ensure int32_t fits in size_t CID: 112395 * check return value of bson_iter_init CID: 115840, 115848, 115851, 115853 * check bounds on return value from sysconf CID: 134019 * check return when calling pthread_once/InitOnceExecuteOnce CID: 138314 * explicit cast to int64_t CID: 138986 * bounds check when summing return from send() CID: 138989 * acquire mutex before modifying topology CID: 156373 * ensure a NULL pointer is not dereferenced CID: 157950 * static cast to ensure proper comparison Co-authored-by: vector-of-bool <[email protected]> * remove wrapping macros * use BSON_ASSERT instead of Boolean check for opt * also check return value of calloc * use bson_malloc rather than plain malloc Co-authored-by: Kevin Albertson <[email protected]> * drop redundant mongo_common_once defitions, use bson_once instead * rather than assert, test against INT_MAX and provide a suitable error message * tighter asserts Co-authored-by: Kevin Albertson <[email protected]> * tighter asserts Co-authored-by: Kevin Albertson <[email protected]> * use bson_free instead of free * tighter asserts * revert inadequate concurrency fix --------- Co-authored-by: vector-of-bool <[email protected]> Co-authored-by: Kevin Albertson <[email protected]>
1 parent afb406d commit 67a62bf

File tree

11 files changed

+46
-39
lines changed

11 files changed

+46
-39
lines changed

src/common/src/common-b64.c

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
*/
4242

4343
#include <common-b64-private.h>
44+
#include <common-thread-private.h>
4445

4546
#include <bson/bson.h>
4647

@@ -262,24 +263,7 @@ static const uint8_t mongoc_b64rmap_space = 0xfe;
262263
static const uint8_t mongoc_b64rmap_invalid = 0xff;
263264

264265
/* initializing the reverse map isn't thread safe, do it in pthread_once */
265-
#if defined(BSON_OS_UNIX)
266-
#include <pthread.h>
267-
#define mongoc_common_once_t pthread_once_t
268-
#define mongoc_common_once pthread_once
269-
#define MONGOC_COMMON_ONCE_FUN(n) void n (void)
270-
#define MONGOC_COMMON_ONCE_RETURN return
271-
#define MONGOC_COMMON_ONCE_INIT PTHREAD_ONCE_INIT
272-
#else
273-
#define mongoc_common_once_t INIT_ONCE
274-
#define MONGOC_COMMON_ONCE_INIT INIT_ONCE_STATIC_INIT
275-
#define mongoc_common_once(o, c) InitOnceExecuteOnce (o, c, NULL, NULL)
276-
#define MONGOC_COMMON_ONCE_FUN(n) \
277-
BOOL CALLBACK MLIB_PRAGMA_IF_MSVC (warning (push)) MLIB_PRAGMA_IF_MSVC (warning (disable : 4100)) \
278-
n (PINIT_ONCE _ignored_a, PVOID _ignored_b, PVOID *_ignored_c) MLIB_PRAGMA_IF_MSVC (warning (pop))
279-
#define MONGOC_COMMON_ONCE_RETURN return true
280-
#endif
281-
282-
static MONGOC_COMMON_ONCE_FUN (bson_b64_initialize_rmap)
266+
static BSON_ONCE_FUN (bson_b64_initialize_rmap)
283267
{
284268
/* Null: end of string, stop parsing */
285269
mongoc_b64rmap[0] = mongoc_b64rmap_end;
@@ -301,7 +285,7 @@ static MONGOC_COMMON_ONCE_FUN (bson_b64_initialize_rmap)
301285
for (uint8_t i = 0; Base64[i] != '\0'; ++i)
302286
mongoc_b64rmap[(uint8_t) Base64[i]] = i;
303287

304-
MONGOC_COMMON_ONCE_RETURN;
288+
BSON_ONCE_RETURN;
305289
}
306290

307291
static int
@@ -516,9 +500,9 @@ mongoc_b64_pton_len (char const *src)
516500
int
517501
mcommon_b64_pton (char const *src, uint8_t *target, size_t targsize)
518502
{
519-
static mongoc_common_once_t once = MONGOC_COMMON_ONCE_INIT;
503+
static bson_once_t once = BSON_ONCE_INIT;
520504

521-
mongoc_common_once (&once, bson_b64_initialize_rmap);
505+
bson_once (&once, bson_b64_initialize_rmap);
522506

523507
if (!src) {
524508
return -1;

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,17 @@ _noop (void)
351351
bson->code_data.in_scope = false; \
352352
} while (0)
353353
#define STACK_POP_DBPOINTER STACK_POP_DOC (_noop ())
354-
#define BASIC_CB_PREAMBLE \
355-
const char *key; \
356-
size_t len; \
357-
bson_json_reader_bson_t *bson = &reader->bson; \
358-
_bson_json_read_fixup_key (bson); \
359-
key = bson->key; \
360-
len = bson->key_buf.len; \
354+
#define BASIC_CB_PREAMBLE \
355+
const char *key; \
356+
size_t len; \
357+
bson_json_reader_bson_t *bson = &reader->bson; \
358+
_bson_json_read_fixup_key (bson); \
359+
key = bson->key; \
360+
len = bson->key_buf.len; \
361+
if (len > INT_MAX) { \
362+
_bson_json_read_set_error (reader, "Failed to read JSON. key size %zu is too large. Max is %d", len, INT_MAX); \
363+
return; \
364+
} \
361365
(void) 0
362366
#define BASIC_CB_BAIL_IF_NOT_NORMAL(_type) \
363367
if (bson->read_state != BSON_JSON_REGULAR) { \
@@ -628,7 +632,7 @@ _bson_json_read_integer (bson_json_reader_t *reader, uint64_t val, int64_t sign)
628632
BASIC_CB_BAIL_IF_NOT_NORMAL ("integer");
629633

630634
if (val <= INT32_MAX || (sign == -1 && val <= (uint64_t) INT32_MAX + 1)) {
631-
bson_append_int32 (STACK_BSON_CHILD, key, (int) len, (int) (val * sign));
635+
bson_append_int32 (STACK_BSON_CHILD, key, (int) len, (int32_t) ((int64_t) val * sign));
632636
} else if (sign == -1) {
633637
#if defined(_WIN32) && !defined(__MINGW32__)
634638
// Unary negation of unsigned integer is deliberate.

src/libbson/src/jsonsl/jsonsl.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,9 +1052,9 @@ void jsonsl_jpr_match_state_init(jsonsl_t jsn,
10521052
if (njprs == 0) {
10531053
return;
10541054
}
1055-
jsn->jprs = (jsonsl_jpr_t *)malloc(sizeof(jsonsl_jpr_t) * njprs);
1055+
jsn->jprs = (jsonsl_jpr_t *) bson_malloc (sizeof (jsonsl_jpr_t) * njprs);
10561056
jsn->jpr_count = njprs;
1057-
jsn->jpr_root = (size_t*)calloc(1, sizeof(size_t) * njprs * jsn->levels_max);
1057+
jsn->jpr_root = (size_t *) bson_malloc0 (sizeof (size_t) * njprs * jsn->levels_max);
10581058
memcpy(jsn->jprs, jprs, sizeof(jsonsl_jpr_t) * njprs);
10591059
/* Set the initial jump table values */
10601060

@@ -1070,8 +1070,8 @@ void jsonsl_jpr_match_state_cleanup(jsonsl_t jsn)
10701070
return;
10711071
}
10721072

1073-
free(jsn->jpr_root);
1074-
free(jsn->jprs);
1073+
bson_free(jsn->jpr_root);
1074+
bson_free(jsn->jprs);
10751075
jsn->jprs = NULL;
10761076
jsn->jpr_root = NULL;
10771077
jsn->jpr_count = 0;

src/libmongoc/src/mongoc/mongoc-client-session.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,9 @@ _max_time_ms_failure (bson_t *reply)
885885
return true;
886886
}
887887

888-
bson_iter_init (&iter, reply);
888+
if (!bson_iter_init (&iter, reply)) {
889+
return false;
890+
}
889891
if (bson_iter_find_descendant (&iter, "writeConcernError.codeName", &descendant) &&
890892
BSON_ITER_HOLDS_UTF8 (&descendant) && 0 == strcmp (bson_iter_utf8 (&descendant, NULL), MAX_TIME_MS_EXPIRED)) {
891893
return true;

src/libmongoc/src/mongoc/mongoc-collection.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -937,8 +937,12 @@ _mongoc_collection_index_keys_equal (const bson_t *expected, const bson_t *actua
937937
bson_iter_t iter_expected;
938938
bson_iter_t iter_actual;
939939

940-
bson_iter_init (&iter_expected, expected);
941-
bson_iter_init (&iter_actual, actual);
940+
if (!bson_iter_init (&iter_expected, expected)) {
941+
return false;
942+
}
943+
if (!bson_iter_init (&iter_actual, actual)) {
944+
return false;
945+
}
942946

943947
while (bson_iter_next (&iter_expected)) {
944948
/* If the key document has fewer items than expected, indexes are unequal

src/libmongoc/src/mongoc/mongoc-counters.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ mongoc_counters_calc_size (void)
119119
if (mlib_cmp (size, >, pg_sz)) {
120120
return size;
121121
} else {
122+
BSON_ASSERT (pg_sz > 0);
122123
return (size_t) pg_sz;
123124
}
124125
#else

src/libmongoc/src/mongoc/mongoc-server-description.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ mongoc_server_description_new_copy (const mongoc_server_description_t *descripti
794794
const uint8_t *data = bson_get_data (&copy->last_hello_response) + offset; \
795795
uint32_t len = description->FIELD.len; \
796796
MONGOC_DEBUG_ASSERT (offset + len <= copy->last_hello_response.len); \
797-
bson_init_static (&copy->FIELD, data, len); \
797+
BSON_ASSERT (bson_init_static (&copy->FIELD, data, len)); \
798798
} else { \
799799
bson_init (&copy->FIELD); \
800800
} \

src/libmongoc/src/mongoc/mongoc-socket.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,7 @@ _mongoc_socket_try_sendv_slow (mongoc_socket_t *sock, /* IN */
12071207
RETURN (ret ? ret : -1);
12081208
}
12091209

1210+
BSON_ASSERT (mlib_cmp (wrote, <=, SSIZE_MAX - ret));
12101211
ret += wrote;
12111212

12121213
if (mlib_cmp (wrote, !=, iov[i].iov_len)) {

src/libmongoc/src/mongoc/mongoc-stream-tls-openssl.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,9 @@ create_stream_with_ctx (
829829
mongoc_stream_t *
830830
mongoc_stream_tls_openssl_new (mongoc_stream_t *base_stream, const char *host, mongoc_ssl_opt_t *opt, int client)
831831
{
832+
BSON_ASSERT_PARAM (base_stream);
833+
BSON_ASSERT_PARAM (opt);
834+
832835
SSL_CTX *ssl_ctx = _mongoc_openssl_ctx_new (opt);
833836

834837
if (!ssl_ctx) {

src/libmongoc/src/mongoc/mongoc-uri.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,10 @@ mongoc_uri_options_validate_names (const bson_t *a, const bson_t *b, bson_error_
904904
/* Scan `a` looking for deprecated names
905905
* where the canonical name was also used in `a`,
906906
* or was used in `b`. */
907-
bson_iter_init (&key_iter, a);
907+
if (!bson_iter_init (&key_iter, a)) {
908+
return false;
909+
}
910+
908911
while (bson_iter_next (&key_iter)) {
909912
key = bson_iter_key (&key_iter);
910913
value = bson_iter_utf8_unsafe (&key_iter, &value_len);
@@ -966,7 +969,10 @@ mongoc_uri_apply_options (mongoc_uri_t *uri, const bson_t *options, bool from_dn
966969
size_t value_len;
967970
bool bval;
968971

969-
bson_iter_init (&iter, options);
972+
if (!bson_iter_init (&iter, options)) {
973+
return false;
974+
}
975+
970976
while (bson_iter_next (&iter)) {
971977
key = bson_iter_key (&iter);
972978
canon = mongoc_uri_canonicalize_option (key);

0 commit comments

Comments
 (0)