diff --git a/NEWS b/NEWS index 04276a05e72..12c4895cc5b 100644 --- a/NEWS +++ b/NEWS @@ -1,10 +1,6 @@ libmongoc 1.29.0 (unreleased) ============================= -Deprecated: - - * Compiling with `BSON_MEMCHECK` defined is deprecated. - * `bson_string_t` and associated functions. Platform Support: diff --git a/src/common/bson-dsl.h b/src/common/bson-dsl.h index a20c9d58b7f..340fea2ede6 100644 --- a/src/common/bson-dsl.h +++ b/src/common/bson-dsl.h @@ -14,6 +14,7 @@ */ #include "bson/bson.h" +#include enum { /// Toggle this value to enable/disable debug output for all bsonDSL @@ -118,7 +119,7 @@ BSON_IF_GNU_LIKE (_Pragma ("GCC diagnostic ignored \"-Wshadow\"")) _bsonDSL_begin ("\"%s\" => [%s]", String, _bsonDSL_strElide (30, Element)); \ const char *_bbString = (String); \ const uint64_t length = (Len); \ - if (bson_in_range_unsigned (int, length)) { \ + if (mcommon_in_range_unsigned (int, length)) { \ _bbCtx.key = _bbString; \ _bbCtx.key_len = (int) length; \ _bsonValueOperation (Element); \ diff --git a/src/common/common-atomic-private.h b/src/common/common-atomic-private.h new file mode 100644 index 00000000000..b5438e3a8df --- /dev/null +++ b/src/common/common-atomic-private.h @@ -0,0 +1,621 @@ +/* + * Copyright 2009-present MongoDB, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "common-prelude.h" + + +#ifndef MONGO_C_DRIVER_COMMON_ATOMIC_PRIVATE_H +#define MONGO_C_DRIVER_COMMON_ATOMIC_PRIVATE_H + + +#include // BSON_INLINE + +#ifdef _MSC_VER +#include +#endif + + +enum mcommon_memory_order { + mcommon_memory_order_seq_cst, + mcommon_memory_order_acquire, + mcommon_memory_order_release, + mcommon_memory_order_relaxed, + mcommon_memory_order_acq_rel, + mcommon_memory_order_consume, +}; + +#if defined(_M_ARM) /* MSVC memorder atomics are only avail on ARM */ +#define MSVC_MEMORDER_SUFFIX(X) X +#else +#define MSVC_MEMORDER_SUFFIX(X) +#endif + +#if defined(USE_LEGACY_GCC_ATOMICS) || (!defined(__clang__) && __GNUC__ == 4) || defined(__xlC__) +#define MCOMMON_USE_LEGACY_GCC_ATOMICS +#else +#undef MCOMMON_USE_LEGACY_GCC_ATOMICS +#endif + +/* Not all GCC-like compilers support the current __atomic built-ins. Older + * GCC (pre-5) used different built-ins named with the __sync prefix. When + * compiling with such older GCC versions, it is necessary to use the applicable + * functions, which requires redefining BSON_IF_GNU_LIKE and defining the + * additional MCOMMON_IF_GNU_LEGACY_ATOMICS macro here. */ +#ifdef MCOMMON_USE_LEGACY_GCC_ATOMICS +#undef BSON_IF_GNU_LIKE +#define BSON_IF_GNU_LIKE(...) +#define BSON_IF_MSVC(...) +#define MCOMMON_IF_GNU_LEGACY_ATOMICS(...) __VA_ARGS__ +#else +#define MCOMMON_IF_GNU_LEGACY_ATOMICS(...) +#endif + +/* CDRIVER-4229 zSeries with gcc 4.8.4 produces illegal instructions for int and + * int32 atomic intrinsics. */ +#if defined(__s390__) || defined(__s390x__) || defined(__zarch__) +#define MCOMMON_EMULATE_INT32 +#define MCOMMON_EMULATE_INT +#endif + +/* CDRIVER-4264 Contrary to documentation, VS 2013 targeting x86 does not + * correctly/consistently provide _InterlockedPointerExchange. */ +#if defined(_MSC_VER) && _MSC_VER < 1900 && defined(_M_IX86) +#define MCOMMON_EMULATE_PTR +#endif + +#define DEF_ATOMIC_OP(MSVC_Intrinsic, GNU_Intrinsic, GNU_Legacy_Intrinsic, Order, ...) \ + do { \ + switch (Order) { \ + case mcommon_memory_order_acq_rel: \ + BSON_IF_MSVC (return MSVC_Intrinsic (__VA_ARGS__);) \ + BSON_IF_GNU_LIKE (return GNU_Intrinsic (__VA_ARGS__, __ATOMIC_ACQ_REL);) \ + MCOMMON_IF_GNU_LEGACY_ATOMICS (return GNU_Legacy_Intrinsic (__VA_ARGS__);) \ + case mcommon_memory_order_seq_cst: \ + BSON_IF_MSVC (return MSVC_Intrinsic (__VA_ARGS__);) \ + BSON_IF_GNU_LIKE (return GNU_Intrinsic (__VA_ARGS__, __ATOMIC_SEQ_CST);) \ + MCOMMON_IF_GNU_LEGACY_ATOMICS (return GNU_Legacy_Intrinsic (__VA_ARGS__);) \ + case mcommon_memory_order_acquire: \ + BSON_IF_MSVC (return BSON_CONCAT (MSVC_Intrinsic, MSVC_MEMORDER_SUFFIX (_acq)) (__VA_ARGS__);) \ + BSON_IF_GNU_LIKE (return GNU_Intrinsic (__VA_ARGS__, __ATOMIC_ACQUIRE);) \ + MCOMMON_IF_GNU_LEGACY_ATOMICS (return GNU_Legacy_Intrinsic (__VA_ARGS__);) \ + case mcommon_memory_order_consume: \ + BSON_IF_MSVC (return BSON_CONCAT (MSVC_Intrinsic, MSVC_MEMORDER_SUFFIX (_acq)) (__VA_ARGS__);) \ + BSON_IF_GNU_LIKE (return GNU_Intrinsic (__VA_ARGS__, __ATOMIC_CONSUME);) \ + MCOMMON_IF_GNU_LEGACY_ATOMICS (return GNU_Legacy_Intrinsic (__VA_ARGS__);) \ + case mcommon_memory_order_release: \ + BSON_IF_MSVC (return BSON_CONCAT (MSVC_Intrinsic, MSVC_MEMORDER_SUFFIX (_rel)) (__VA_ARGS__);) \ + BSON_IF_GNU_LIKE (return GNU_Intrinsic (__VA_ARGS__, __ATOMIC_RELEASE);) \ + MCOMMON_IF_GNU_LEGACY_ATOMICS (return GNU_Legacy_Intrinsic (__VA_ARGS__);) \ + case mcommon_memory_order_relaxed: \ + BSON_IF_MSVC (return BSON_CONCAT (MSVC_Intrinsic, MSVC_MEMORDER_SUFFIX (_nf)) (__VA_ARGS__);) \ + BSON_IF_GNU_LIKE (return GNU_Intrinsic (__VA_ARGS__, __ATOMIC_RELAXED);) \ + MCOMMON_IF_GNU_LEGACY_ATOMICS (return GNU_Legacy_Intrinsic (__VA_ARGS__);) \ + default: \ + BSON_UNREACHABLE ("Invalid mcommon_memory_order value"); \ + } \ + } while (0) + + +#define DEF_ATOMIC_CMPEXCH_STRONG(VCSuffix1, VCSuffix2, GNU_MemOrder, Ptr, ExpectActualVar, NewValue) \ + do { \ + BSON_IF_MSVC (ExpectActualVar = BSON_CONCAT3 (_InterlockedCompareExchange, VCSuffix1, VCSuffix2) ( \ + Ptr, NewValue, ExpectActualVar);) \ + BSON_IF_GNU_LIKE ((void) __atomic_compare_exchange_n (Ptr, \ + &ExpectActualVar, \ + NewValue, \ + false, /* Not weak */ \ + GNU_MemOrder, \ + GNU_MemOrder);) \ + MCOMMON_IF_GNU_LEGACY_ATOMICS (__typeof__ (ExpectActualVar) _val; \ + _val = __sync_val_compare_and_swap (Ptr, ExpectActualVar, NewValue); \ + ExpectActualVar = _val;) \ + } while (0) + + +#define DEF_ATOMIC_CMPEXCH_WEAK(VCSuffix1, VCSuffix2, GNU_MemOrder, Ptr, ExpectActualVar, NewValue) \ + do { \ + BSON_IF_MSVC (ExpectActualVar = BSON_CONCAT3 (_InterlockedCompareExchange, VCSuffix1, VCSuffix2) ( \ + Ptr, NewValue, ExpectActualVar);) \ + BSON_IF_GNU_LIKE ((void) __atomic_compare_exchange_n (Ptr, \ + &ExpectActualVar, \ + NewValue, \ + true, /* Yes weak */ \ + GNU_MemOrder, \ + GNU_MemOrder);) \ + MCOMMON_IF_GNU_LEGACY_ATOMICS (__typeof__ (ExpectActualVar) _val; \ + _val = __sync_val_compare_and_swap (Ptr, ExpectActualVar, NewValue); \ + ExpectActualVar = _val;) \ + } while (0) + + +#define DECL_ATOMIC_INTEGRAL(NamePart, Type, VCIntrinSuffix) \ + static BSON_INLINE Type mcommon_atomic_##NamePart##_fetch_add ( \ + Type volatile *a, Type addend, enum mcommon_memory_order ord) \ + { \ + DEF_ATOMIC_OP (BSON_CONCAT (_InterlockedExchangeAdd, VCIntrinSuffix), \ + __atomic_fetch_add, \ + __sync_fetch_and_add, \ + ord, \ + a, \ + addend); \ + } \ + \ + static BSON_INLINE Type mcommon_atomic_##NamePart##_fetch_sub ( \ + Type volatile *a, Type subtrahend, enum mcommon_memory_order ord) \ + { \ + /* MSVC doesn't have a subtract intrinsic, so just reuse addition */ \ + BSON_IF_MSVC (return mcommon_atomic_##NamePart##_fetch_add (a, -subtrahend, ord);) \ + BSON_IF_GNU_LIKE (DEF_ATOMIC_OP (~, __atomic_fetch_sub, ~, ord, a, subtrahend);) \ + MCOMMON_IF_GNU_LEGACY_ATOMICS (DEF_ATOMIC_OP (~, ~, __sync_fetch_and_sub, ord, a, subtrahend);) \ + } \ + \ + static BSON_INLINE Type mcommon_atomic_##NamePart##_fetch (Type volatile const *a, enum mcommon_memory_order order) \ + { \ + /* MSVC doesn't have a load intrinsic, so just add zero */ \ + BSON_IF_MSVC (return mcommon_atomic_##NamePart##_fetch_add ((Type volatile *) a, 0, order);) \ + /* GNU doesn't want RELEASE order for the fetch operation, so we can't \ + * just use DEF_ATOMIC_OP. */ \ + BSON_IF_GNU_LIKE (switch (order) { \ + case mcommon_memory_order_release: /* Fall back to seqcst */ \ + case mcommon_memory_order_acq_rel: /* Fall back to seqcst */ \ + case mcommon_memory_order_seq_cst: \ + return __atomic_load_n (a, __ATOMIC_SEQ_CST); \ + case mcommon_memory_order_acquire: \ + return __atomic_load_n (a, __ATOMIC_ACQUIRE); \ + case mcommon_memory_order_consume: \ + return __atomic_load_n (a, __ATOMIC_CONSUME); \ + case mcommon_memory_order_relaxed: \ + return __atomic_load_n (a, __ATOMIC_RELAXED); \ + default: \ + BSON_UNREACHABLE ("Invalid mcommon_memory_order value"); \ + }) \ + MCOMMON_IF_GNU_LEGACY_ATOMICS ({ \ + BSON_UNUSED (order); \ + __sync_synchronize (); \ + return *a; \ + }) \ + } \ + \ + static BSON_INLINE Type mcommon_atomic_##NamePart##_exchange ( \ + Type volatile *a, Type value, enum mcommon_memory_order ord) \ + { \ + BSON_IF_MSVC (DEF_ATOMIC_OP (BSON_CONCAT (_InterlockedExchange, VCIntrinSuffix), ~, ~, ord, a, value);) \ + /* GNU doesn't want CONSUME order for the exchange operation, so we \ + * cannot use DEF_ATOMIC_OP. */ \ + BSON_IF_GNU_LIKE (switch (ord) { \ + case mcommon_memory_order_acq_rel: \ + return __atomic_exchange_n (a, value, __ATOMIC_ACQ_REL); \ + case mcommon_memory_order_release: \ + return __atomic_exchange_n (a, value, __ATOMIC_RELEASE); \ + case mcommon_memory_order_seq_cst: \ + return __atomic_exchange_n (a, value, __ATOMIC_SEQ_CST); \ + case mcommon_memory_order_consume: /* Fall back to acquire */ \ + case mcommon_memory_order_acquire: \ + return __atomic_exchange_n (a, value, __ATOMIC_ACQUIRE); \ + case mcommon_memory_order_relaxed: \ + return __atomic_exchange_n (a, value, __ATOMIC_RELAXED); \ + default: \ + BSON_UNREACHABLE ("Invalid mcommon_memory_order value"); \ + }) \ + MCOMMON_IF_GNU_LEGACY_ATOMICS (BSON_UNUSED (ord); return __sync_val_compare_and_swap (a, *a, value);) \ + } \ + \ + static BSON_INLINE Type mcommon_atomic_##NamePart##_compare_exchange_strong ( \ + Type volatile *a, Type expect, Type new_value, enum mcommon_memory_order ord) \ + { \ + Type actual = expect; \ + switch (ord) { \ + case mcommon_memory_order_release: \ + case mcommon_memory_order_acq_rel: \ + case mcommon_memory_order_seq_cst: \ + DEF_ATOMIC_CMPEXCH_STRONG (VCIntrinSuffix, , __ATOMIC_SEQ_CST, a, actual, new_value); \ + break; \ + case mcommon_memory_order_acquire: \ + DEF_ATOMIC_CMPEXCH_STRONG ( \ + VCIntrinSuffix, MSVC_MEMORDER_SUFFIX (_acq), __ATOMIC_ACQUIRE, a, actual, new_value); \ + break; \ + case mcommon_memory_order_consume: \ + DEF_ATOMIC_CMPEXCH_STRONG ( \ + VCIntrinSuffix, MSVC_MEMORDER_SUFFIX (_acq), __ATOMIC_CONSUME, a, actual, new_value); \ + break; \ + case mcommon_memory_order_relaxed: \ + DEF_ATOMIC_CMPEXCH_STRONG ( \ + VCIntrinSuffix, MSVC_MEMORDER_SUFFIX (_nf), __ATOMIC_RELAXED, a, actual, new_value); \ + break; \ + default: \ + BSON_UNREACHABLE ("Invalid mcommon_memory_order value"); \ + } \ + return actual; \ + } \ + \ + static BSON_INLINE Type mcommon_atomic_##NamePart##_compare_exchange_weak ( \ + Type volatile *a, Type expect, Type new_value, enum mcommon_memory_order ord) \ + { \ + Type actual = expect; \ + switch (ord) { \ + case mcommon_memory_order_release: \ + case mcommon_memory_order_acq_rel: \ + case mcommon_memory_order_seq_cst: \ + DEF_ATOMIC_CMPEXCH_WEAK (VCIntrinSuffix, , __ATOMIC_SEQ_CST, a, actual, new_value); \ + break; \ + case mcommon_memory_order_acquire: \ + DEF_ATOMIC_CMPEXCH_WEAK ( \ + VCIntrinSuffix, MSVC_MEMORDER_SUFFIX (_acq), __ATOMIC_ACQUIRE, a, actual, new_value); \ + break; \ + case mcommon_memory_order_consume: \ + DEF_ATOMIC_CMPEXCH_WEAK ( \ + VCIntrinSuffix, MSVC_MEMORDER_SUFFIX (_acq), __ATOMIC_CONSUME, a, actual, new_value); \ + break; \ + case mcommon_memory_order_relaxed: \ + DEF_ATOMIC_CMPEXCH_WEAK (VCIntrinSuffix, MSVC_MEMORDER_SUFFIX (_nf), __ATOMIC_RELAXED, a, actual, new_value); \ + break; \ + default: \ + BSON_UNREACHABLE ("Invalid mcommon_memory_order value"); \ + } \ + return actual; \ + } + +#define DECL_ATOMIC_STDINT(Name, VCSuffix) DECL_ATOMIC_INTEGRAL (Name, Name##_t, VCSuffix) + +#if defined(_MSC_VER) || defined(MCOMMON_USE_LEGACY_GCC_ATOMICS) +/* MSVC and GCC require built-in types (not typedefs) for their atomic + * intrinsics. */ +#if defined(_MSC_VER) +#define DECL_ATOMIC_INTEGRAL_INT8 char +#define DECL_ATOMIC_INTEGRAL_INT32 long +#define DECL_ATOMIC_INTEGRAL_INT long +#else +#define DECL_ATOMIC_INTEGRAL_INT8 signed char +#define DECL_ATOMIC_INTEGRAL_INT32 int +#define DECL_ATOMIC_INTEGRAL_INT int +#endif +DECL_ATOMIC_INTEGRAL (int8, DECL_ATOMIC_INTEGRAL_INT8, 8) +DECL_ATOMIC_INTEGRAL (int16, short, 16) +#if !defined(MCOMMON_EMULATE_INT32) +DECL_ATOMIC_INTEGRAL (int32, DECL_ATOMIC_INTEGRAL_INT32, ) +#endif +#if !defined(MCOMMON_EMULATE_INT) +DECL_ATOMIC_INTEGRAL (int, DECL_ATOMIC_INTEGRAL_INT, ) +#endif +#else +/* Other compilers that we support provide generic intrinsics */ +DECL_ATOMIC_STDINT (int8, 8) +DECL_ATOMIC_STDINT (int16, 16) +#if !defined(MCOMMON_EMULATE_INT32) +DECL_ATOMIC_STDINT (int32, ) +#endif +#if !defined(MCOMMON_EMULATE_INT) +DECL_ATOMIC_INTEGRAL (int, int, ) +#endif +#endif + +#ifndef DECL_ATOMIC_INTEGRAL_INT32 +#define DECL_ATOMIC_INTEGRAL_INT32 int32_t +#endif + +#define _mcommon_emul_atomic_int64_fetch_add COMMON_NAME (emul_atomic_int64_fetch_add) +#define _mcommon_emul_atomic_int64_exchange COMMON_NAME (emul_atomic_int64_exchange) +#define _mcommon_emul_atomic_int64_compare_exchange_strong COMMON_NAME (emul_atomic_int64_compare_exchange_strong) +#define _mcommon_emul_atomic_int64_compare_exchange_weak COMMON_NAME (emul_atomic_int64_compare_exchange_weak) +#define _mcommon_emul_atomic_int32_fetch_add COMMON_NAME (emul_atomic_int32_fetch_add) +#define _mcommon_emul_atomic_int32_exchange COMMON_NAME (emul_atomic_int32_exchange) +#define _mcommon_emul_atomic_int32_compare_exchange_strong COMMON_NAME (emul_atomic_int32_compare_exchange_strong) +#define _mcommon_emul_atomic_int32_compare_exchange_weak COMMON_NAME (emul_atomic_int32_compare_exchange_weak) +#define _mcommon_emul_atomic_int_fetch_add COMMON_NAME (emul_atomic_int_fetch_add) +#define _mcommon_emul_atomic_int_exchange COMMON_NAME (emul_atomic_int_exchange) +#define _mcommon_emul_atomic_int_compare_exchange_strong COMMON_NAME (emul_atomic_int_compare_exchange_strong) +#define _mcommon_emul_atomic_int_compare_exchange_weak COMMON_NAME (emul_atomic_int_compare_exchange_weak) +#define _mcommon_emul_atomic_ptr_exchange COMMON_NAME (emul_atomic_ptr_exchange) +#define mcommon_thrd_yield COMMON_NAME (thrd_yield) + +int64_t +_mcommon_emul_atomic_int64_fetch_add (int64_t volatile *val, int64_t v, enum mcommon_memory_order); +int64_t +_mcommon_emul_atomic_int64_exchange (int64_t volatile *val, int64_t v, enum mcommon_memory_order); +int64_t +_mcommon_emul_atomic_int64_compare_exchange_strong (int64_t volatile *val, + int64_t expect_value, + int64_t new_value, + enum mcommon_memory_order); + +int64_t +_mcommon_emul_atomic_int64_compare_exchange_weak (int64_t volatile *val, + int64_t expect_value, + int64_t new_value, + enum mcommon_memory_order); + +int32_t +_mcommon_emul_atomic_int32_fetch_add (int32_t volatile *val, int32_t v, enum mcommon_memory_order); +int32_t +_mcommon_emul_atomic_int32_exchange (int32_t volatile *val, int32_t v, enum mcommon_memory_order); +int32_t +_mcommon_emul_atomic_int32_compare_exchange_strong (int32_t volatile *val, + int32_t expect_value, + int32_t new_value, + enum mcommon_memory_order); + +int32_t +_mcommon_emul_atomic_int32_compare_exchange_weak (int32_t volatile *val, + int32_t expect_value, + int32_t new_value, + enum mcommon_memory_order); + +int +_mcommon_emul_atomic_int_fetch_add (int volatile *val, int v, enum mcommon_memory_order); +int +_mcommon_emul_atomic_int_exchange (int volatile *val, int v, enum mcommon_memory_order); +int +_mcommon_emul_atomic_int_compare_exchange_strong (int volatile *val, + int expect_value, + int new_value, + enum mcommon_memory_order); + +int +_mcommon_emul_atomic_int_compare_exchange_weak (int volatile *val, + int expect_value, + int new_value, + enum mcommon_memory_order); + +void * +_mcommon_emul_atomic_ptr_exchange (void *volatile *val, void *v, enum mcommon_memory_order); + +void +mcommon_thrd_yield (void); + +#if (defined(_MSC_VER) && !defined(_M_IX86)) || (defined(__LP64__) && __LP64__) +/* (64-bit intrinsics are only available in x64) */ +#ifdef _MSC_VER +DECL_ATOMIC_INTEGRAL (int64, __int64, 64) +#else +DECL_ATOMIC_STDINT (int64, 64) +#endif +#else +static BSON_INLINE int64_t +mcommon_atomic_int64_fetch (const int64_t volatile *val, enum mcommon_memory_order order) +{ + return _mcommon_emul_atomic_int64_fetch_add ((int64_t volatile *) val, 0, order); +} + +static BSON_INLINE int64_t +mcommon_atomic_int64_fetch_add (int64_t volatile *val, int64_t v, enum mcommon_memory_order order) +{ + return _mcommon_emul_atomic_int64_fetch_add (val, v, order); +} + +static BSON_INLINE int64_t +mcommon_atomic_int64_fetch_sub (int64_t volatile *val, int64_t v, enum mcommon_memory_order order) +{ + return _mcommon_emul_atomic_int64_fetch_add (val, -v, order); +} + +static BSON_INLINE int64_t +mcommon_atomic_int64_exchange (int64_t volatile *val, int64_t v, enum mcommon_memory_order order) +{ + return _mcommon_emul_atomic_int64_exchange (val, v, order); +} + +static BSON_INLINE int64_t +mcommon_atomic_int64_compare_exchange_strong (int64_t volatile *val, + int64_t expect_value, + int64_t new_value, + enum mcommon_memory_order order) +{ + return _mcommon_emul_atomic_int64_compare_exchange_strong (val, expect_value, new_value, order); +} + +static BSON_INLINE int64_t +mcommon_atomic_int64_compare_exchange_weak (int64_t volatile *val, + int64_t expect_value, + int64_t new_value, + enum mcommon_memory_order order) +{ + return _mcommon_emul_atomic_int64_compare_exchange_weak (val, expect_value, new_value, order); +} +#endif + +#if defined(MCOMMON_EMULATE_INT32) +static BSON_INLINE int32_t +mcommon_atomic_int32_fetch (const int32_t volatile *val, enum mcommon_memory_order order) +{ + return _mcommon_emul_atomic_int32_fetch_add ((int32_t volatile *) val, 0, order); +} + +static BSON_INLINE int32_t +mcommon_atomic_int32_fetch_add (int32_t volatile *val, int32_t v, enum mcommon_memory_order order) +{ + return _mcommon_emul_atomic_int32_fetch_add (val, v, order); +} + +static BSON_INLINE int32_t +mcommon_atomic_int32_fetch_sub (int32_t volatile *val, int32_t v, enum mcommon_memory_order order) +{ + return _mcommon_emul_atomic_int32_fetch_add (val, -v, order); +} + +static BSON_INLINE int32_t +mcommon_atomic_int32_exchange (int32_t volatile *val, int32_t v, enum mcommon_memory_order order) +{ + return _mcommon_emul_atomic_int32_exchange (val, v, order); +} + +static BSON_INLINE int32_t +mcommon_atomic_int32_compare_exchange_strong (int32_t volatile *val, + int32_t expect_value, + int32_t new_value, + enum mcommon_memory_order order) +{ + return _mcommon_emul_atomic_int32_compare_exchange_strong (val, expect_value, new_value, order); +} + +static BSON_INLINE int32_t +mcommon_atomic_int32_compare_exchange_weak (int32_t volatile *val, + int32_t expect_value, + int32_t new_value, + enum mcommon_memory_order order) +{ + return _mcommon_emul_atomic_int32_compare_exchange_weak (val, expect_value, new_value, order); +} +#endif /* MCOMMON_EMULATE_INT32 */ + +#if defined(MCOMMON_EMULATE_INT) +static BSON_INLINE int +mcommon_atomic_int_fetch (const int volatile *val, enum mcommon_memory_order order) +{ + return _mcommon_emul_atomic_int_fetch_add ((int volatile *) val, 0, order); +} + +static BSON_INLINE int +mcommon_atomic_int_fetch_add (int volatile *val, int v, enum mcommon_memory_order order) +{ + return _mcommon_emul_atomic_int_fetch_add (val, v, order); +} + +static BSON_INLINE int +mcommon_atomic_int_fetch_sub (int volatile *val, int v, enum mcommon_memory_order order) +{ + return _mcommon_emul_atomic_int_fetch_add (val, -v, order); +} + +static BSON_INLINE int +mcommon_atomic_int_exchange (int volatile *val, int v, enum mcommon_memory_order order) +{ + return _mcommon_emul_atomic_int_exchange (val, v, order); +} + +static BSON_INLINE int +mcommon_atomic_int_compare_exchange_strong (int volatile *val, + int expect_value, + int new_value, + enum mcommon_memory_order order) +{ + return _mcommon_emul_atomic_int_compare_exchange_strong (val, expect_value, new_value, order); +} + +static BSON_INLINE int +mcommon_atomic_int_compare_exchange_weak (int volatile *val, + int expect_value, + int new_value, + enum mcommon_memory_order order) +{ + return _mcommon_emul_atomic_int_compare_exchange_weak (val, expect_value, new_value, order); +} +#endif /* MCOMMON_EMULATE_INT */ + +static BSON_INLINE void * +mcommon_atomic_ptr_exchange (void *volatile *ptr, void *new_value, enum mcommon_memory_order ord) +{ +#if defined(MCOMMON_EMULATE_PTR) + return _mcommon_emul_atomic_ptr_exchange (ptr, new_value, ord); +#elif defined(MCOMMON_USE_LEGACY_GCC_ATOMICS) + /* The older __sync_val_compare_and_swap also takes oldval */ + DEF_ATOMIC_OP (_InterlockedExchangePointer, , __sync_val_compare_and_swap, ord, ptr, *ptr, new_value); +#else + DEF_ATOMIC_OP (_InterlockedExchangePointer, __atomic_exchange_n, , ord, ptr, new_value); +#endif +} + +static BSON_INLINE void * +mcommon_atomic_ptr_compare_exchange_strong (void *volatile *ptr, + void *expect, + void *new_value, + enum mcommon_memory_order ord) +{ + switch (ord) { + case mcommon_memory_order_release: + case mcommon_memory_order_acq_rel: + case mcommon_memory_order_seq_cst: + DEF_ATOMIC_CMPEXCH_STRONG (Pointer, , __ATOMIC_SEQ_CST, ptr, expect, new_value); + return expect; + case mcommon_memory_order_relaxed: + DEF_ATOMIC_CMPEXCH_STRONG (Pointer, MSVC_MEMORDER_SUFFIX (_nf), __ATOMIC_RELAXED, ptr, expect, new_value); + return expect; + case mcommon_memory_order_consume: + DEF_ATOMIC_CMPEXCH_STRONG (Pointer, MSVC_MEMORDER_SUFFIX (_acq), __ATOMIC_CONSUME, ptr, expect, new_value); + return expect; + case mcommon_memory_order_acquire: + DEF_ATOMIC_CMPEXCH_STRONG (Pointer, MSVC_MEMORDER_SUFFIX (_acq), __ATOMIC_ACQUIRE, ptr, expect, new_value); + return expect; + default: + BSON_UNREACHABLE ("Invalid mcommon_memory_order value"); + } +} + + +static BSON_INLINE void * +mcommon_atomic_ptr_compare_exchange_weak (void *volatile *ptr, + void *expect, + void *new_value, + enum mcommon_memory_order ord) +{ + switch (ord) { + case mcommon_memory_order_release: + case mcommon_memory_order_acq_rel: + case mcommon_memory_order_seq_cst: + DEF_ATOMIC_CMPEXCH_WEAK (Pointer, , __ATOMIC_SEQ_CST, ptr, expect, new_value); + return expect; + case mcommon_memory_order_relaxed: + DEF_ATOMIC_CMPEXCH_WEAK (Pointer, MSVC_MEMORDER_SUFFIX (_nf), __ATOMIC_RELAXED, ptr, expect, new_value); + return expect; + case mcommon_memory_order_consume: + DEF_ATOMIC_CMPEXCH_WEAK (Pointer, MSVC_MEMORDER_SUFFIX (_acq), __ATOMIC_CONSUME, ptr, expect, new_value); + return expect; + case mcommon_memory_order_acquire: + DEF_ATOMIC_CMPEXCH_WEAK (Pointer, MSVC_MEMORDER_SUFFIX (_acq), __ATOMIC_ACQUIRE, ptr, expect, new_value); + return expect; + default: + BSON_UNREACHABLE ("Invalid mcommon_memory_order value"); + } +} + + +static BSON_INLINE void * +mcommon_atomic_ptr_fetch (void *volatile const *ptr, enum mcommon_memory_order ord) +{ + return mcommon_atomic_ptr_compare_exchange_strong ((void *volatile *) ptr, NULL, NULL, ord); +} + +#undef DECL_ATOMIC_STDINT +#undef DECL_ATOMIC_INTEGRAL +#undef DEF_ATOMIC_OP +#undef DEF_ATOMIC_CMPEXCH_STRONG +#undef DEF_ATOMIC_CMPEXCH_WEAK +#undef MSVC_MEMORDER_SUFFIX + +/** + * @brief Generate a full-fence memory barrier at the call site. + */ +static BSON_INLINE void +mcommon_atomic_thread_fence (void) +{ + BSON_IF_MSVC (MemoryBarrier ();) + BSON_IF_GNU_LIKE (__sync_synchronize ();) + MCOMMON_IF_GNU_LEGACY_ATOMICS (__sync_synchronize ();) +} + +#ifdef MCOMMON_USE_LEGACY_GCC_ATOMICS +#undef BSON_IF_GNU_LIKE +#define BSON_IF_GNU_LIKE(...) __VA_ARGS__ +#endif +#undef MCOMMON_IF_GNU_LEGACY_ATOMICS +#undef MCOMMON_USE_LEGACY_GCC_ATOMICS + + +#undef MCOMMON_EMULATE_PTR +#undef MCOMMON_EMULATE_INT32 +#undef MCOMMON_EMULATE_INT + + +#endif /* MONGO_C_DRIVER_COMMON_ATOMIC_PRIVATE_H */ diff --git a/src/common/common-atomic.c b/src/common/common-atomic.c new file mode 100644 index 00000000000..d708f2b8ce9 --- /dev/null +++ b/src/common/common-atomic.c @@ -0,0 +1,251 @@ +/* + * Copyright 2009-present MongoDB, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include + +#ifdef BSON_OS_UNIX +/* For sched_yield() */ +#include +#endif + +void +mcommon_thrd_yield (void) +{ + BSON_IF_WINDOWS (SwitchToThread ();) + BSON_IF_POSIX (sched_yield ();) +} + +/** + * Some platforms do not support compiler intrinsics for atomic operations. + * We emulate that here using a spin lock and regular arithmetic operations + */ +static int8_t gEmulAtomicLock = 0; + +static void +_lock_emul_atomic (void) +{ + int i; + if (mcommon_atomic_int8_compare_exchange_weak (&gEmulAtomicLock, 0, 1, mcommon_memory_order_acquire) == 0) { + /* Successfully took the spinlock */ + return; + } + /* Failed. Try taking ten more times, then begin sleeping. */ + for (i = 0; i < 10; ++i) { + if (mcommon_atomic_int8_compare_exchange_weak (&gEmulAtomicLock, 0, 1, mcommon_memory_order_acquire) == 0) { + /* Succeeded in taking the lock */ + return; + } + } + /* Still don't have the lock. Spin and yield */ + while (mcommon_atomic_int8_compare_exchange_weak (&gEmulAtomicLock, 0, 1, mcommon_memory_order_acquire) != 0) { + mcommon_thrd_yield (); + } +} + +static void +_unlock_emul_atomic (void) +{ + int64_t rv = mcommon_atomic_int8_exchange (&gEmulAtomicLock, 0, mcommon_memory_order_release); + BSON_ASSERT (rv == 1 && "Released atomic lock while not holding it"); +} + +int64_t +_mcommon_emul_atomic_int64_fetch_add (volatile int64_t *p, int64_t n, enum mcommon_memory_order _unused) +{ + int64_t ret; + + BSON_UNUSED (_unused); + + _lock_emul_atomic (); + ret = *p; + *p += n; + _unlock_emul_atomic (); + return ret; +} + +int64_t +_mcommon_emul_atomic_int64_exchange (volatile int64_t *p, int64_t n, enum mcommon_memory_order _unused) +{ + int64_t ret; + + BSON_UNUSED (_unused); + + _lock_emul_atomic (); + ret = *p; + *p = n; + _unlock_emul_atomic (); + return ret; +} + +int64_t +_mcommon_emul_atomic_int64_compare_exchange_strong (volatile int64_t *p, + int64_t expect_value, + int64_t new_value, + enum mcommon_memory_order _unused) +{ + int64_t ret; + + BSON_UNUSED (_unused); + + _lock_emul_atomic (); + ret = *p; + if (ret == expect_value) { + *p = new_value; + } + _unlock_emul_atomic (); + return ret; +} + +int64_t +_mcommon_emul_atomic_int64_compare_exchange_weak (volatile int64_t *p, + int64_t expect_value, + int64_t new_value, + enum mcommon_memory_order order) +{ + /* We're emulating. We can't do a weak version. */ + return _mcommon_emul_atomic_int64_compare_exchange_strong (p, expect_value, new_value, order); +} + + +int32_t +_mcommon_emul_atomic_int32_fetch_add (volatile int32_t *p, int32_t n, enum mcommon_memory_order _unused) +{ + int32_t ret; + + BSON_UNUSED (_unused); + + _lock_emul_atomic (); + ret = *p; + *p += n; + _unlock_emul_atomic (); + return ret; +} + +int32_t +_mcommon_emul_atomic_int32_exchange (volatile int32_t *p, int32_t n, enum mcommon_memory_order _unused) +{ + int32_t ret; + + BSON_UNUSED (_unused); + + _lock_emul_atomic (); + ret = *p; + *p = n; + _unlock_emul_atomic (); + return ret; +} + +int32_t +_mcommon_emul_atomic_int32_compare_exchange_strong (volatile int32_t *p, + int32_t expect_value, + int32_t new_value, + enum mcommon_memory_order _unused) +{ + int32_t ret; + + BSON_UNUSED (_unused); + + _lock_emul_atomic (); + ret = *p; + if (ret == expect_value) { + *p = new_value; + } + _unlock_emul_atomic (); + return ret; +} + +int32_t +_mcommon_emul_atomic_int32_compare_exchange_weak (volatile int32_t *p, + int32_t expect_value, + int32_t new_value, + enum mcommon_memory_order order) +{ + /* We're emulating. We can't do a weak version. */ + return _mcommon_emul_atomic_int32_compare_exchange_strong (p, expect_value, new_value, order); +} + + +int +_mcommon_emul_atomic_int_fetch_add (volatile int *p, int n, enum mcommon_memory_order _unused) +{ + int ret; + + BSON_UNUSED (_unused); + + _lock_emul_atomic (); + ret = *p; + *p += n; + _unlock_emul_atomic (); + return ret; +} + +int +_mcommon_emul_atomic_int_exchange (volatile int *p, int n, enum mcommon_memory_order _unused) +{ + int ret; + + BSON_UNUSED (_unused); + + _lock_emul_atomic (); + ret = *p; + *p = n; + _unlock_emul_atomic (); + return ret; +} + +int +_mcommon_emul_atomic_int_compare_exchange_strong (volatile int *p, + int expect_value, + int new_value, + enum mcommon_memory_order _unused) +{ + int ret; + + BSON_UNUSED (_unused); + + _lock_emul_atomic (); + ret = *p; + if (ret == expect_value) { + *p = new_value; + } + _unlock_emul_atomic (); + return ret; +} + +int +_mcommon_emul_atomic_int_compare_exchange_weak (volatile int *p, + int expect_value, + int new_value, + enum mcommon_memory_order order) +{ + /* We're emulating. We can't do a weak version. */ + return _mcommon_emul_atomic_int_compare_exchange_strong (p, expect_value, new_value, order); +} + +void * +_mcommon_emul_atomic_ptr_exchange (void *volatile *p, void *n, enum mcommon_memory_order _unused) +{ + void *ret; + + BSON_UNUSED (_unused); + + _lock_emul_atomic (); + ret = *p; + *p = n; + _unlock_emul_atomic (); + return ret; +} diff --git a/src/common/common-cmp-private.h b/src/common/common-cmp-private.h new file mode 100644 index 00000000000..3bb5009678e --- /dev/null +++ b/src/common/common-cmp-private.h @@ -0,0 +1,174 @@ +/* + * Copyright 2009-present MongoDB, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "common-prelude.h" + +#ifndef MONGO_C_DRIVER_COMMON_CMP_PRIVATE_H +#define MONGO_C_DRIVER_COMMON_CMP_PRIVATE_H + + +#include /* ssize_t, BSON_CONCAT */ + +#include +#include +#include + + +BSON_BEGIN_DECLS + + +/* Based on the "Safe Integral Comparisons" proposal merged in C++20: + * http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p0586r2.html + * + * Due to lack of type deduction in C, relational comparison functions (e.g. + * `cmp_less`) are defined in sets of four "functions" according to the + * signedness of each value argument, e.g.: + * - mcommon_cmp_less_ss (signed-value, signed-value) + * - mcommon_cmp_less_uu (unsigned-value, unsigned-value) + * - mcommon_cmp_less_su (signed-value, unsigned-value) + * - mcommon_cmp_less_us (unsigned-value, signed-value) + * + * Similarly, the `in_range` function is defined as a set of two "functions" + * according to the signedness of the value argument: + * - mcommon_in_range_signed (Type, signed-value) + * - mcommon_in_range_unsigned (Type, unsigned-value) + * + * The user must take care to use the correct signedness for the provided + * argument(s). Enabling compiler warnings for implicit sign conversions is + * recommended. + */ + + +#define MCOMMON_CMP_SET(op, ss, uu, su, us) \ + static BSON_INLINE bool BSON_CONCAT3 (mcommon_cmp_, op, _ss) (int64_t t, int64_t u) \ + { \ + return (ss); \ + } \ + \ + static BSON_INLINE bool BSON_CONCAT3 (mcommon_cmp_, op, _uu) (uint64_t t, uint64_t u) \ + { \ + return (uu); \ + } \ + \ + static BSON_INLINE bool BSON_CONCAT3 (mcommon_cmp_, op, _su) (int64_t t, uint64_t u) \ + { \ + return (su); \ + } \ + \ + static BSON_INLINE bool BSON_CONCAT3 (mcommon_cmp_, op, _us) (uint64_t t, int64_t u) \ + { \ + return (us); \ + } + +MCOMMON_CMP_SET (equal, t == u, t == u, t < 0 ? false : (uint64_t) (t) == u, u < 0 ? false : t == (uint64_t) (u)) + +MCOMMON_CMP_SET (not_equal, + !mcommon_cmp_equal_ss (t, u), + !mcommon_cmp_equal_uu (t, u), + !mcommon_cmp_equal_su (t, u), + !mcommon_cmp_equal_us (t, u)) + +MCOMMON_CMP_SET (less, t < u, t < u, t < 0 ? true : (uint64_t) (t) < u, u < 0 ? false : t < (uint64_t) (u)) + +MCOMMON_CMP_SET (greater, + mcommon_cmp_less_ss (u, t), + mcommon_cmp_less_uu (u, t), + mcommon_cmp_less_us (u, t), + mcommon_cmp_less_su (u, t)) + +MCOMMON_CMP_SET (less_equal, + !mcommon_cmp_greater_ss (t, u), + !mcommon_cmp_greater_uu (t, u), + !mcommon_cmp_greater_su (t, u), + !mcommon_cmp_greater_us (t, u)) + +MCOMMON_CMP_SET (greater_equal, + !mcommon_cmp_less_ss (t, u), + !mcommon_cmp_less_uu (t, u), + !mcommon_cmp_less_su (t, u), + !mcommon_cmp_less_us (t, u)) + +#undef MCOMMON_CMP_SET + + +/* Return true if the given value is within the range of the corresponding + * signed type. The suffix must match the signedness of the given value. */ +#define MCOMMON_IN_RANGE_SET_SIGNED(Type, min, max) \ + static BSON_INLINE bool BSON_CONCAT3 (mcommon_in_range, _##Type, _signed) (int64_t value) \ + { \ + return mcommon_cmp_greater_equal_ss (value, min) && mcommon_cmp_less_equal_ss (value, max); \ + } \ + \ + static BSON_INLINE bool BSON_CONCAT3 (mcommon_in_range, _##Type, _unsigned) (uint64_t value) \ + { \ + return mcommon_cmp_greater_equal_us (value, min) && mcommon_cmp_less_equal_us (value, max); \ + } + +/* Return true if the given value is within the range of the corresponding + * unsigned type. The suffix must match the signedness of the given value. */ +#define MCOMMON_IN_RANGE_SET_UNSIGNED(Type, max) \ + static BSON_INLINE bool BSON_CONCAT3 (mcommon_in_range, _##Type, _signed) (int64_t value) \ + { \ + return mcommon_cmp_greater_equal_su (value, 0u) && mcommon_cmp_less_equal_su (value, max); \ + } \ + \ + static BSON_INLINE bool BSON_CONCAT3 (mcommon_in_range, _##Type, _unsigned) (uint64_t value) \ + { \ + return mcommon_cmp_less_equal_uu (value, max); \ + } + +MCOMMON_IN_RANGE_SET_SIGNED (signed_char, SCHAR_MIN, SCHAR_MAX) +MCOMMON_IN_RANGE_SET_SIGNED (short, SHRT_MIN, SHRT_MAX) +MCOMMON_IN_RANGE_SET_SIGNED (int, INT_MIN, INT_MAX) +MCOMMON_IN_RANGE_SET_SIGNED (long, LONG_MIN, LONG_MAX) +MCOMMON_IN_RANGE_SET_SIGNED (long_long, LLONG_MIN, LLONG_MAX) + +MCOMMON_IN_RANGE_SET_UNSIGNED (unsigned_char, UCHAR_MAX) +MCOMMON_IN_RANGE_SET_UNSIGNED (unsigned_short, USHRT_MAX) +MCOMMON_IN_RANGE_SET_UNSIGNED (unsigned_int, UINT_MAX) +MCOMMON_IN_RANGE_SET_UNSIGNED (unsigned_long, ULONG_MAX) +MCOMMON_IN_RANGE_SET_UNSIGNED (unsigned_long_long, ULLONG_MAX) + +MCOMMON_IN_RANGE_SET_SIGNED (int8_t, INT8_MIN, INT8_MAX) +MCOMMON_IN_RANGE_SET_SIGNED (int16_t, INT16_MIN, INT16_MAX) +MCOMMON_IN_RANGE_SET_SIGNED (int32_t, INT32_MIN, INT32_MAX) +MCOMMON_IN_RANGE_SET_SIGNED (int64_t, INT64_MIN, INT64_MAX) + +MCOMMON_IN_RANGE_SET_UNSIGNED (uint8_t, UINT8_MAX) +MCOMMON_IN_RANGE_SET_UNSIGNED (uint16_t, UINT16_MAX) +MCOMMON_IN_RANGE_SET_UNSIGNED (uint32_t, UINT32_MAX) +MCOMMON_IN_RANGE_SET_UNSIGNED (uint64_t, UINT64_MAX) + +MCOMMON_IN_RANGE_SET_SIGNED (ssize_t, SSIZE_MIN, SSIZE_MAX) +MCOMMON_IN_RANGE_SET_UNSIGNED (size_t, SIZE_MAX) + +#undef MCOMMON_IN_RANGE_SET_SIGNED +#undef MCOMMON_IN_RANGE_SET_UNSIGNED + + +/* Return true if the value with *signed* type is in the representable range of + * Type and false otherwise. */ +#define mcommon_in_range_signed(Type, value) BSON_CONCAT3 (mcommon_in_range, _##Type, _signed) (value) + +/* Return true if the value with *unsigned* type is in the representable range + * of Type and false otherwise. */ +#define mcommon_in_range_unsigned(Type, value) BSON_CONCAT3 (mcommon_in_range, _##Type, _unsigned) (value) + + +BSON_END_DECLS + + +#endif /* MONGO_C_DRIVER_COMMON_CMP_PRIVATE_H */ diff --git a/src/common/common-macros-private.h b/src/common/common-macros-private.h index ca63554ea22..1bd6a236dd5 100644 --- a/src/common/common-macros-private.h +++ b/src/common/common-macros-private.h @@ -28,7 +28,7 @@ #endif // `MC_ENABLE_CONVERSION_WARNING_BEGIN` enables -Wconversion to check for potentially unsafe integer conversions. -// The `bson_in_range_*` functions can help address these warnings by ensuring a cast is within bounds. +// The `mcommon_in_range_*` functions can help address these warnings by ensuring a cast is within bounds. #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) // gcc 4.6 added support for "diagnostic push". #define MC_ENABLE_CONVERSION_WARNING_BEGIN \ _Pragma ("GCC diagnostic push") _Pragma ("GCC diagnostic warning \"-Wconversion\"") diff --git a/src/libbson/NEWS b/src/libbson/NEWS index 4776f9133cc..c0edcf21ff1 100644 --- a/src/libbson/NEWS +++ b/src/libbson/NEWS @@ -4,6 +4,9 @@ libbson 1.29.0 (Unreleased) Deprecated: * `bson_string_t` and associated functions are deprecated and planned for removal in a future major release. + * Compiling with `BSON_MEMCHECK` defined is deprecated. + * `bson_in_range_*` and `bson_cmp_*` functions. + * `bson_atomic_*` and `bson_thrd_yield` functions. libbson 1.28.1 ============== diff --git a/src/libbson/examples/bson-check-depth.c b/src/libbson/examples/bson-check-depth.c index 7bf7775109b..09cd90b7bbc 100644 --- a/src/libbson/examples/bson-check-depth.c +++ b/src/libbson/examples/bson-check-depth.c @@ -114,7 +114,7 @@ main (int argc, char **argv) return 1; } - BSON_ASSERT (bson_in_range_signed (uint32_t, max_depth)); + BSON_ASSERT (max_depth >= 0 && (uint64_t) max_depth <= UINT32_MAX); while ((bson = bson_reader_read (bson_reader, &reached_eof))) { check_depth (bson, (uint32_t) max_depth); diff --git a/src/libbson/fuzz/fuzz_test_bson_utf8_escape_for_json.c b/src/libbson/fuzz/fuzz_test_bson_utf8_escape_for_json.c index a798bd0a276..6cd1b2f43ea 100644 --- a/src/libbson/fuzz/fuzz_test_bson_utf8_escape_for_json.c +++ b/src/libbson/fuzz/fuzz_test_bson_utf8_escape_for_json.c @@ -4,7 +4,7 @@ int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) { // Reject inputs with lengths greater than ssize_t - if (bson_cmp_greater_us (size, SSIZE_MAX)) { + if (size > (size_t) SSIZE_MAX) { return -1; } diff --git a/src/libbson/src/bson/bson-atomic.c b/src/libbson/src/bson/bson-atomic.c index 64caa79bd45..ae25a5b4c87 100644 --- a/src/libbson/src/bson/bson-atomic.c +++ b/src/libbson/src/bson/bson-atomic.c @@ -16,6 +16,9 @@ #include +#include + +BEGIN_IGNORE_DEPRECATIONS #ifdef BSON_OS_UNIX /* For sched_yield() */ @@ -267,3 +270,5 @@ _bson_emul_atomic_ptr_exchange (void *volatile *p, void *n, enum bson_memory_ord _unlock_emul_atomic (); return ret; } + +END_IGNORE_DEPRECATIONS diff --git a/src/libbson/src/bson/bson-atomic.h b/src/libbson/src/bson/bson-atomic.h index 8195d8777f8..1b61b5c555b 100644 --- a/src/libbson/src/bson/bson-atomic.h +++ b/src/libbson/src/bson/bson-atomic.h @@ -29,10 +29,27 @@ #include #endif +// bson-atomic.h is deprecated. +// Ignore deprecation warnings for function calls within this file. + +#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) +#define BSON_BEGIN_IGNORE_DEPRECATIONS \ + _Pragma ("GCC diagnostic push") _Pragma ("GCC diagnostic ignored \"-Wdeprecated-declarations\"") +#define BSON_END_IGNORE_DEPRECATIONS _Pragma ("GCC diagnostic pop") +#elif defined(__clang__) +#define BSON_BEGIN_IGNORE_DEPRECATIONS \ + _Pragma ("clang diagnostic push") _Pragma ("clang diagnostic ignored \"-Wdeprecated-declarations\"") +#define BSON_END_IGNORE_DEPRECATIONS _Pragma ("clang diagnostic pop") +#else +#define BSON_BEGIN_IGNORE_DEPRECATIONS +#define BSON_END_IGNORE_DEPRECATIONS +#endif + +BSON_BEGIN_IGNORE_DEPRECATIONS BSON_BEGIN_DECLS -enum bson_memory_order { +enum BSON_GNUC_DEPRECATED bson_memory_order { bson_memory_order_seq_cst, bson_memory_order_acquire, bson_memory_order_release, @@ -146,7 +163,7 @@ enum bson_memory_order { #define DECL_ATOMIC_INTEGRAL(NamePart, Type, VCIntrinSuffix) \ - static BSON_INLINE Type bson_atomic_##NamePart##_fetch_add ( \ + static BSON_INLINE Type BSON_GNUC_DEPRECATED bson_atomic_##NamePart##_fetch_add ( \ Type volatile *a, Type addend, enum bson_memory_order ord) \ { \ DEF_ATOMIC_OP (BSON_CONCAT (_InterlockedExchangeAdd, VCIntrinSuffix), \ @@ -157,7 +174,7 @@ enum bson_memory_order { addend); \ } \ \ - static BSON_INLINE Type bson_atomic_##NamePart##_fetch_sub ( \ + static BSON_INLINE Type BSON_GNUC_DEPRECATED bson_atomic_##NamePart##_fetch_sub ( \ Type volatile *a, Type subtrahend, enum bson_memory_order ord) \ { \ /* MSVC doesn't have a subtract intrinsic, so just reuse addition */ \ @@ -166,7 +183,8 @@ enum bson_memory_order { BSON_IF_GNU_LEGACY_ATOMICS (DEF_ATOMIC_OP (~, ~, __sync_fetch_and_sub, ord, a, subtrahend);) \ } \ \ - static BSON_INLINE Type bson_atomic_##NamePart##_fetch (Type volatile const *a, enum bson_memory_order order) \ + static BSON_INLINE Type BSON_GNUC_DEPRECATED bson_atomic_##NamePart##_fetch (Type volatile const *a, \ + enum bson_memory_order order) \ { \ /* MSVC doesn't have a load intrinsic, so just add zero */ \ BSON_IF_MSVC (return bson_atomic_##NamePart##_fetch_add ((Type volatile *) a, 0, order);) \ @@ -193,7 +211,7 @@ enum bson_memory_order { }) \ } \ \ - static BSON_INLINE Type bson_atomic_##NamePart##_exchange ( \ + static BSON_INLINE Type BSON_GNUC_DEPRECATED bson_atomic_##NamePart##_exchange ( \ Type volatile *a, Type value, enum bson_memory_order ord) \ { \ BSON_IF_MSVC (DEF_ATOMIC_OP (BSON_CONCAT (_InterlockedExchange, VCIntrinSuffix), ~, ~, ord, a, value);) \ @@ -217,7 +235,7 @@ enum bson_memory_order { BSON_IF_GNU_LEGACY_ATOMICS (BSON_UNUSED (ord); return __sync_val_compare_and_swap (a, *a, value);) \ } \ \ - static BSON_INLINE Type bson_atomic_##NamePart##_compare_exchange_strong ( \ + static BSON_INLINE Type BSON_GNUC_DEPRECATED bson_atomic_##NamePart##_compare_exchange_strong ( \ Type volatile *a, Type expect, Type new_value, enum bson_memory_order ord) \ { \ Type actual = expect; \ @@ -245,7 +263,7 @@ enum bson_memory_order { return actual; \ } \ \ - static BSON_INLINE Type bson_atomic_##NamePart##_compare_exchange_weak ( \ + static BSON_INLINE Type BSON_GNUC_DEPRECATED bson_atomic_##NamePart##_compare_exchange_weak ( \ Type volatile *a, Type expect, Type new_value, enum bson_memory_order ord) \ { \ Type actual = expect; \ @@ -372,31 +390,31 @@ DECL_ATOMIC_INTEGRAL (int64, __int64, 64) DECL_ATOMIC_STDINT (int64, 64) #endif #else -static BSON_INLINE int64_t +static BSON_INLINE int64_t BSON_GNUC_DEPRECATED bson_atomic_int64_fetch (const int64_t volatile *val, enum bson_memory_order order) { return _bson_emul_atomic_int64_fetch_add ((int64_t volatile *) val, 0, order); } -static BSON_INLINE int64_t +static BSON_INLINE int64_t BSON_GNUC_DEPRECATED bson_atomic_int64_fetch_add (int64_t volatile *val, int64_t v, enum bson_memory_order order) { return _bson_emul_atomic_int64_fetch_add (val, v, order); } -static BSON_INLINE int64_t +static BSON_INLINE int64_t BSON_GNUC_DEPRECATED bson_atomic_int64_fetch_sub (int64_t volatile *val, int64_t v, enum bson_memory_order order) { return _bson_emul_atomic_int64_fetch_add (val, -v, order); } -static BSON_INLINE int64_t +static BSON_INLINE int64_t BSON_GNUC_DEPRECATED bson_atomic_int64_exchange (int64_t volatile *val, int64_t v, enum bson_memory_order order) { return _bson_emul_atomic_int64_exchange (val, v, order); } -static BSON_INLINE int64_t +static BSON_INLINE int64_t BSON_GNUC_DEPRECATED bson_atomic_int64_compare_exchange_strong (int64_t volatile *val, int64_t expect_value, int64_t new_value, @@ -405,7 +423,7 @@ bson_atomic_int64_compare_exchange_strong (int64_t volatile *val, return _bson_emul_atomic_int64_compare_exchange_strong (val, expect_value, new_value, order); } -static BSON_INLINE int64_t +static BSON_INLINE int64_t BSON_GNUC_DEPRECATED bson_atomic_int64_compare_exchange_weak (int64_t volatile *val, int64_t expect_value, int64_t new_value, @@ -416,31 +434,31 @@ bson_atomic_int64_compare_exchange_weak (int64_t volatile *val, #endif #if defined(BSON_EMULATE_INT32) -static BSON_INLINE int32_t +static BSON_INLINE int32_t BSON_GNUC_DEPRECATED bson_atomic_int32_fetch (const int32_t volatile *val, enum bson_memory_order order) { return _bson_emul_atomic_int32_fetch_add ((int32_t volatile *) val, 0, order); } -static BSON_INLINE int32_t +static BSON_INLINE int32_t BSON_GNUC_DEPRECATED bson_atomic_int32_fetch_add (int32_t volatile *val, int32_t v, enum bson_memory_order order) { return _bson_emul_atomic_int32_fetch_add (val, v, order); } -static BSON_INLINE int32_t +static BSON_INLINE int32_t BSON_GNUC_DEPRECATED bson_atomic_int32_fetch_sub (int32_t volatile *val, int32_t v, enum bson_memory_order order) { return _bson_emul_atomic_int32_fetch_add (val, -v, order); } -static BSON_INLINE int32_t +static BSON_INLINE int32_t BSON_GNUC_DEPRECATED bson_atomic_int32_exchange (int32_t volatile *val, int32_t v, enum bson_memory_order order) { return _bson_emul_atomic_int32_exchange (val, v, order); } -static BSON_INLINE int32_t +static BSON_INLINE int32_t BSON_GNUC_DEPRECATED bson_atomic_int32_compare_exchange_strong (int32_t volatile *val, int32_t expect_value, int32_t new_value, @@ -449,7 +467,7 @@ bson_atomic_int32_compare_exchange_strong (int32_t volatile *val, return _bson_emul_atomic_int32_compare_exchange_strong (val, expect_value, new_value, order); } -static BSON_INLINE int32_t +static BSON_INLINE int32_t BSON_GNUC_DEPRECATED bson_atomic_int32_compare_exchange_weak (int32_t volatile *val, int32_t expect_value, int32_t new_value, @@ -460,31 +478,31 @@ bson_atomic_int32_compare_exchange_weak (int32_t volatile *val, #endif /* BSON_EMULATE_INT32 */ #if defined(BSON_EMULATE_INT) -static BSON_INLINE int +static BSON_INLINE int BSON_GNUC_DEPRECATED bson_atomic_int_fetch (const int volatile *val, enum bson_memory_order order) { return _bson_emul_atomic_int_fetch_add ((int volatile *) val, 0, order); } -static BSON_INLINE int +static BSON_INLINE int BSON_GNUC_DEPRECATED bson_atomic_int_fetch_add (int volatile *val, int v, enum bson_memory_order order) { return _bson_emul_atomic_int_fetch_add (val, v, order); } -static BSON_INLINE int +static BSON_INLINE int BSON_GNUC_DEPRECATED bson_atomic_int_fetch_sub (int volatile *val, int v, enum bson_memory_order order) { return _bson_emul_atomic_int_fetch_add (val, -v, order); } -static BSON_INLINE int +static BSON_INLINE int BSON_GNUC_DEPRECATED bson_atomic_int_exchange (int volatile *val, int v, enum bson_memory_order order) { return _bson_emul_atomic_int_exchange (val, v, order); } -static BSON_INLINE int +static BSON_INLINE int BSON_GNUC_DEPRECATED bson_atomic_int_compare_exchange_strong (int volatile *val, int expect_value, int new_value, @@ -493,14 +511,14 @@ bson_atomic_int_compare_exchange_strong (int volatile *val, return _bson_emul_atomic_int_compare_exchange_strong (val, expect_value, new_value, order); } -static BSON_INLINE int +static BSON_INLINE int BSON_GNUC_DEPRECATED bson_atomic_int_compare_exchange_weak (int volatile *val, int expect_value, int new_value, enum bson_memory_order order) { return _bson_emul_atomic_int_compare_exchange_weak (val, expect_value, new_value, order); } #endif /* BSON_EMULATE_INT */ -static BSON_INLINE void * +static BSON_INLINE void *BSON_GNUC_DEPRECATED bson_atomic_ptr_exchange (void *volatile *ptr, void *new_value, enum bson_memory_order ord) { #if defined(BSON_EMULATE_PTR) @@ -513,7 +531,7 @@ bson_atomic_ptr_exchange (void *volatile *ptr, void *new_value, enum bson_memory #endif } -static BSON_INLINE void * +static BSON_INLINE void *BSON_GNUC_DEPRECATED bson_atomic_ptr_compare_exchange_strong (void *volatile *ptr, void *expect, void *new_value, enum bson_memory_order ord) { switch (ord) { @@ -537,7 +555,7 @@ bson_atomic_ptr_compare_exchange_strong (void *volatile *ptr, void *expect, void } -static BSON_INLINE void * +static BSON_INLINE void *BSON_GNUC_DEPRECATED bson_atomic_ptr_compare_exchange_weak (void *volatile *ptr, void *expect, void *new_value, enum bson_memory_order ord) { switch (ord) { @@ -561,7 +579,7 @@ bson_atomic_ptr_compare_exchange_weak (void *volatile *ptr, void *expect, void * } -static BSON_INLINE void * +static BSON_INLINE void *BSON_GNUC_DEPRECATED bson_atomic_ptr_fetch (void *volatile const *ptr, enum bson_memory_order ord) { return bson_atomic_ptr_compare_exchange_strong ((void *volatile *) ptr, NULL, NULL, ord); @@ -577,7 +595,7 @@ bson_atomic_ptr_fetch (void *volatile const *ptr, enum bson_memory_order ord) /** * @brief Generate a full-fence memory barrier at the call site. */ -static BSON_INLINE void +static BSON_INLINE void BSON_GNUC_DEPRECATED bson_atomic_thread_fence (void) { BSON_IF_MSVC (MemoryBarrier ();) @@ -592,13 +610,13 @@ bson_atomic_thread_fence (void) #undef BSON_IF_GNU_LEGACY_ATOMICS #undef BSON_USE_LEGACY_GCC_ATOMICS -BSON_GNUC_DEPRECATED_FOR ("bson_atomic_thread_fence") +BSON_GNUC_DEPRECATED BSON_EXPORT (void) bson_memory_barrier (void); -BSON_GNUC_DEPRECATED_FOR ("bson_atomic_int_fetch_add") +BSON_GNUC_DEPRECATED BSON_EXPORT (int32_t) bson_atomic_int_add (volatile int32_t *p, int32_t n); -BSON_GNUC_DEPRECATED_FOR ("bson_atomic_int64_fetch_add") +BSON_GNUC_DEPRECATED BSON_EXPORT (int64_t) bson_atomic_int64_add (volatile int64_t *p, int64_t n); @@ -608,5 +626,9 @@ BSON_EXPORT (int64_t) bson_atomic_int64_add (volatile int64_t *p, int64_t n); BSON_END_DECLS +BSON_END_IGNORE_DEPRECATIONS + +#undef BSON_BEGIN_IGNORE_DEPRECATIONS +#undef BSON_END_IGNORE_DEPRECATIONS #endif /* BSON_ATOMIC_H */ diff --git a/src/libbson/src/bson/bson-cmp.h b/src/libbson/src/bson/bson-cmp.h index f5785f52173..3932164a992 100644 --- a/src/libbson/src/bson/bson-cmp.h +++ b/src/libbson/src/bson/bson-cmp.h @@ -28,6 +28,23 @@ #include #include +// bson-cmp.h is deprecated. +// Ignore deprecation warnings for function calls within this file. + +#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) +#define BSON_BEGIN_IGNORE_DEPRECATIONS \ + _Pragma ("GCC diagnostic push") _Pragma ("GCC diagnostic ignored \"-Wdeprecated-declarations\"") +#define BSON_END_IGNORE_DEPRECATIONS _Pragma ("GCC diagnostic pop") +#elif defined(__clang__) +#define BSON_BEGIN_IGNORE_DEPRECATIONS \ + _Pragma ("clang diagnostic push") _Pragma ("clang diagnostic ignored \"-Wdeprecated-declarations\"") +#define BSON_END_IGNORE_DEPRECATIONS _Pragma ("clang diagnostic pop") +#else +#define BSON_BEGIN_IGNORE_DEPRECATIONS +#define BSON_END_IGNORE_DEPRECATIONS +#endif + +BSON_BEGIN_IGNORE_DEPRECATIONS BSON_BEGIN_DECLS @@ -53,26 +70,25 @@ BSON_BEGIN_DECLS * recommended. */ - -#define BSON_CMP_SET(op, ss, uu, su, us) \ - static BSON_INLINE bool BSON_CONCAT3 (bson_cmp_, op, _ss) (int64_t t, int64_t u) \ - { \ - return (ss); \ - } \ - \ - static BSON_INLINE bool BSON_CONCAT3 (bson_cmp_, op, _uu) (uint64_t t, uint64_t u) \ - { \ - return (uu); \ - } \ - \ - static BSON_INLINE bool BSON_CONCAT3 (bson_cmp_, op, _su) (int64_t t, uint64_t u) \ - { \ - return (su); \ - } \ - \ - static BSON_INLINE bool BSON_CONCAT3 (bson_cmp_, op, _us) (uint64_t t, int64_t u) \ - { \ - return (us); \ +#define BSON_CMP_SET(op, ss, uu, su, us) \ + static BSON_INLINE bool BSON_GNUC_DEPRECATED BSON_CONCAT3 (bson_cmp_, op, _ss) (int64_t t, int64_t u) \ + { \ + return (ss); \ + } \ + \ + static BSON_INLINE bool BSON_GNUC_DEPRECATED BSON_CONCAT3 (bson_cmp_, op, _uu) (uint64_t t, uint64_t u) \ + { \ + return (uu); \ + } \ + \ + static BSON_INLINE bool BSON_GNUC_DEPRECATED BSON_CONCAT3 (bson_cmp_, op, _su) (int64_t t, uint64_t u) \ + { \ + return (su); \ + } \ + \ + static BSON_INLINE bool BSON_GNUC_DEPRECATED BSON_CONCAT3 (bson_cmp_, op, _us) (uint64_t t, int64_t u) \ + { \ + return (us); \ } BSON_CMP_SET (equal, t == u, t == u, t < 0 ? false : (uint64_t) (t) == u, u < 0 ? false : t == (uint64_t) (u)) @@ -105,28 +121,28 @@ BSON_CMP_SET (greater_equal, /* Return true if the given value is within the range of the corresponding * signed type. The suffix must match the signedness of the given value. */ -#define BSON_IN_RANGE_SET_SIGNED(Type, min, max) \ - static BSON_INLINE bool BSON_CONCAT3 (bson_in_range, _##Type, _signed) (int64_t value) \ - { \ - return bson_cmp_greater_equal_ss (value, min) && bson_cmp_less_equal_ss (value, max); \ - } \ - \ - static BSON_INLINE bool BSON_CONCAT3 (bson_in_range, _##Type, _unsigned) (uint64_t value) \ - { \ - return bson_cmp_greater_equal_us (value, min) && bson_cmp_less_equal_us (value, max); \ +#define BSON_IN_RANGE_SET_SIGNED(Type, min, max) \ + static BSON_INLINE bool BSON_GNUC_DEPRECATED BSON_CONCAT3 (bson_in_range, _##Type, _signed) (int64_t value) \ + { \ + return bson_cmp_greater_equal_ss (value, min) && bson_cmp_less_equal_ss (value, max); \ + } \ + \ + static BSON_INLINE bool BSON_GNUC_DEPRECATED BSON_CONCAT3 (bson_in_range, _##Type, _unsigned) (uint64_t value) \ + { \ + return bson_cmp_greater_equal_us (value, min) && bson_cmp_less_equal_us (value, max); \ } /* Return true if the given value is within the range of the corresponding * unsigned type. The suffix must match the signedness of the given value. */ -#define BSON_IN_RANGE_SET_UNSIGNED(Type, max) \ - static BSON_INLINE bool BSON_CONCAT3 (bson_in_range, _##Type, _signed) (int64_t value) \ - { \ - return bson_cmp_greater_equal_su (value, 0u) && bson_cmp_less_equal_su (value, max); \ - } \ - \ - static BSON_INLINE bool BSON_CONCAT3 (bson_in_range, _##Type, _unsigned) (uint64_t value) \ - { \ - return bson_cmp_less_equal_uu (value, max); \ +#define BSON_IN_RANGE_SET_UNSIGNED(Type, max) \ + static BSON_INLINE bool BSON_GNUC_DEPRECATED BSON_CONCAT3 (bson_in_range, _##Type, _signed) (int64_t value) \ + { \ + return bson_cmp_greater_equal_su (value, 0u) && bson_cmp_less_equal_su (value, max); \ + } \ + \ + static BSON_INLINE bool BSON_GNUC_DEPRECATED BSON_CONCAT3 (bson_in_range, _##Type, _unsigned) (uint64_t value) \ + { \ + return bson_cmp_less_equal_uu (value, max); \ } BSON_IN_RANGE_SET_SIGNED (signed_char, SCHAR_MIN, SCHAR_MAX) @@ -169,5 +185,9 @@ BSON_IN_RANGE_SET_UNSIGNED (size_t, SIZE_MAX) BSON_END_DECLS +BSON_END_IGNORE_DEPRECATIONS + +#undef BSON_BEGIN_IGNORE_DEPRECATIONS +#undef BSON_END_IGNORE_DEPRECATIONS #endif /* BSON_CMP_H */ diff --git a/src/libbson/src/bson/bson-context.c b/src/libbson/src/bson/bson-context.c index 6b8eb67ec05..11fd8967f49 100644 --- a/src/libbson/src/bson/bson-context.c +++ b/src/libbson/src/bson/bson-context.c @@ -22,7 +22,7 @@ #include #include -#include +#include #include #include #include @@ -61,8 +61,8 @@ void _bson_context_set_oid_seq32 (bson_context_t *context, /* IN */ bson_oid_t *oid) /* OUT */ { - uint32_t seq = (uint32_t) bson_atomic_int32_fetch_add ( - (DECL_ATOMIC_INTEGRAL_INT32 *) &context->seq32, 1, bson_memory_order_seq_cst); + uint32_t seq = (uint32_t) mcommon_atomic_int32_fetch_add ( + (DECL_ATOMIC_INTEGRAL_INT32 *) &context->seq32, 1, mcommon_memory_order_seq_cst); seq = BSON_UINT32_TO_BE (seq); memcpy (&oid->bytes[BSON_OID_SEQ32_OFFSET], ((uint8_t *) &seq) + 1, BSON_OID_SEQ32_SIZE); } @@ -72,7 +72,8 @@ void _bson_context_set_oid_seq64 (bson_context_t *context, /* IN */ bson_oid_t *oid) /* OUT */ { - uint64_t seq = (uint64_t) bson_atomic_int64_fetch_add ((int64_t *) &context->seq64, 1, bson_memory_order_seq_cst); + uint64_t seq = + (uint64_t) mcommon_atomic_int64_fetch_add ((int64_t *) &context->seq64, 1, mcommon_memory_order_seq_cst); seq = BSON_UINT64_TO_BE (seq); memcpy (&oid->bytes[BSON_OID_SEQ64_OFFSET], &seq, BSON_OID_SEQ64_SIZE); @@ -278,7 +279,8 @@ _bson_context_init_random (bson_context_t *context, bool init_seq) bson_gettimeofday (&rand_params.time); rand_params.pid = _bson_getpid (); _bson_context_get_hostname (rand_params.hostname); - rand_params.rand_call_counter = bson_atomic_int64_fetch_add (&s_rand_call_counter, 1, bson_memory_order_seq_cst); + rand_params.rand_call_counter = + mcommon_atomic_int64_fetch_add (&s_rand_call_counter, 1, mcommon_memory_order_seq_cst); /* Generate a SipHash key. We do not care about secrecy or determinism, only * uniqueness. */ diff --git a/src/libbson/src/bson/bson-decimal128.c b/src/libbson/src/bson/bson-decimal128.c index 2905ccc87a8..35a725040ac 100644 --- a/src/libbson/src/bson/bson-decimal128.c +++ b/src/libbson/src/bson/bson-decimal128.c @@ -19,11 +19,11 @@ #include #include -#include #include #include #include #include +#include #define BSON_DECIMAL128_EXPONENT_MAX 6111 @@ -298,7 +298,7 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */ *(str_out++) = '0'; } - for (uint32_t i = 0; bson_cmp_greater_us (significand_digits - i, BSON_MAX (radix_position - 1, 0)) && + for (uint32_t i = 0; mcommon_cmp_greater_us (significand_digits - i, BSON_MAX (radix_position - 1, 0)) && (str_out - str) < available_bytes; i++) { *(str_out++) = *(significand_read++) + '0'; @@ -582,7 +582,7 @@ bson_decimal128_from_string_w_len (const char *string, /* IN */ int read_exponent = SSCANF (++str_read, "%" SCNd64 "%n", &temp_exponent, &nread); str_read += nread; - if (!read_exponent || nread == 0 || !bson_in_range_int32_t_signed (temp_exponent)) { + if (!read_exponent || nread == 0 || !mcommon_in_range_int32_t_signed (temp_exponent)) { BSON_DECIMAL128_SET_NAN (*dec); return false; } @@ -623,11 +623,11 @@ bson_decimal128_from_string_w_len (const char *string, /* IN */ /* to represent user input */ /* Overflow prevention */ - if (bson_cmp_less_equal_su (exponent, radix_position) && - bson_cmp_greater_us (radix_position, exponent + (1 << 14))) { + if (mcommon_cmp_less_equal_su (exponent, radix_position) && + mcommon_cmp_greater_us (radix_position, exponent + (1 << 14))) { exponent = BSON_DECIMAL128_EXPONENT_MIN; } else { - BSON_ASSERT (bson_in_range_unsigned (int32_t, radix_position)); + BSON_ASSERT (mcommon_in_range_unsigned (int32_t, radix_position)); exponent -= (int32_t) radix_position; } diff --git a/src/libbson/src/bson/bson-iter.c b/src/libbson/src/bson/bson-iter.c index 5159a39b518..7688e96f0e8 100644 --- a/src/libbson/src/bson/bson-iter.c +++ b/src/libbson/src/bson/bson-iter.c @@ -19,6 +19,7 @@ #include #include #include +#include #define ITER_TYPE(i) ((bson_type_t) * ((i)->raw + (i)->type)) @@ -111,7 +112,7 @@ bson_iter_init_from_data (bson_iter_t *iter, /* OUT */ return false; } - if (BSON_UNLIKELY (!bson_in_range_unsigned (uint32_t, length))) { + if (BSON_UNLIKELY (!mcommon_in_range_unsigned (uint32_t, length))) { memset (iter, 0, sizeof *iter); return false; } diff --git a/src/libbson/src/bson/bson-json.c b/src/libbson/src/bson/bson-json.c index 5b53dd86a32..15903c1adc4 100644 --- a/src/libbson/src/bson/bson-json.c +++ b/src/libbson/src/bson/bson-json.c @@ -28,6 +28,7 @@ #include "common-b64-private.h" #include "jsonsl/jsonsl.h" +#include #ifdef _WIN32 #include @@ -1109,7 +1110,7 @@ _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)); + BSON_ASSERT (mcommon_in_range_unsigned (int, len)); STACK_PUSH_DOC (bson_append_document_begin (STACK_BSON_PARENT, key, (int) len, STACK_BSON_CHILD)); _bson_json_save_map_key (bson, (const uint8_t *) "$type", 5); break; @@ -2070,8 +2071,8 @@ bson_json_reader_read (bson_json_reader_t *reader, /* IN */ /* accumulate a key or string value */ if (reader->json_text_pos != -1) { - if (bson_cmp_less_su (reader->json_text_pos, reader->json->pos)) { - BSON_ASSERT (bson_in_range_unsigned (ssize_t, reader->json->pos)); + if (mcommon_cmp_less_su (reader->json_text_pos, reader->json->pos)) { + BSON_ASSERT (mcommon_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. */ diff --git a/src/libbson/src/bson/bson-keys.c b/src/libbson/src/bson/bson-keys.c index 8f409ebcf16..53acb6c0580 100644 --- a/src/libbson/src/bson/bson-keys.c +++ b/src/libbson/src/bson/bson-keys.c @@ -19,7 +19,7 @@ #include #include -#include +#include static const char *gUint32Strs[] = { @@ -145,6 +145,6 @@ bson_uint32_to_string (uint32_t value, /* IN */ int ret = bson_snprintf (str, size, "%u", value); // Truncation is OK. BSON_ASSERT (ret > 0); - BSON_ASSERT (bson_in_range_size_t_signed (ret)); + BSON_ASSERT (mcommon_in_range_size_t_signed (ret)); return (size_t) ret; } diff --git a/src/libbson/src/bson/bson-memory.c b/src/libbson/src/bson/bson-memory.c index fbf7fbd2e60..e3ab9325349 100644 --- a/src/libbson/src/bson/bson-memory.c +++ b/src/libbson/src/bson/bson-memory.c @@ -20,7 +20,6 @@ #include #include -#include #include #include diff --git a/src/libbson/src/bson/bson-string.c b/src/libbson/src/bson/bson-string.c index 58a251dee63..8d7398f9136 100644 --- a/src/libbson/src/bson/bson-string.c +++ b/src/libbson/src/bson/bson-string.c @@ -20,7 +20,7 @@ #include #include -#include +#include #include #include #include @@ -108,7 +108,7 @@ bson_string_new (const char *str) /* IN */ ret = bson_malloc0 (sizeof *ret); const size_t len_sz = str == NULL ? 0u : strlen (str); - BSON_ASSERT (bson_in_range_unsigned (uint32_t, len_sz)); + BSON_ASSERT (mcommon_in_range_unsigned (uint32_t, len_sz)); const uint32_t len_u32 = (uint32_t) len_sz; bson_string_ensure_space (ret, len_u32); if (str) { @@ -206,7 +206,7 @@ _bson_string_append_ex (bson_string_t *string, /* IN */ BSON_ASSERT (string); BSON_ASSERT (str); - BSON_ASSERT (bson_in_range_unsigned (uint32_t, len)); + BSON_ASSERT (mcommon_in_range_unsigned (uint32_t, len)); const uint32_t len_u32 = (uint32_t) len; BSON_ASSERT (len_u32 <= UINT32_MAX - string->len); const uint32_t new_len = len_u32 + string->len; diff --git a/src/libbson/src/bson/bson-value.c b/src/libbson/src/bson/bson-value.c index 21060e53161..0aef4321af4 100644 --- a/src/libbson/src/bson/bson-value.c +++ b/src/libbson/src/bson/bson-value.c @@ -19,7 +19,7 @@ #include #include #include -#include +#include void @@ -36,7 +36,7 @@ bson_value_copy (const bson_value_t *src, /* IN */ dst->value.v_double = src->value.v_double; break; case BSON_TYPE_UTF8: - BSON_ASSERT (bson_in_range_size_t_unsigned (src->value.v_utf8.len)); + BSON_ASSERT (mcommon_in_range_size_t_unsigned (src->value.v_utf8.len)); size_t utf8_len_sz = (size_t) src->value.v_utf8.len; BSON_ASSERT (utf8_len_sz <= SIZE_MAX - 1); dst->value.v_utf8.len = src->value.v_utf8.len; @@ -72,7 +72,7 @@ bson_value_copy (const bson_value_t *src, /* IN */ dst->value.v_regex.options = bson_strdup (src->value.v_regex.options); break; case BSON_TYPE_DBPOINTER: - BSON_ASSERT (bson_in_range_size_t_unsigned (src->value.v_dbpointer.collection_len)); + BSON_ASSERT (mcommon_in_range_size_t_unsigned (src->value.v_dbpointer.collection_len)); size_t dbpointer_len_sz = (size_t) src->value.v_dbpointer.collection_len; BSON_ASSERT (dbpointer_len_sz <= SIZE_MAX - 1); dst->value.v_dbpointer.collection_len = src->value.v_dbpointer.collection_len; @@ -83,7 +83,7 @@ bson_value_copy (const bson_value_t *src, /* IN */ bson_oid_copy (&src->value.v_dbpointer.oid, &dst->value.v_dbpointer.oid); break; case BSON_TYPE_CODE: - BSON_ASSERT (bson_in_range_size_t_unsigned (src->value.v_code.code_len)); + BSON_ASSERT (mcommon_in_range_size_t_unsigned (src->value.v_code.code_len)); size_t code_len_sz = (size_t) src->value.v_code.code_len; BSON_ASSERT (code_len_sz <= SIZE_MAX - 1); dst->value.v_code.code_len = src->value.v_code.code_len; @@ -92,7 +92,7 @@ bson_value_copy (const bson_value_t *src, /* IN */ dst->value.v_code.code[dst->value.v_code.code_len] = '\0'; break; case BSON_TYPE_SYMBOL: - BSON_ASSERT (bson_in_range_size_t_unsigned (src->value.v_symbol.len)); + BSON_ASSERT (mcommon_in_range_size_t_unsigned (src->value.v_symbol.len)); size_t symbol_len_sz = (size_t) src->value.v_symbol.len; BSON_ASSERT (symbol_len_sz <= SIZE_MAX - 1); dst->value.v_symbol.len = src->value.v_symbol.len; @@ -101,7 +101,7 @@ bson_value_copy (const bson_value_t *src, /* IN */ dst->value.v_symbol.symbol[dst->value.v_symbol.len] = '\0'; break; case BSON_TYPE_CODEWSCOPE: - BSON_ASSERT (bson_in_range_size_t_unsigned (src->value.v_codewscope.code_len)); + BSON_ASSERT (mcommon_in_range_size_t_unsigned (src->value.v_codewscope.code_len)); size_t codewscope_len_sz = (size_t) src->value.v_codewscope.code_len; BSON_ASSERT (codewscope_len_sz <= SIZE_MAX - 1); dst->value.v_codewscope.code_len = src->value.v_codewscope.code_len; diff --git a/src/libbson/src/bson/bson.c b/src/libbson/src/bson/bson.c index 8ab0e0d040f..a87207263d3 100644 --- a/src/libbson/src/bson/bson.c +++ b/src/libbson/src/bson/bson.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "common-b64-private.h" @@ -2819,11 +2820,11 @@ _bson_as_json_visit_after (const bson_iter_t *iter, const char *key, void *data) return false; } - if (bson_cmp_greater_equal_us (state->str->len, state->max_len)) { + if (mcommon_cmp_greater_equal_us (state->str->len, state->max_len)) { state->max_len_reached = true; - if (bson_cmp_greater_us (state->str->len, state->max_len)) { - BSON_ASSERT (bson_in_range_signed (uint32_t, state->max_len)); + if (mcommon_cmp_greater_us (state->str->len, state->max_len)) { + BSON_ASSERT (mcommon_in_range_signed (uint32_t, state->max_len)); /* Truncate string to maximum length */ mcd_string_truncate (state->str, (uint32_t) state->max_len); } @@ -2921,7 +2922,7 @@ _bson_as_json_visit_codewscope ( /* Encode scope with the same mode */ if (state->max_len != BSON_MAX_LEN_UNLIMITED) { - BSON_ASSERT (bson_in_range_unsigned (int32_t, state->str->len)); + BSON_ASSERT (mcommon_in_range_unsigned (int32_t, state->str->len)); max_scope_len = BSON_MAX (0, state->max_len - (int32_t) state->str->len); } @@ -2974,7 +2975,7 @@ _bson_as_json_visit_document (const bson_iter_t *iter, const char *key, const bs child_state.mode = state->mode; child_state.max_len = BSON_MAX_LEN_UNLIMITED; if (state->max_len != BSON_MAX_LEN_UNLIMITED) { - BSON_ASSERT (bson_in_range_unsigned (int32_t, state->str->len)); + BSON_ASSERT (mcommon_in_range_unsigned (int32_t, state->str->len)); child_state.max_len = BSON_MAX (0, state->max_len - (int32_t) state->str->len); } @@ -3023,7 +3024,7 @@ _bson_as_json_visit_array (const bson_iter_t *iter, const char *key, const bson_ child_state.mode = state->mode; child_state.max_len = BSON_MAX_LEN_UNLIMITED; if (state->max_len != BSON_MAX_LEN_UNLIMITED) { - BSON_ASSERT (bson_in_range_unsigned (int32_t, state->str->len)); + BSON_ASSERT (mcommon_in_range_unsigned (int32_t, state->str->len)); child_state.max_len = BSON_MAX (0, state->max_len - (int32_t) state->str->len); } diff --git a/src/libbson/src/bson/bson.h b/src/libbson/src/bson/bson.h index f66b5a1297c..68d59cd39a0 100644 --- a/src/libbson/src/bson/bson.h +++ b/src/libbson/src/bson/bson.h @@ -27,8 +27,8 @@ #include #include -#include -#include +#include // Deprecated. +#include // Deprecated. #include #include #include diff --git a/src/libbson/tests/test-b64.c b/src/libbson/tests/test-b64.c index 7b16d187e5d..bb21a60495d 100644 --- a/src/libbson/tests/test-b64.c +++ b/src/libbson/tests/test-b64.c @@ -18,6 +18,7 @@ #include "TestSuite.h" #include "common-b64-private.h" +#include static void _test_encode_helper (char *input, size_t input_len, char *expected_output, int expected_output_len) @@ -33,7 +34,7 @@ _test_encode_helper (char *input, size_t input_len, char *expected_output, int e 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 (bson_cmp_equal_us (target_size - 1u, ret)); + ASSERT (mcommon_cmp_equal_us (target_size - 1u, ret)); ASSERT_CMPSTR (output, expected_output); bson_free (output); } diff --git a/src/libbson/tests/test-common-atomic.c b/src/libbson/tests/test-common-atomic.c new file mode 100644 index 00000000000..e0eab2196b5 --- /dev/null +++ b/src/libbson/tests/test-common-atomic.c @@ -0,0 +1,128 @@ +/* + * Copyright 2009-present MongoDB, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include +#include + +#include "TestSuite.h" + +#define ATOMIC(Kind, Operation) BSON_CONCAT4 (mcommon_atomic_, Kind, _, Operation) + + +#define TEST_KIND_WITH_MEMORDER(Kind, TypeName, MemOrder, Assert) \ + do { \ + int i; \ + TypeName got; \ + TypeName value = 0; \ + got = ATOMIC (Kind, fetch) (&value, MemOrder); \ + Assert (got, ==, 0); \ + got = ATOMIC (Kind, fetch_add) (&value, 42, MemOrder); \ + Assert (got, ==, 0); \ + Assert (value, ==, 42); \ + got = ATOMIC (Kind, fetch_sub) (&value, 7, MemOrder); \ + Assert (got, ==, 42); \ + Assert (value, ==, 35); \ + got = ATOMIC (Kind, exchange) (&value, 77, MemOrder); \ + Assert (got, ==, 35); \ + Assert (value, ==, 77); \ + /* Compare-exchange fail: */ \ + got = ATOMIC (Kind, compare_exchange_strong) (&value, 4, 9, MemOrder); \ + Assert (got, ==, 77); \ + Assert (value, ==, 77); \ + /* Compare-exchange succeed: */ \ + got = ATOMIC (Kind, compare_exchange_strong) (&value, 77, 9, MemOrder); \ + Assert (got, ==, 77); \ + Assert (value, ==, 9); \ + /* Compare-exchange fail: */ \ + got = ATOMIC (Kind, compare_exchange_weak) (&value, 8, 12, MemOrder); \ + Assert (got, ==, 9); \ + Assert (value, ==, 9); \ + /* Compare-exchange-weak succeed: */ \ + /* 'weak' may fail spuriously, so it must *eventually* succeed */ \ + for (i = 0; i < 10000 && value != 53; ++i) { \ + got = ATOMIC (Kind, compare_exchange_weak) (&value, 9, 53, MemOrder); \ + Assert (got, ==, 9); \ + } \ + /* Check that it evenutally succeeded */ \ + Assert (value, ==, 53); \ + } while (0) + +#define TEST_INTEGER_KIND(Kind, TypeName, Assert) \ + do { \ + TEST_KIND_WITH_MEMORDER (Kind, TypeName, mcommon_memory_order_relaxed, Assert); \ + TEST_KIND_WITH_MEMORDER (Kind, TypeName, mcommon_memory_order_acq_rel, Assert); \ + TEST_KIND_WITH_MEMORDER (Kind, TypeName, mcommon_memory_order_acquire, Assert); \ + TEST_KIND_WITH_MEMORDER (Kind, TypeName, mcommon_memory_order_release, Assert); \ + TEST_KIND_WITH_MEMORDER (Kind, TypeName, mcommon_memory_order_consume, Assert); \ + TEST_KIND_WITH_MEMORDER (Kind, TypeName, mcommon_memory_order_seq_cst, Assert); \ + } while (0) + + +static void +test_integers (void) +{ + TEST_INTEGER_KIND (int64, int64_t, ASSERT_CMPINT64); + TEST_INTEGER_KIND (int32, int32_t, ASSERT_CMPINT32); + TEST_INTEGER_KIND (int16, int16_t, ASSERT_CMPINT); + TEST_INTEGER_KIND (int8, int8_t, ASSERT_CMPINT); + TEST_INTEGER_KIND (int, int, ASSERT_CMPINT); +} + + +static void +test_pointers (void) +{ + int u = 12; + int v = 9; + int w = 91; + int *ptr = &v; + int *other; + int *prev; + other = mcommon_atomic_ptr_fetch ((void *) &ptr, mcommon_memory_order_relaxed); + ASSERT_CMPVOID (other, ==, ptr); + prev = mcommon_atomic_ptr_exchange ((void *) &other, &u, mcommon_memory_order_relaxed); + ASSERT_CMPVOID (prev, ==, &v); + ASSERT_CMPVOID (other, ==, &u); + prev = mcommon_atomic_ptr_compare_exchange_strong ((void *) &other, &v, &w, mcommon_memory_order_relaxed); + ASSERT_CMPVOID (prev, ==, &u); + ASSERT_CMPVOID (other, ==, &u); + prev = mcommon_atomic_ptr_compare_exchange_strong ((void *) &other, &u, &w, mcommon_memory_order_relaxed); + ASSERT_CMPVOID (prev, ==, &u); + ASSERT_CMPVOID (other, ==, &w); +} + + +static void +test_thread_fence (void) +{ + mcommon_atomic_thread_fence (); +} + +static void +test_thrd_yield (void) +{ + mcommon_thrd_yield (); +} + +void +test_mcommon_atomic_install (TestSuite *suite) +{ + TestSuite_Add (suite, "/mcommon/atomic/integers", test_integers); + TestSuite_Add (suite, "/mcommon/atomic/pointers", test_pointers); + TestSuite_Add (suite, "/mcommon/atomic/thread_fence", test_thread_fence); + TestSuite_Add (suite, "/mcommon/atomic/thread_yield", test_thrd_yield); +} diff --git a/src/libbson/tests/test-common-cmp.c b/src/libbson/tests/test-common-cmp.c new file mode 100644 index 00000000000..10812948a69 --- /dev/null +++ b/src/libbson/tests/test-common-cmp.c @@ -0,0 +1,308 @@ +/* + * Copyright 2009-present MongoDB, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "TestSuite.h" + +#include + +static void +test_mcommon_cmp_equal (void) +{ + ASSERT (mcommon_cmp_equal_ss (0, 0)); + ASSERT (!mcommon_cmp_equal_ss (0, -1)); + ASSERT (!mcommon_cmp_equal_ss (0, 1)); + ASSERT (!mcommon_cmp_equal_ss (-1, 0)); + ASSERT (mcommon_cmp_equal_ss (-1, -1)); + ASSERT (!mcommon_cmp_equal_ss (-1, 1)); + ASSERT (!mcommon_cmp_equal_ss (1, 0)); + ASSERT (!mcommon_cmp_equal_ss (1, -1)); + ASSERT (mcommon_cmp_equal_ss (1, 1)); + + ASSERT (mcommon_cmp_equal_uu (0u, 0u)); + ASSERT (!mcommon_cmp_equal_uu (0u, 1u)); + ASSERT (!mcommon_cmp_equal_uu (1u, 0u)); + ASSERT (mcommon_cmp_equal_uu (1u, 1u)); + + ASSERT (mcommon_cmp_equal_su (0, 0u)); + ASSERT (!mcommon_cmp_equal_su (0, 1u)); + ASSERT (!mcommon_cmp_equal_su (-1, 0u)); + ASSERT (!mcommon_cmp_equal_su (-1, 1u)); + ASSERT (!mcommon_cmp_equal_su (1, 0u)); + ASSERT (mcommon_cmp_equal_su (1, 1u)); + + ASSERT (mcommon_cmp_equal_us (0u, 0)); + ASSERT (!mcommon_cmp_equal_us (0u, -1)); + ASSERT (!mcommon_cmp_equal_us (0u, 1)); + ASSERT (!mcommon_cmp_equal_us (1u, 0)); + ASSERT (!mcommon_cmp_equal_us (1u, -1)); + ASSERT (mcommon_cmp_equal_us (1u, 1)); +} + +static void +test_mcommon_cmp_not_equal (void) +{ + ASSERT (!mcommon_cmp_not_equal_ss (0, 0)); + ASSERT (mcommon_cmp_not_equal_ss (0, -1)); + ASSERT (mcommon_cmp_not_equal_ss (0, 1)); + ASSERT (mcommon_cmp_not_equal_ss (-1, 0)); + ASSERT (!mcommon_cmp_not_equal_ss (-1, -1)); + ASSERT (mcommon_cmp_not_equal_ss (-1, 1)); + ASSERT (mcommon_cmp_not_equal_ss (1, 0)); + ASSERT (mcommon_cmp_not_equal_ss (1, -1)); + ASSERT (!mcommon_cmp_not_equal_ss (1, 1)); + + ASSERT (!mcommon_cmp_not_equal_uu (0u, 0u)); + ASSERT (mcommon_cmp_not_equal_uu (0u, 1u)); + ASSERT (mcommon_cmp_not_equal_uu (1u, 0u)); + ASSERT (!mcommon_cmp_not_equal_uu (1u, 1u)); + + ASSERT (!mcommon_cmp_not_equal_su (0, 0u)); + ASSERT (mcommon_cmp_not_equal_su (0, 1u)); + ASSERT (mcommon_cmp_not_equal_su (-1, 0u)); + ASSERT (mcommon_cmp_not_equal_su (-1, 1u)); + ASSERT (mcommon_cmp_not_equal_su (1, 0u)); + ASSERT (!mcommon_cmp_not_equal_su (1, 1u)); + + ASSERT (!mcommon_cmp_not_equal_us (0u, 0)); + ASSERT (mcommon_cmp_not_equal_us (0u, -1)); + ASSERT (mcommon_cmp_not_equal_us (0u, 1)); + ASSERT (mcommon_cmp_not_equal_us (1u, 0)); + ASSERT (mcommon_cmp_not_equal_us (1u, -1)); + ASSERT (!mcommon_cmp_not_equal_us (1u, 1)); +} + +static void +test_mcommon_cmp_less (void) +{ + ASSERT (!mcommon_cmp_less_ss (0, 0)); + ASSERT (!mcommon_cmp_less_ss (0, -1)); + ASSERT (mcommon_cmp_less_ss (0, 1)); + ASSERT (mcommon_cmp_less_ss (-1, 0)); + ASSERT (!mcommon_cmp_less_ss (-1, -1)); + ASSERT (mcommon_cmp_less_ss (-1, 1)); + ASSERT (!mcommon_cmp_less_ss (1, 0)); + ASSERT (!mcommon_cmp_less_ss (1, -1)); + ASSERT (!mcommon_cmp_less_ss (1, 1)); + + ASSERT (!mcommon_cmp_less_uu (0u, 0u)); + ASSERT (mcommon_cmp_less_uu (0u, 1u)); + ASSERT (!mcommon_cmp_less_uu (1u, 0u)); + ASSERT (!mcommon_cmp_less_uu (1u, 1u)); + + ASSERT (!mcommon_cmp_less_su (0, 0u)); + ASSERT (mcommon_cmp_less_su (0, 1u)); + ASSERT (mcommon_cmp_less_su (-1, 0u)); + ASSERT (mcommon_cmp_less_su (-1, 1u)); + ASSERT (!mcommon_cmp_less_su (1, 0u)); + ASSERT (!mcommon_cmp_less_su (1, 1u)); + + ASSERT (!mcommon_cmp_less_us (0u, 0)); + ASSERT (!mcommon_cmp_less_us (0u, -1)); + ASSERT (mcommon_cmp_less_us (0u, 1)); + ASSERT (!mcommon_cmp_less_us (1u, 0)); + ASSERT (!mcommon_cmp_less_us (1u, -1)); + ASSERT (!mcommon_cmp_less_us (1u, 1)); +} + +static void +test_mcommon_cmp_greater (void) +{ + ASSERT (!mcommon_cmp_greater_ss (0, 0)); + ASSERT (mcommon_cmp_greater_ss (0, -1)); + ASSERT (!mcommon_cmp_greater_ss (0, 1)); + ASSERT (!mcommon_cmp_greater_ss (-1, 0)); + ASSERT (!mcommon_cmp_greater_ss (-1, -1)); + ASSERT (!mcommon_cmp_greater_ss (-1, 1)); + ASSERT (mcommon_cmp_greater_ss (1, 0)); + ASSERT (mcommon_cmp_greater_ss (1, -1)); + ASSERT (!mcommon_cmp_greater_ss (1, 1)); + + ASSERT (!mcommon_cmp_greater_uu (0u, 0u)); + ASSERT (!mcommon_cmp_greater_uu (0u, 1u)); + ASSERT (mcommon_cmp_greater_uu (1u, 0u)); + ASSERT (!mcommon_cmp_greater_uu (1u, 1u)); + + ASSERT (!mcommon_cmp_greater_su (0, 0u)); + ASSERT (!mcommon_cmp_greater_su (0, 1u)); + ASSERT (!mcommon_cmp_greater_su (-1, 0u)); + ASSERT (!mcommon_cmp_greater_su (-1, 1u)); + ASSERT (mcommon_cmp_greater_su (1, 0u)); + ASSERT (!mcommon_cmp_greater_su (1, 1u)); + + ASSERT (!mcommon_cmp_greater_us (0u, 0)); + ASSERT (mcommon_cmp_greater_us (0u, -1)); + ASSERT (!mcommon_cmp_greater_us (0u, 1)); + ASSERT (mcommon_cmp_greater_us (1u, 0)); + ASSERT (mcommon_cmp_greater_us (1u, -1)); + ASSERT (!mcommon_cmp_greater_us (1u, 1)); +} + +static void +test_mcommon_cmp_less_equal (void) +{ + ASSERT (mcommon_cmp_less_equal_ss (0, 0)); + ASSERT (!mcommon_cmp_less_equal_ss (0, -1)); + ASSERT (mcommon_cmp_less_equal_ss (0, 1)); + ASSERT (mcommon_cmp_less_equal_ss (-1, 0)); + ASSERT (mcommon_cmp_less_equal_ss (-1, -1)); + ASSERT (mcommon_cmp_less_equal_ss (-1, 1)); + ASSERT (!mcommon_cmp_less_equal_ss (1, 0)); + ASSERT (!mcommon_cmp_less_equal_ss (1, -1)); + ASSERT (mcommon_cmp_less_equal_ss (1, 1)); + + ASSERT (mcommon_cmp_less_equal_uu (0u, 0u)); + ASSERT (mcommon_cmp_less_equal_uu (0u, 1u)); + ASSERT (!mcommon_cmp_less_equal_uu (1u, 0u)); + ASSERT (mcommon_cmp_less_equal_uu (1u, 1u)); + + ASSERT (mcommon_cmp_less_equal_su (0, 0u)); + ASSERT (mcommon_cmp_less_equal_su (0, 1u)); + ASSERT (mcommon_cmp_less_equal_su (-1, 0u)); + ASSERT (mcommon_cmp_less_equal_su (-1, 1u)); + ASSERT (!mcommon_cmp_less_equal_su (1, 0u)); + ASSERT (mcommon_cmp_less_equal_su (1, 1u)); + + ASSERT (mcommon_cmp_less_equal_us (0u, 0)); + ASSERT (!mcommon_cmp_less_equal_us (0u, -1)); + ASSERT (mcommon_cmp_less_equal_us (0u, 1)); + ASSERT (!mcommon_cmp_less_equal_us (1u, 0)); + ASSERT (!mcommon_cmp_less_equal_us (1u, -1)); + ASSERT (mcommon_cmp_less_equal_us (1u, 1)); +} + +static void +test_mcommon_cmp_greater_equal (void) +{ + ASSERT (mcommon_cmp_greater_equal_ss (0, 0)); + ASSERT (mcommon_cmp_greater_equal_ss (0, -1)); + ASSERT (!mcommon_cmp_greater_equal_ss (0, 1)); + ASSERT (!mcommon_cmp_greater_equal_ss (-1, 0)); + ASSERT (mcommon_cmp_greater_equal_ss (-1, -1)); + ASSERT (!mcommon_cmp_greater_equal_ss (-1, 1)); + ASSERT (mcommon_cmp_greater_equal_ss (1, 0)); + ASSERT (mcommon_cmp_greater_equal_ss (1, -1)); + ASSERT (mcommon_cmp_greater_equal_ss (1, 1)); + + ASSERT (mcommon_cmp_greater_equal_uu (0u, 0u)); + ASSERT (!mcommon_cmp_greater_equal_uu (0u, 1u)); + ASSERT (mcommon_cmp_greater_equal_uu (1u, 0u)); + ASSERT (mcommon_cmp_greater_equal_uu (1u, 1u)); + + ASSERT (mcommon_cmp_greater_equal_su (0, 0u)); + ASSERT (!mcommon_cmp_greater_equal_su (0, 1u)); + ASSERT (!mcommon_cmp_greater_equal_su (-1, 0u)); + ASSERT (!mcommon_cmp_greater_equal_su (-1, 1u)); + ASSERT (mcommon_cmp_greater_equal_su (1, 0u)); + ASSERT (mcommon_cmp_greater_equal_su (1, 1u)); + + ASSERT (mcommon_cmp_greater_equal_us (0u, 0)); + ASSERT (mcommon_cmp_greater_equal_us (0u, -1)); + ASSERT (!mcommon_cmp_greater_equal_us (0u, 1)); + ASSERT (mcommon_cmp_greater_equal_us (1u, 0)); + ASSERT (mcommon_cmp_greater_equal_us (1u, -1)); + ASSERT (mcommon_cmp_greater_equal_us (1u, 1)); +} + +/* Sanity check: ensure ssize_t limits are as expected relative to size_t. */ +BSON_STATIC_ASSERT2 (ssize_t_size_min_check, SSIZE_MIN + 1 == -SSIZE_MAX); +BSON_STATIC_ASSERT2 (ssize_t_size_max_check, (size_t) SSIZE_MAX <= SIZE_MAX); + +static void +test_mcommon_in_range (void) +{ + const int64_t int8_min = INT8_MIN; + const int64_t int8_max = INT8_MAX; + const int64_t int32_min = INT32_MIN; + const int64_t int32_max = INT32_MAX; + + const uint64_t uint8_max = UINT8_MAX; + const uint64_t uint32_max = UINT32_MAX; + + const ssize_t ssize_min = SSIZE_MIN; + const ssize_t ssize_max = SSIZE_MAX; + + ASSERT (!mcommon_in_range_signed (int8_t, int8_min - 1)); + ASSERT (mcommon_in_range_signed (int8_t, int8_min)); + ASSERT (mcommon_in_range_signed (int8_t, 0)); + ASSERT (mcommon_in_range_signed (int8_t, int8_max)); + ASSERT (!mcommon_in_range_signed (int8_t, int8_max + 1)); + + ASSERT (mcommon_in_range_unsigned (int8_t, 0u)); + ASSERT (mcommon_in_range_unsigned (int8_t, (uint64_t) int8_max)); + ASSERT (!mcommon_in_range_unsigned (int8_t, (uint64_t) (int8_max + 1))); + + ASSERT (!mcommon_in_range_signed (uint8_t, int8_min - 1)); + ASSERT (!mcommon_in_range_signed (uint8_t, int8_min)); + ASSERT (mcommon_in_range_signed (uint8_t, 0)); + ASSERT (mcommon_in_range_signed (uint8_t, int8_max)); + ASSERT (mcommon_in_range_signed (uint8_t, int8_max + 1)); + ASSERT (mcommon_in_range_signed (uint8_t, (int64_t) uint8_max)); + ASSERT (!mcommon_in_range_signed (uint8_t, (int64_t) uint8_max + 1)); + + ASSERT (mcommon_in_range_unsigned (uint8_t, 0u)); + ASSERT (mcommon_in_range_unsigned (uint8_t, uint8_max)); + ASSERT (!mcommon_in_range_unsigned (uint8_t, uint8_max + 1u)); + + ASSERT (!mcommon_in_range_signed (int32_t, int32_min - 1)); + ASSERT (mcommon_in_range_signed (int32_t, int32_min)); + ASSERT (mcommon_in_range_signed (int32_t, 0)); + ASSERT (mcommon_in_range_signed (int32_t, int32_max)); + ASSERT (!mcommon_in_range_signed (int32_t, int32_max + 1)); + + ASSERT (mcommon_in_range_unsigned (int32_t, 0u)); + ASSERT (mcommon_in_range_unsigned (int32_t, (uint64_t) int32_max)); + ASSERT (!mcommon_in_range_unsigned (int32_t, (uint64_t) (int32_max + 1))); + + ASSERT (!mcommon_in_range_signed (uint32_t, int32_min - 1)); + ASSERT (!mcommon_in_range_signed (uint32_t, int32_min)); + ASSERT (mcommon_in_range_signed (uint32_t, 0)); + ASSERT (mcommon_in_range_signed (uint32_t, int32_max)); + ASSERT (mcommon_in_range_signed (uint32_t, int32_max + 1)); + ASSERT (mcommon_in_range_signed (uint32_t, (int64_t) uint32_max)); + ASSERT (!mcommon_in_range_signed (uint32_t, (int64_t) uint32_max + 1)); + + ASSERT (mcommon_in_range_unsigned (uint32_t, 0u)); + ASSERT (mcommon_in_range_unsigned (uint32_t, uint32_max)); + ASSERT (!mcommon_in_range_unsigned (uint32_t, uint32_max + 1u)); + + ASSERT (mcommon_in_range_signed (ssize_t, ssize_min)); + ASSERT (mcommon_in_range_signed (ssize_t, 0)); + ASSERT (mcommon_in_range_signed (ssize_t, ssize_max)); + + ASSERT (mcommon_in_range_unsigned (ssize_t, 0u)); + ASSERT (mcommon_in_range_unsigned (ssize_t, (size_t) ssize_max)); + ASSERT (!mcommon_in_range_unsigned (ssize_t, (size_t) ssize_max + 1u)); + + ASSERT (!mcommon_in_range_signed (size_t, ssize_min)); + ASSERT (mcommon_in_range_signed (size_t, 0)); + ASSERT (mcommon_in_range_signed (size_t, ssize_max)); + + ASSERT (mcommon_in_range_unsigned (size_t, 0u)); + ASSERT (mcommon_in_range_unsigned (size_t, (size_t) ssize_max)); + ASSERT (mcommon_in_range_unsigned (size_t, (size_t) ssize_max + 1u)); +} + +void +test_mcommon_cmp_install (TestSuite *suite) +{ + TestSuite_Add (suite, "/mcommon/cmp/equal", test_mcommon_cmp_equal); + TestSuite_Add (suite, "/mcommon/cmp/not_equal", test_mcommon_cmp_not_equal); + TestSuite_Add (suite, "/mcommon/cmp/less", test_mcommon_cmp_less); + TestSuite_Add (suite, "/mcommon/cmp/greater", test_mcommon_cmp_greater); + TestSuite_Add (suite, "/mcommon/cmp/less_equal", test_mcommon_cmp_less_equal); + TestSuite_Add (suite, "/mcommon/cmp/greater_equal", test_mcommon_cmp_greater_equal); + TestSuite_Add (suite, "/mcommon/cmp/in_range", test_mcommon_in_range); +} diff --git a/src/libbson/tests/test-iso8601.c b/src/libbson/tests/test-iso8601.c index 34e3141f290..0cdd394869c 100644 --- a/src/libbson/tests/test-iso8601.c +++ b/src/libbson/tests/test-iso8601.c @@ -2,6 +2,7 @@ #include "bson/bson-iso8601-private.h" #include "TestSuite.h" +#include static const bool is_time_t_small = (sizeof (time_t) == sizeof (int32_t)); @@ -13,7 +14,7 @@ test_date (const char *str, int64_t millis) const size_t len = strlen (str); - BSON_ASSERT (bson_in_range_unsigned (int32_t, len)); + BSON_ASSERT (mcommon_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); @@ -59,7 +60,7 @@ test_date_should_fail (const char *str) const size_t len = strlen (str); - BSON_ASSERT (bson_in_range_unsigned (int32_t, len)); + BSON_ASSERT (mcommon_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); diff --git a/src/libbson/tests/test-json.c b/src/libbson/tests/test-json.c index 863d107d4d6..93a23e722d0 100644 --- a/src/libbson/tests/test-json.c +++ b/src/libbson/tests/test-json.c @@ -7,6 +7,7 @@ #include "TestSuite.h" #include "test-conveniences.h" #include +#include static ssize_t test_bson_json_read_cb_helper (void *string, uint8_t *buf, size_t len) @@ -2716,7 +2717,7 @@ test_bson_as_json_with_opts (bson_t *bson, bson_json_mode_t mode, int max_len, c ASSERT_CMPSIZE_T (json_len, ==, strlen (expected)); if (max_len != BSON_MAX_LEN_UNLIMITED) { - ASSERT (bson_in_range_signed (size_t, max_len)); + ASSERT (mcommon_in_range_signed (size_t, max_len)); ASSERT_CMPSIZE_T (json_len, <=, (size_t) max_len); } @@ -2742,7 +2743,7 @@ run_bson_as_json_with_opts_tests (bson_t *bson, bson_json_mode_t mode, const cha const size_t ulen = strlen (expected); char *truncated; - BSON_ASSERT (bson_in_range_unsigned (int, ulen)); + BSON_ASSERT (mcommon_in_range_unsigned (int, ulen)); const int len = (int) ulen; /* Test with 0 length (empty string). */ diff --git a/src/libmongoc/CMakeLists.txt b/src/libmongoc/CMakeLists.txt index 2347bca9075..5622dbecc6e 100644 --- a/src/libmongoc/CMakeLists.txt +++ b/src/libmongoc/CMakeLists.txt @@ -660,6 +660,7 @@ set (MONGOC_SOURCES ${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-write-command.c ${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-write-concern.c ${PROJECT_SOURCE_DIR}/src/mongoc/service-gcp.c + ${mongo-c-driver_SOURCE_DIR}/src/common/common-atomic.c ${mongo-c-driver_SOURCE_DIR}/src/common/common-b64.c ${mongo-c-driver_SOURCE_DIR}/src/common/common-md5.c ${mongo-c-driver_SOURCE_DIR}/src/common/common-thread.c @@ -1027,8 +1028,10 @@ endif () set (test-libmongoc-sources + ${mongo-c-driver_SOURCE_DIR}/src/libbson/tests/test-common-cmp.c ${mongo-c-driver_SOURCE_DIR}/src/libbson/tests/corpus-test.c ${mongo-c-driver_SOURCE_DIR}/src/libbson/tests/corpus-test.h + ${mongo-c-driver_SOURCE_DIR}/src/libbson/tests/test-common-atomic.c ${mongo-c-driver_SOURCE_DIR}/src/libbson/tests/test-atomic.c ${mongo-c-driver_SOURCE_DIR}/src/libbson/tests/test-b64.c ${mongo-c-driver_SOURCE_DIR}/src/libbson/tests/test-bson.c diff --git a/src/libmongoc/examples/client-side-encryption-auto-decryption.c b/src/libmongoc/examples/client-side-encryption-auto-decryption.c index 016cdca6553..a1353bf2dc6 100644 --- a/src/libmongoc/examples/client-side-encryption-auto-decryption.c +++ b/src/libmongoc/examples/client-side-encryption-auto-decryption.c @@ -142,7 +142,7 @@ main (void) to_encrypt.value_type = BSON_TYPE_UTF8; to_encrypt.value.v_utf8.str = "123456789"; const size_t len = strlen (to_encrypt.value.v_utf8.str); - BSON_ASSERT (bson_in_range_unsigned (uint32_t, len)); + BSON_ASSERT (len <= UINT32_MAX); to_encrypt.value.v_utf8.len = (uint32_t) len; ret = mongoc_client_encryption_encrypt (client_encryption, &to_encrypt, encrypt_opts, &encrypted_field, &error); diff --git a/src/libmongoc/examples/client-side-encryption-explicit.c b/src/libmongoc/examples/client-side-encryption-explicit.c index 4bf235888ef..b347d71e4f1 100644 --- a/src/libmongoc/examples/client-side-encryption-explicit.c +++ b/src/libmongoc/examples/client-side-encryption-explicit.c @@ -118,7 +118,7 @@ main (void) to_encrypt.value_type = BSON_TYPE_UTF8; to_encrypt.value.v_utf8.str = "123456789"; const size_t len = strlen (to_encrypt.value.v_utf8.str); - BSON_ASSERT (bson_in_range_unsigned (uint32_t, len)); + BSON_ASSERT (len <= UINT32_MAX); to_encrypt.value.v_utf8.len = (uint32_t) len; ret = mongoc_client_encryption_encrypt (client_encryption, &to_encrypt, encrypt_opts, &encrypted_field, &error); diff --git a/src/libmongoc/examples/client-side-encryption-helpers.c b/src/libmongoc/examples/client-side-encryption-helpers.c index 3e917c1961a..ca56a203f8e 100644 --- a/src/libmongoc/examples/client-side-encryption-helpers.c +++ b/src/libmongoc/examples/client-side-encryption-helpers.c @@ -13,8 +13,9 @@ hex_to_bin (const char *hex, uint32_t *len) return NULL; } - if (!bson_in_range_unsigned (uint32_t, hex_len / 2u)) { - return NULL; + size_t num_bytes = hex_len / 2u; + if (num_bytes >= UINT32_MAX) { + return false; } *len = (uint32_t) (hex_len / 2u); diff --git a/src/libmongoc/src/mongoc/mcd-azure.c b/src/libmongoc/src/mongoc/mcd-azure.c index 204e0d6f2e5..299ab2c09af 100644 --- a/src/libmongoc/src/mongoc/mcd-azure.c +++ b/src/libmongoc/src/mongoc/mcd-azure.c @@ -17,6 +17,7 @@ #include "./mcd-azure.h" #include "mongoc-util-private.h" +#include #define AZURE_API_VERSION "2018-02-01" @@ -126,7 +127,7 @@ mcd_azure_access_token_try_init_from_json_str (mcd_azure_access_token *out, MONGOC_ERROR_AZURE, MONGOC_ERROR_KMS_SERVER_BAD_JSON, "Invalid 'expires_in' string \"%.*s\" from IMDS server", - bson_in_range_unsigned (int, expires_in_len) ? (int) expires_in_len : INT_MAX, + mcommon_in_range_unsigned (int, expires_in_len) ? (int) expires_in_len : INT_MAX, expires_in_str); } else { out->expires_in = mcd_seconds (s); diff --git a/src/libmongoc/src/mongoc/mcd-rpc.c b/src/libmongoc/src/mongoc/mcd-rpc.c index a5ebbf0b308..51103e5391b 100644 --- a/src/libmongoc/src/mongoc/mcd-rpc.c +++ b/src/libmongoc/src/mongoc/mcd-rpc.c @@ -6,6 +6,7 @@ #undef MONGOC_INSIDE #include +#include typedef struct _mcd_rpc_message_header mcd_rpc_message_header; @@ -282,7 +283,7 @@ _consume_bson_objects (const uint8_t **ptr, size_t *remaining_bytes, int32_t *nu } if (doc_len < MONGOC_RPC_MINIMUM_BSON_LENGTH || - bson_cmp_greater_su (doc_len, *remaining_bytes + sizeof (int32_t))) { + mcommon_cmp_greater_su (doc_len, *remaining_bytes + sizeof (int32_t))) { *ptr -= sizeof (int32_t); // Revert so *data_end points to start of // document as invalid input. return false; @@ -385,7 +386,7 @@ _consume_op_msg_section ( // identifier field, but 4 bytes is sufficient to avoid unsigned integer // overflow when computing `remaining_section_bytes` and to encourage as // much progress is made parsing input data as able. - if (bson_cmp_less_su (section.payload.document_sequence.section_len, sizeof (int32_t))) { + if (mcommon_cmp_less_su (section.payload.document_sequence.section_len, sizeof (int32_t))) { *ptr -= sizeof (int32_t); // Revert so *data_end points to start of // document sequence as invalid input. return false; @@ -785,7 +786,7 @@ _consume_op_kill_cursors (mcd_rpc_message *rpc, const uint8_t **ptr, size_t *rem if (op_kill_cursors->number_of_cursor_ids < 0 || // Truncation may (deliberately) leave unparsed bytes that will later // trigger validation failure due to unexpected remaining bytes. - bson_cmp_greater_su (op_kill_cursors->number_of_cursor_ids, *remaining_bytes / sizeof (int64_t))) { + mcommon_cmp_greater_su (op_kill_cursors->number_of_cursor_ids, *remaining_bytes / sizeof (int64_t))) { *ptr -= sizeof (int32_t); // Revert so *data_len points to start of // numberOfCursorIds as invalid input. return false; @@ -851,7 +852,7 @@ mcd_rpc_message_from_data_in_place (mcd_rpc_message *rpc, const void *data, size } if (rpc->msg_header.message_length < MONGOC_RPC_MINIMUM_MESSAGE_LENGTH || - bson_cmp_greater_su (rpc->msg_header.message_length, remaining_bytes + sizeof (int32_t))) { + mcommon_cmp_greater_su (rpc->msg_header.message_length, remaining_bytes + sizeof (int32_t))) { ptr -= sizeof (int32_t); // Revert so *data_end points to start of // messageLength as invalid input. goto fail; @@ -1799,7 +1800,7 @@ mcd_rpc_op_compressed_set_compressed_message (mcd_rpc_message *rpc, { ASSERT_MCD_RPC_ACCESSOR_PRECONDITIONS; BSON_ASSERT (rpc->msg_header.op_code == MONGOC_OP_CODE_COMPRESSED); - BSON_ASSERT (bson_in_range_unsigned (int32_t, compressed_message_length)); + BSON_ASSERT (mcommon_in_range_unsigned (int32_t, compressed_message_length)); rpc->op_compressed.compressed_message = compressed_message; rpc->op_compressed.compressed_message_len = compressed_message_length; return (int32_t) compressed_message_length; @@ -1920,7 +1921,7 @@ mcd_rpc_op_msg_section_set_identifier (mcd_rpc_message *rpc, size_t index, const rpc->op_msg.sections[index].payload.document_sequence.identifier = identifier; rpc->op_msg.sections[index].payload.document_sequence.identifier_len = identifier_len; - BSON_ASSERT (bson_in_range_unsigned (int32_t, identifier_len)); + BSON_ASSERT (mcommon_in_range_unsigned (int32_t, identifier_len)); return (int32_t) identifier_len; } @@ -1956,7 +1957,7 @@ mcd_rpc_op_msg_section_set_document_sequence (mcd_rpc_message *rpc, rpc->op_msg.sections[index].payload.document_sequence.bson_objects = document_sequence; rpc->op_msg.sections[index].payload.document_sequence.bson_objects_len = bson_objects_len; - BSON_ASSERT (bson_in_range_unsigned (int32_t, document_sequence_length)); + BSON_ASSERT (mcommon_in_range_unsigned (int32_t, document_sequence_length)); return (int32_t) bson_objects_len; } @@ -2103,7 +2104,7 @@ mcd_rpc_op_reply_set_documents (mcd_rpc_message *rpc, const void *documents, siz rpc->op_reply.documents = documents; rpc->op_reply.documents_len = documents_len; - BSON_ASSERT (bson_in_range_unsigned (int32_t, documents_len)); + BSON_ASSERT (mcommon_in_range_unsigned (int32_t, documents_len)); return (int32_t) documents_len; } @@ -2151,7 +2152,7 @@ mcd_rpc_op_update_set_full_collection_name (mcd_rpc_message *rpc, const char *fu rpc->op_update.full_collection_name = full_collection_name; rpc->op_update.full_collection_name_len = length; - BSON_ASSERT (bson_in_range_unsigned (int32_t, length)); + BSON_ASSERT (mcommon_in_range_unsigned (int32_t, length)); return (int32_t) length; } @@ -2232,7 +2233,7 @@ mcd_rpc_op_insert_set_full_collection_name (mcd_rpc_message *rpc, const char *fu rpc->op_insert.full_collection_name = full_collection_name; rpc->op_insert.full_collection_name_len = length; - BSON_ASSERT (bson_in_range_unsigned (int32_t, length)); + BSON_ASSERT (mcommon_in_range_unsigned (int32_t, length)); return (int32_t) length; } @@ -2245,7 +2246,7 @@ mcd_rpc_op_insert_set_documents (mcd_rpc_message *rpc, const void *documents, si rpc->op_insert.documents = documents; rpc->op_insert.documents_len = documents_len; - BSON_ASSERT (bson_in_range_unsigned (int32_t, documents_len)); + BSON_ASSERT (mcommon_in_range_unsigned (int32_t, documents_len)); return (int32_t) documents_len; } @@ -2318,7 +2319,7 @@ mcd_rpc_op_query_set_full_collection_name (mcd_rpc_message *rpc, const char *ful rpc->op_query.full_collection_name = full_collection_name; rpc->op_query.full_collection_name_len = length; - BSON_ASSERT (bson_in_range_unsigned (int32_t, length)); + BSON_ASSERT (mcommon_in_range_unsigned (int32_t, length)); return (int32_t) length; } @@ -2394,7 +2395,7 @@ mcd_rpc_op_get_more_set_full_collection_name (mcd_rpc_message *rpc, const char * rpc->op_get_more.full_collection_name = full_collection_name; rpc->op_get_more.full_collection_name_len = length; - BSON_ASSERT (bson_in_range_unsigned (int32_t, length)); + BSON_ASSERT (mcommon_in_range_unsigned (int32_t, length)); return (int32_t) length; } @@ -2452,7 +2453,7 @@ mcd_rpc_op_delete_set_full_collection_name (mcd_rpc_message *rpc, const char *fu rpc->op_delete.full_collection_name = full_collection_name; rpc->op_delete.full_collection_name_len = length; - BSON_ASSERT (bson_in_range_unsigned (int32_t, length)); + BSON_ASSERT (mcommon_in_range_unsigned (int32_t, length)); return (int32_t) length; } @@ -2496,7 +2497,7 @@ mcd_rpc_op_kill_cursors_set_cursor_ids (mcd_rpc_message *rpc, const int64_t *cur { ASSERT_MCD_RPC_ACCESSOR_PRECONDITIONS; BSON_ASSERT (rpc->msg_header.op_code == MONGOC_OP_CODE_KILL_CURSORS); - BSON_ASSERT (bson_cmp_less_su (number_of_cursor_ids, (size_t) INT32_MAX / sizeof (int64_t))); + BSON_ASSERT (mcommon_cmp_less_su (number_of_cursor_ids, (size_t) INT32_MAX / sizeof (int64_t))); const size_t cursor_ids_length = (size_t) number_of_cursor_ids * sizeof (int64_t); diff --git a/src/libmongoc/src/mongoc/mongoc-buffer.c b/src/libmongoc/src/mongoc/mongoc-buffer.c index 0eafdaad65c..4ae1cb3069e 100644 --- a/src/libmongoc/src/mongoc/mongoc-buffer.c +++ b/src/libmongoc/src/mongoc/mongoc-buffer.c @@ -21,6 +21,7 @@ #include "mongoc-error.h" #include "mongoc-buffer-private.h" #include "mongoc-trace-private.h" +#include #undef MONGOC_LOG_DOMAIN @@ -185,7 +186,7 @@ _mongoc_buffer_append_from_stream ( BSON_ASSERT ((buffer->len + size) <= buffer->datalen); - if (BSON_UNLIKELY (!bson_in_range_signed (int32_t, timeout_msec))) { + if (BSON_UNLIKELY (!mcommon_in_range_signed (int32_t, timeout_msec))) { // CDRIVER-4589 bson_set_error (error, MONGOC_ERROR_STREAM, @@ -196,7 +197,7 @@ _mongoc_buffer_append_from_stream ( } ret = mongoc_stream_read (stream, buf, size, size, (int32_t) timeout_msec); - if (bson_cmp_not_equal_su (ret, size)) { + if (mcommon_cmp_not_equal_su (ret, size)) { bson_set_error (error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_SOCKET, @@ -238,7 +239,7 @@ _mongoc_buffer_fill ( BSON_ASSERT (buffer->datalen); if (min_bytes <= buffer->len) { - BSON_ASSERT (bson_in_range_unsigned (ssize_t, buffer->len)); + BSON_ASSERT (mcommon_in_range_unsigned (ssize_t, buffer->len)); RETURN ((ssize_t) buffer->len); } @@ -248,7 +249,7 @@ _mongoc_buffer_fill ( avail_bytes = buffer->datalen - buffer->len; - if (BSON_UNLIKELY (!bson_in_range_signed (int32_t, timeout_msec))) { + if (BSON_UNLIKELY (!mcommon_in_range_signed (int32_t, timeout_msec))) { // CDRIVER-4589 bson_set_error (error, MONGOC_ERROR_STREAM, @@ -277,7 +278,7 @@ _mongoc_buffer_fill ( RETURN (-1); } - BSON_ASSERT (bson_in_range_unsigned (ssize_t, buffer->len)); + BSON_ASSERT (mcommon_in_range_unsigned (ssize_t, buffer->len)); RETURN ((ssize_t) buffer->len); } @@ -318,7 +319,7 @@ _mongoc_buffer_try_append_from_stream (mongoc_buffer_t *buffer, BSON_ASSERT ((buffer->len + size) <= buffer->datalen); - if (BSON_UNLIKELY (!bson_in_range_signed (int32_t, timeout_msec))) { + if (BSON_UNLIKELY (!mcommon_in_range_signed (int32_t, timeout_msec))) { // CDRIVER-4589 MONGOC_ERROR ("timeout_msec value %" PRId64 " exceeds supported 32-bit range", timeout_msec); RETURN (-1); diff --git a/src/libmongoc/src/mongoc/mongoc-bulkwrite.c b/src/libmongoc/src/mongoc/mongoc-bulkwrite.c index f24502185e3..d16ab9c1443 100644 --- a/src/libmongoc/src/mongoc/mongoc-bulkwrite.c +++ b/src/libmongoc/src/mongoc/mongoc-bulkwrite.c @@ -28,6 +28,7 @@ #include #include // _mongoc_iter_document_as_bson #include +#include MC_ENABLE_CONVERSION_WARNING_BEGIN @@ -275,7 +276,7 @@ mongoc_bulkwrite_append_insertone (mongoc_bulkwrite_t *self, // Store an iterator to the document's `_id` in the persisted payload: bson_iter_t persisted_id_iter; { - BSON_ASSERT (bson_in_range_size_t_unsigned (op.len)); + BSON_ASSERT (mcommon_in_range_size_t_unsigned (op.len)); size_t start = self->ops.len - (size_t) op.len; BSON_ASSERT (bson_iter_init_from_data_at_offset ( &persisted_id_iter, self->ops.data + start, (size_t) op.len, persisted_id_offset, strlen ("_id"))); @@ -1339,7 +1340,7 @@ _bulkwritereturn_apply_result (mongoc_bulkwritereturn_t *self, } } - BSON_ASSERT (bson_in_range_size_t_signed (idx)); + BSON_ASSERT (mcommon_in_range_size_t_signed (idx)); // `models_idx` is the index of the model that produced this result. size_t models_idx = (size_t) idx + ops_doc_offset; if (ok == 0) { @@ -1578,7 +1579,7 @@ mongoc_bulkwrite_execute (mongoc_bulkwrite_t *self, const mongoc_bulkwriteopts_t goto fail; } if (!mongoc_write_concern_is_acknowledged (wc) && - bson_cmp_greater_us (self->max_insert_len, maxBsonObjectSize)) { + mcommon_cmp_greater_us (self->max_insert_len, maxBsonObjectSize)) { bson_set_error (&error, MONGOC_ERROR_COMMAND, MONGOC_ERROR_COMMAND_INVALID_ARG, @@ -1712,7 +1713,7 @@ mongoc_bulkwrite_execute (mongoc_bulkwrite_t *self, const mongoc_bulkwriteopts_t mongoc_cmd_payload_t *payload = &parts.assembled.payloads[0]; const mongoc_buffer_t *nsinfo_docseq = mcd_nsinfo_as_document_sequence (nsinfo); payload->documents = nsinfo_docseq->data; - BSON_ASSERT (bson_in_range_int32_t_unsigned (nsinfo_docseq->len)); + BSON_ASSERT (mcommon_in_range_int32_t_unsigned (nsinfo_docseq->len)); payload->size = (int32_t) nsinfo_docseq->len; payload->identifier = "nsInfo"; } @@ -1722,7 +1723,7 @@ mongoc_bulkwrite_execute (mongoc_bulkwrite_t *self, const mongoc_bulkwriteopts_t mongoc_cmd_payload_t *payload = &parts.assembled.payloads[1]; payload->identifier = "ops"; payload->documents = self->ops.data + ops_byte_offset; - BSON_ASSERT (bson_in_range_int32_t_unsigned (ops_byte_len)); + BSON_ASSERT (mcommon_in_range_int32_t_unsigned (ops_byte_len)); payload->size = (int32_t) ops_byte_len; } @@ -1780,7 +1781,7 @@ mongoc_bulkwrite_execute (mongoc_bulkwrite_t *self, const mongoc_bulkwriteopts_t bson_t cursor_opts = BSON_INITIALIZER; { uint32_t serverid = parts.assembled.server_stream->sd->id; - BSON_ASSERT (bson_in_range_int32_t_unsigned (serverid)); + BSON_ASSERT (mcommon_in_range_int32_t_unsigned (serverid)); int32_t serverid_i32 = (int32_t) serverid; BSON_ASSERT (BSON_APPEND_INT32 (&cursor_opts, "serverId", serverid_i32)); // Use same session if one was applied. @@ -1857,7 +1858,7 @@ mongoc_bulkwrite_execute (mongoc_bulkwrite_t *self, const mongoc_bulkwriteopts_t has_successful_results = true; } } else { - BSON_ASSERT (bson_in_range_size_t_signed (ret.res->errorscount)); + BSON_ASSERT (mcommon_in_range_size_t_signed (ret.res->errorscount)); size_t errorscount_sz = (size_t) ret.res->errorscount; if (errorscount_sz < self->n_ops) { has_successful_results = true; diff --git a/src/libmongoc/src/mongoc/mongoc-client-side-encryption.c b/src/libmongoc/src/mongoc/mongoc-client-side-encryption.c index 620e8e7ffc9..eed4d02c2cd 100644 --- a/src/libmongoc/src/mongoc/mongoc-client-side-encryption.c +++ b/src/libmongoc/src/mongoc/mongoc-client-side-encryption.c @@ -31,6 +31,7 @@ #include "mongoc-database-private.h" #include "mongoc-util-private.h" #include +#include /*-------------------------------------------------------------------------- * Auto Encryption options. @@ -1901,14 +1902,14 @@ _mongoc_cse_client_pool_enable_auto_encryption (mongoc_topology_t *topology, GOTO (fail); } - prev_cse_state = bson_atomic_int_compare_exchange_strong ( - (int *) &topology->cse_state, MONGOC_CSE_DISABLED, MONGOC_CSE_STARTING, bson_memory_order_acquire); + prev_cse_state = mcommon_atomic_int_compare_exchange_strong ( + (int *) &topology->cse_state, MONGOC_CSE_DISABLED, MONGOC_CSE_STARTING, mcommon_memory_order_acquire); while (prev_cse_state == MONGOC_CSE_STARTING) { /* Another thread is starting client-side encryption. It may take some * time to start, but don't continue until it is finished. */ bson_thrd_yield (); - prev_cse_state = bson_atomic_int_compare_exchange_strong ( - (int *) &topology->cse_state, MONGOC_CSE_DISABLED, MONGOC_CSE_STARTING, bson_memory_order_acquire); + prev_cse_state = mcommon_atomic_int_compare_exchange_strong ( + (int *) &topology->cse_state, MONGOC_CSE_DISABLED, MONGOC_CSE_STARTING, mcommon_memory_order_acquire); } if (prev_cse_state == MONGOC_CSE_ENABLED) { @@ -1974,7 +1975,7 @@ _mongoc_cse_client_pool_enable_auto_encryption (mongoc_topology_t *topology, if (prev_cse_state == MONGOC_CSE_DISABLED) { /* We need to set the new CSE state. */ mongoc_topology_cse_state_t new_state = setup_okay ? MONGOC_CSE_ENABLED : MONGOC_CSE_DISABLED; - bson_atomic_int_exchange ((int *) &topology->cse_state, new_state, bson_memory_order_release); + mcommon_atomic_int_exchange ((int *) &topology->cse_state, new_state, mcommon_memory_order_release); } mongoc_uri_destroy (mongocryptd_uri); RETURN (setup_okay); @@ -2757,7 +2758,7 @@ _mongoc_cse_is_enabled (mongoc_client_t *client) while (1) { mongoc_topology_cse_state_t state = - bson_atomic_int_fetch ((int *) &client->topology->cse_state, bson_memory_order_relaxed); + mcommon_atomic_int_fetch ((int *) &client->topology->cse_state, mcommon_memory_order_relaxed); if (state != MONGOC_CSE_STARTING) { return state == MONGOC_CSE_ENABLED; } diff --git a/src/libmongoc/src/mongoc/mongoc-client.c b/src/libmongoc/src/mongoc/mongoc-client.c index e7cfa879cd5..e57179cc008 100644 --- a/src/libmongoc/src/mongoc/mongoc-client.c +++ b/src/libmongoc/src/mongoc/mongoc-client.c @@ -71,6 +71,7 @@ #endif #include +#include #include @@ -624,7 +625,7 @@ mongoc_client_connect_tcp (int32_t connecttimeoutms, const mongoc_host_list_t *h // Expect no truncation. int req = bson_snprintf (portstr, sizeof portstr, "%hu", host->port); - BSON_ASSERT (bson_cmp_less_su (req, sizeof portstr)); + BSON_ASSERT (mcommon_cmp_less_su (req, sizeof portstr)); memset (&hints, 0, sizeof hints); hints.ai_family = host->family; @@ -721,7 +722,7 @@ mongoc_client_connect_unix (const mongoc_host_list_t *host, bson_error_t *error) // Expect no truncation. int req = bson_snprintf (saddr.sun_path, sizeof saddr.sun_path - 1, "%s", host->host); - if (bson_cmp_greater_equal_su (req, sizeof saddr.sun_path - 1)) { + if (mcommon_cmp_greater_equal_su (req, sizeof saddr.sun_path - 1)) { bson_set_error (error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_SOCKET, "Failed to define socket address path."); RETURN (NULL); } diff --git a/src/libmongoc/src/mongoc/mongoc-cluster.c b/src/libmongoc/src/mongoc/mongoc-cluster.c index c03f01a2934..59fe6b24aa8 100644 --- a/src/libmongoc/src/mongoc/mongoc-cluster.c +++ b/src/libmongoc/src/mongoc/mongoc-cluster.c @@ -58,6 +58,7 @@ #include "mongoc-error-private.h" #include +#include #include @@ -170,7 +171,7 @@ _mongoc_cluster_buffer_iovec (mongoc_iovec_t *iov, size_t iovcnt, int skip, char size_t difference = 0; for (size_t n = 0u; n < iovcnt; n++) { - BSON_ASSERT (bson_in_range_unsigned (int, iov[n].iov_len)); + BSON_ASSERT (mcommon_in_range_unsigned (int, iov[n].iov_len)); const int iov_len = (int) iov[n].iov_len; total_iov_len += iov_len; @@ -3253,10 +3254,10 @@ _mongoc_cluster_run_opmsg_send ( for (size_t i = 0; i < cmd->payloads_count; i++) { const mongoc_cmd_payload_t payload = cmd->payloads[i]; - BSON_ASSERT (bson_in_range_signed (size_t, payload.size)); + BSON_ASSERT (mcommon_in_range_signed (size_t, payload.size)); const size_t section_length = sizeof (int32_t) + strlen (payload.identifier) + 1u + (size_t) payload.size; - BSON_ASSERT (bson_in_range_unsigned (int32_t, section_length)); + BSON_ASSERT (mcommon_in_range_unsigned (int32_t, section_length)); size_t section_idx = 1u + i; message_length += mcd_rpc_op_msg_section_set_kind (rpc, section_idx, 1); @@ -3515,7 +3516,7 @@ mcd_rpc_message_compress (mcd_rpc_message *rpc, // compressedMessage does not include msgHeader fields. BSON_ASSERT (original_message_length >= message_header_length); const size_t uncompressed_size = (size_t) (original_message_length - message_header_length); - BSON_ASSERT (bson_in_range_unsigned (int32_t, uncompressed_size)); + BSON_ASSERT (mcommon_in_range_unsigned (int32_t, uncompressed_size)); const size_t estimated_compressed_size = mongoc_compressor_max_compressed_length (compressor_id, uncompressed_size); diff --git a/src/libmongoc/src/mongoc/mongoc-compression.c b/src/libmongoc/src/mongoc/mongoc-compression.c index ad6fa9d7e81..b9fff9a44e2 100644 --- a/src/libmongoc/src/mongoc/mongoc-compression.c +++ b/src/libmongoc/src/mongoc/mongoc-compression.c @@ -20,6 +20,7 @@ #include "mongoc-compression-private.h" #include "mongoc-trace-private.h" #include "mongoc-util-private.h" +#include #ifdef MONGOC_ENABLE_COMPRESSION #ifdef MONGOC_ENABLE_COMPRESSION_ZLIB @@ -45,7 +46,7 @@ mongoc_compressor_max_compressed_length (int32_t compressor_id, size_t len) #ifdef MONGOC_ENABLE_COMPRESSION_ZLIB case MONGOC_COMPRESSOR_ZLIB_ID: - BSON_ASSERT (bson_in_range_unsigned (unsigned_long, len)); + BSON_ASSERT (mcommon_in_range_unsigned (unsigned_long, len)); return compressBound ((unsigned long) len); #endif @@ -170,12 +171,12 @@ mongoc_uncompress (int32_t compressor_id, case MONGOC_COMPRESSOR_ZLIB_ID: { #ifdef MONGOC_ENABLE_COMPRESSION_ZLIB // Malformed message: unrepresentable. - if (BSON_UNLIKELY (!bson_in_range_unsigned (unsigned_long, compressed_len))) { + if (BSON_UNLIKELY (!mcommon_in_range_unsigned (unsigned_long, compressed_len))) { return false; } // Malformed message: unrepresentable. - if (BSON_UNLIKELY (!bson_in_range_unsigned (unsigned_long, *uncompressed_len))) { + if (BSON_UNLIKELY (!mcommon_in_range_unsigned (unsigned_long, *uncompressed_len))) { return false; } @@ -253,7 +254,7 @@ mongoc_compress (int32_t compressor_id, case MONGOC_COMPRESSOR_ZLIB_ID: #ifdef MONGOC_ENABLE_COMPRESSION_ZLIB - BSON_ASSERT (bson_in_range_unsigned (unsigned_long, uncompressed_len)); + BSON_ASSERT (mcommon_in_range_unsigned (unsigned_long, uncompressed_len)); return compress2 ((unsigned char *) compressed, (unsigned long *) compressed_len, (unsigned char *) uncompressed, diff --git a/src/libmongoc/src/mongoc/mongoc-counters-private.h b/src/libmongoc/src/mongoc/mongoc-counters-private.h index bcda0ea98ee..e06b9b85ac5 100644 --- a/src/libmongoc/src/mongoc/mongoc-counters-private.h +++ b/src/libmongoc/src/mongoc/mongoc-counters-private.h @@ -20,6 +20,7 @@ #define MONGOC_COUNTERS_PRIVATE_H #include +#include #include "mongoc.h" @@ -155,7 +156,7 @@ enum { int64_t *counter = &BSON_CONCAT (__mongoc_counter_, ident) \ .cpus[_mongoc_sched_getcpu ()] \ .slots[BSON_CONCAT (COUNTER_, ident) % SLOTS_PER_CACHELINE]; \ - bson_atomic_int64_fetch_add (counter, val, bson_memory_order_seq_cst); \ + mcommon_atomic_int64_fetch_add (counter, val, mcommon_memory_order_seq_cst); \ } \ static BSON_INLINE void mongoc_counter_##ident##_inc (void) \ { \ @@ -170,9 +171,9 @@ enum { uint32_t i; \ for (i = 0; i < _mongoc_get_cpu_count (); i++) { \ int64_t *counter = &__mongoc_counter_##ident.cpus[i].slots[COUNTER_##ident % SLOTS_PER_CACHELINE]; \ - bson_atomic_int64_exchange (counter, 0, bson_memory_order_seq_cst); \ + mcommon_atomic_int64_exchange (counter, 0, mcommon_memory_order_seq_cst); \ } \ - bson_atomic_thread_fence (); \ + mcommon_atomic_thread_fence (); \ } \ static BSON_INLINE int32_t mongoc_counter_##ident##_count (void) \ { \ @@ -182,7 +183,7 @@ enum { const int64_t *counter = &BSON_CONCAT (__mongoc_counter_, ident) \ .cpus[_i] \ .slots[BSON_CONCAT (COUNTER_, ident) % SLOTS_PER_CACHELINE]; \ - _sum += bson_atomic_int64_fetch (counter, bson_memory_order_seq_cst); \ + _sum += mcommon_atomic_int64_fetch (counter, mcommon_memory_order_seq_cst); \ } \ return _sum; \ } diff --git a/src/libmongoc/src/mongoc/mongoc-counters.c b/src/libmongoc/src/mongoc/mongoc-counters.c index 0fd9d84cef3..45851d4e688 100644 --- a/src/libmongoc/src/mongoc/mongoc-counters.c +++ b/src/libmongoc/src/mongoc/mongoc-counters.c @@ -33,6 +33,7 @@ #include "mongoc-counters-private.h" #include "mongoc-log.h" +#include #pragma pack(1) @@ -266,7 +267,7 @@ mongoc_counters_register ( bson_strncpy (infos->name, name, sizeof infos->name); bson_strncpy (infos->description, description, sizeof infos->description); - bson_atomic_thread_fence (); + mcommon_atomic_thread_fence (); counters->n_counters++; @@ -316,7 +317,7 @@ _mongoc_counters_init (void) * we have initialized the rest of the counters. Don't forget our memory * barrier to prevent compiler reordering. */ - bson_atomic_thread_fence (); + mcommon_atomic_thread_fence (); counters->size = (uint32_t) size; #endif } diff --git a/src/libmongoc/src/mongoc/mongoc-crypt.c b/src/libmongoc/src/mongoc/mongoc-crypt.c index 3b77d730bc7..6da3e392e9a 100644 --- a/src/libmongoc/src/mongoc/mongoc-crypt.c +++ b/src/libmongoc/src/mongoc/mongoc-crypt.c @@ -34,6 +34,7 @@ #include "mcd-time.h" #include "service-gcp.h" #include +#include // `mcd_mapof_kmsid_to_tlsopts` maps a KMS ID (e.g. `aws` or `aws:myname`) to a // `mongoc_ssl_opt_t`. The acryonym TLS is preferred over SSL for @@ -648,7 +649,7 @@ _state_need_kms (_state_machine_t *state_machine, bson_error_t *error) } mongocrypt_binary_destroy (http_reply); - BSON_ASSERT (bson_in_range_signed (uint32_t, read_ret)); + BSON_ASSERT (mcommon_in_range_signed (uint32_t, read_ret)); http_reply = mongocrypt_binary_new_from_data (buf, (uint32_t) read_ret); if (!mongocrypt_kms_ctx_feed (kms_ctx, http_reply)) { _kms_ctx_check_error (kms_ctx, error, true); diff --git a/src/libmongoc/src/mongoc/mongoc-crypto-cng.c b/src/libmongoc/src/mongoc/mongoc-crypto-cng.c index 781cfac60cf..111ed5d9817 100644 --- a/src/libmongoc/src/mongoc/mongoc-crypto-cng.c +++ b/src/libmongoc/src/mongoc/mongoc-crypto-cng.c @@ -21,6 +21,7 @@ #include "mongoc-crypto-cng-private.h" #include "mongoc-log.h" #include "mongoc-thread-private.h" +#include #include #include @@ -158,19 +159,19 @@ _bcrypt_derive_key_pbkdf2 (BCRYPT_ALG_HANDLE prf, size_t output_len, unsigned char *output) { - if (BSON_UNLIKELY (bson_cmp_greater_uu (password_len, ULONG_MAX))) { + if (BSON_UNLIKELY (mcommon_cmp_greater_uu (password_len, ULONG_MAX))) { MONGOC_ERROR ("PBDKF2 HMAC password length exceeds ULONG_MAX"); return false; } - if (BSON_UNLIKELY (bson_cmp_greater_uu (salt_len, ULONG_MAX))) { + if (BSON_UNLIKELY (mcommon_cmp_greater_uu (salt_len, ULONG_MAX))) { MONGOC_ERROR ("PBDKF2 HMAC salt length exceeds ULONG_MAX"); return false; } // `(ULONGLONG) iterations` is statically asserted above. - if (BSON_UNLIKELY (bson_cmp_greater_uu (output_len, ULONG_MAX))) { + if (BSON_UNLIKELY (mcommon_cmp_greater_uu (output_len, ULONG_MAX))) { MONGOC_ERROR ("PBDKF2 HMAC output length exceeds ULONG_MAX"); return false; } diff --git a/src/libmongoc/src/mongoc/mongoc-crypto-openssl.c b/src/libmongoc/src/mongoc/mongoc-crypto-openssl.c index c5ebedd8181..62a98fa160d 100644 --- a/src/libmongoc/src/mongoc/mongoc-crypto-openssl.c +++ b/src/libmongoc/src/mongoc/mongoc-crypto-openssl.c @@ -21,6 +21,7 @@ #include "mongoc-crypto-openssl-private.h" #include "mongoc-crypto-private.h" #include "mongoc-log.h" +#include #include #include @@ -38,22 +39,22 @@ mongoc_crypto_openssl_pbkdf2_hmac_sha1 (mongoc_crypto_t *crypto, { BSON_UNUSED (crypto); - if (BSON_UNLIKELY (bson_cmp_greater_us (password_len, INT_MAX))) { + if (BSON_UNLIKELY (mcommon_cmp_greater_us (password_len, INT_MAX))) { MONGOC_ERROR ("PBKDF2 HMAC password length exceeds INT_MAX"); return false; } - if (BSON_UNLIKELY (bson_cmp_greater_us (salt_len, INT_MAX))) { + if (BSON_UNLIKELY (mcommon_cmp_greater_us (salt_len, INT_MAX))) { MONGOC_ERROR ("PBKDF2 HMAC salt length exceeds INT_MAX"); return false; } - if (BSON_UNLIKELY (bson_cmp_greater_us (iterations, INT_MAX))) { + if (BSON_UNLIKELY (mcommon_cmp_greater_us (iterations, INT_MAX))) { MONGOC_ERROR ("PBKDF2 HMAC iteration count exceeds INT_MAX"); return false; } - if (BSON_UNLIKELY (bson_cmp_greater_us (iterations, INT_MAX))) { + if (BSON_UNLIKELY (mcommon_cmp_greater_us (iterations, INT_MAX))) { MONGOC_ERROR ("PBKDF2 HMAC output buffer length exceeds INT_MAX"); return false; } @@ -136,22 +137,22 @@ mongoc_crypto_openssl_pbkdf2_hmac_sha256 (mongoc_crypto_t *crypto, { BSON_UNUSED (crypto); - if (BSON_UNLIKELY (bson_cmp_greater_us (password_len, INT_MAX))) { + if (BSON_UNLIKELY (mcommon_cmp_greater_us (password_len, INT_MAX))) { MONGOC_ERROR ("PBKDF2 HMAC password length exceeds INT_MAX"); return false; } - if (BSON_UNLIKELY (bson_cmp_greater_us (salt_len, INT_MAX))) { + if (BSON_UNLIKELY (mcommon_cmp_greater_us (salt_len, INT_MAX))) { MONGOC_ERROR ("PBKDF2 HMAC salt length exceeds INT_MAX"); return false; } - if (BSON_UNLIKELY (bson_cmp_greater_us (iterations, INT_MAX))) { + if (BSON_UNLIKELY (mcommon_cmp_greater_us (iterations, INT_MAX))) { MONGOC_ERROR ("PBKDF2 HMAC iteration count exceeds INT_MAX"); return false; } - if (BSON_UNLIKELY (bson_cmp_greater_us (iterations, INT_MAX))) { + if (BSON_UNLIKELY (mcommon_cmp_greater_us (iterations, INT_MAX))) { MONGOC_ERROR ("PBKDF2 HMAC output buffer length exceeds INT_MAX"); return false; } diff --git a/src/libmongoc/src/mongoc/mongoc-cursor.c b/src/libmongoc/src/mongoc/mongoc-cursor.c index 99d157d37ee..2a26513ecc2 100644 --- a/src/libmongoc/src/mongoc/mongoc-cursor.c +++ b/src/libmongoc/src/mongoc/mongoc-cursor.c @@ -31,6 +31,7 @@ #include "mongoc-aggregate-private.h" #include +#include #undef MONGOC_LOG_DOMAIN #define MONGOC_LOG_DOMAIN "cursor" @@ -1369,7 +1370,7 @@ mongoc_cursor_set_batch_size (mongoc_cursor_t *cursor, uint32_t batch_size) } else if (BSON_ITER_HOLDS_INT64 (&iter)) { bson_iter_overwrite_int64 (&iter, (int64_t) batch_size); } else if (BSON_ITER_HOLDS_INT32 (&iter)) { - if (!bson_in_range_int32_t_unsigned (batch_size)) { + if (!mcommon_in_range_int32_t_unsigned (batch_size)) { MONGOC_WARNING ("unable to overwrite stored int32 batchSize with " "out-of-range value %" PRIu32, batch_size); diff --git a/src/libmongoc/src/mongoc/mongoc-cyrus.c b/src/libmongoc/src/mongoc/mongoc-cyrus.c index e8af62a44a0..dbb1d19b769 100644 --- a/src/libmongoc/src/mongoc/mongoc-cyrus.c +++ b/src/libmongoc/src/mongoc/mongoc-cyrus.c @@ -26,6 +26,7 @@ #include "mongoc-trace-private.h" #include "common-b64-private.h" #include +#include #undef MONGOC_LOG_DOMAIN #define MONGOC_LOG_DOMAIN "CYRUS-SASL" @@ -356,7 +357,7 @@ _mongoc_cyrus_start (mongoc_cyrus_t *sasl, uint8_t **outbuf, uint32_t *outbuflen error, MONGOC_ERROR_SASL, MONGOC_ERROR_CLIENT_AUTHENTICATE, "Unable to base64 encode client SASL message"); return false; } else { - BSON_ASSERT (bson_in_range_signed (uint32_t, b64_ret)); + BSON_ASSERT (mcommon_in_range_signed (uint32_t, b64_ret)); *outbuflen = (uint32_t) b64_ret; } @@ -446,7 +447,7 @@ _mongoc_cyrus_step (mongoc_cyrus_t *sasl, } else { /* Set the output length to the number of characters written excluding * the NULL. */ - BSON_ASSERT (bson_in_range_signed (uint32_t, b64_ret)); + BSON_ASSERT (mcommon_in_range_signed (uint32_t, b64_ret)); *outbuflen = (uint32_t) b64_ret; } } diff --git a/src/libmongoc/src/mongoc/mongoc-gridfs-bucket-file.c b/src/libmongoc/src/mongoc/mongoc-gridfs-bucket-file.c index 2938e846faa..c998bb37b89 100644 --- a/src/libmongoc/src/mongoc/mongoc-gridfs-bucket-file.c +++ b/src/libmongoc/src/mongoc/mongoc-gridfs-bucket-file.c @@ -22,6 +22,7 @@ #include "mongoc-stream-gridfs-upload-private.h" #include "mongoc-collection-private.h" #include "mongoc-util-private.h" +#include #include @@ -330,7 +331,7 @@ _mongoc_gridfs_bucket_file_writev (mongoc_gridfs_bucket_file_t *file, const mong } } - BSON_ASSERT (bson_in_range_signed (size_t, file->chunk_size)); + BSON_ASSERT (mcommon_in_range_signed (size_t, file->chunk_size)); const size_t chunk_size = (size_t) file->chunk_size; for (size_t i = 0u; i < iovcnt; i++) { @@ -354,7 +355,7 @@ _mongoc_gridfs_bucket_file_writev (mongoc_gridfs_bucket_file_t *file, const mong } } - BSON_ASSERT (bson_in_range_unsigned (ssize_t, total)); + BSON_ASSERT (mcommon_in_range_unsigned (ssize_t, total)); return (ssize_t) total; } @@ -398,14 +399,14 @@ _mongoc_gridfs_bucket_file_readv (mongoc_gridfs_bucket_file_t *file, mongoc_iove } if (file->finished) { /* There's nothing left to read */ - BSON_ASSERT (bson_in_range_unsigned (ssize_t, total)); + BSON_ASSERT (mcommon_in_range_unsigned (ssize_t, total)); RETURN ((ssize_t) total); } } } } - BSON_ASSERT (bson_in_range_unsigned (ssize_t, total)); + BSON_ASSERT (mcommon_in_range_unsigned (ssize_t, total)); RETURN ((ssize_t) total); } diff --git a/src/libmongoc/src/mongoc/mongoc-gridfs-file.c b/src/libmongoc/src/mongoc/mongoc-gridfs-file.c index a6b6d8433d7..92403c6e5e9 100644 --- a/src/libmongoc/src/mongoc/mongoc-gridfs-file.c +++ b/src/libmongoc/src/mongoc/mongoc-gridfs-file.c @@ -35,6 +35,7 @@ #include "mongoc-trace-private.h" #include "mongoc-util-private.h" #include "mongoc-error.h" +#include static bool _mongoc_gridfs_file_refresh_page (mongoc_gridfs_file_t *file); @@ -434,7 +435,7 @@ mongoc_gridfs_file_readv ( BSON_ASSERT (iovcnt); /* Reading when positioned past the end does nothing */ - if (bson_cmp_greater_equal_us (file->pos, file->length)) { + if (mcommon_cmp_greater_equal_us (file->pos, file->length)) { return 0; } @@ -498,7 +499,7 @@ mongoc_gridfs_file_writev (mongoc_gridfs_file_t *file, const mongoc_iovec_t *iov } /* When writing past the end-of-file, fill the gap with zeros */ - if (bson_cmp_greater_us (file->pos, file->length) && !_mongoc_gridfs_file_extend (file)) { + if (mcommon_cmp_greater_us (file->pos, file->length) && !_mongoc_gridfs_file_extend (file)) { return -1; } @@ -563,13 +564,13 @@ _mongoc_gridfs_file_extend (mongoc_gridfs_file_t *file) BSON_ASSERT (file); - if (bson_cmp_greater_equal_su (file->length, file->pos)) { + if (mcommon_cmp_greater_equal_su (file->length, file->pos)) { RETURN (0); } const uint64_t target_length = file->pos; - BSON_ASSERT (bson_in_range_signed (uint64_t, file->length)); + BSON_ASSERT (mcommon_in_range_signed (uint64_t, file->length)); const uint64_t diff = file->pos - (uint64_t) file->length; if (-1 == mongoc_gridfs_file_seek (file, 0, SEEK_END)) { @@ -584,7 +585,7 @@ _mongoc_gridfs_file_extend (mongoc_gridfs_file_t *file) /* Set bytes until we reach the limit or fill a page */ { const uint64_t len = target_length - file->pos; - BSON_ASSERT (bson_in_range_unsigned (uint32_t, len)); + BSON_ASSERT (mcommon_in_range_unsigned (uint32_t, len)); file->pos += _mongoc_gridfs_file_page_memset0 (file->page, (uint32_t) len); } @@ -597,11 +598,11 @@ _mongoc_gridfs_file_extend (mongoc_gridfs_file_t *file) } } - BSON_ASSERT (bson_in_range_unsigned (int64_t, target_length)); + BSON_ASSERT (mcommon_in_range_unsigned (int64_t, target_length)); file->length = (int64_t) target_length; file->is_dirty = true; - BSON_ASSERT (bson_in_range_unsigned (ssize_t, diff)); + BSON_ASSERT (mcommon_in_range_unsigned (ssize_t, diff)); RETURN ((ssize_t) diff); } @@ -811,7 +812,7 @@ _mongoc_gridfs_file_refresh_page (mongoc_gridfs_file_t *file) /* we might have had a cursor before, then seeked ahead past a chunk. * iterate until we're on the right chunk */ - while (bson_cmp_less_equal_us (file->cursor_range[0], file->n)) { + while (mcommon_cmp_less_equal_us (file->cursor_range[0], file->n)) { if (!mongoc_cursor_next (file->cursor, &chunk)) { /* copy cursor error; if there's none, we're missing a chunk */ if (!mongoc_cursor_error (file->cursor, &file->error)) { @@ -841,7 +842,7 @@ _mongoc_gridfs_file_refresh_page (mongoc_gridfs_file_t *file) // If this not the last chunk, ensure length is equal to chunk size. bool is_last_chunk = ((file->n + 1) == existing_chunks); // If this is not the last chunk, error. - if (!is_last_chunk && bson_cmp_not_equal_us (len, file->chunk_size)) { + if (!is_last_chunk && mcommon_cmp_not_equal_us (len, file->chunk_size)) { bson_set_error (&file->error, MONGOC_ERROR_GRIDFS, MONGOC_ERROR_GRIDFS_CORRUPT, @@ -870,7 +871,7 @@ _mongoc_gridfs_file_refresh_page (mongoc_gridfs_file_t *file) RETURN (0); } - if (bson_cmp_greater_us (len, file->chunk_size)) { + if (mcommon_cmp_greater_us (len, file->chunk_size)) { bson_set_error (&file->error, MONGOC_ERROR_GRIDFS, MONGOC_ERROR_GRIDFS_CORRUPT, @@ -930,7 +931,7 @@ mongoc_gridfs_file_seek (mongoc_gridfs_file_t *file, int64_t delta, int whence) offset = delta; break; case SEEK_CUR: - BSON_ASSERT (bson_in_range_unsigned (int64_t, file->pos)); + BSON_ASSERT (mcommon_in_range_unsigned (int64_t, file->pos)); offset = (int64_t) file->pos + delta; break; case SEEK_END: @@ -964,15 +965,15 @@ mongoc_gridfs_file_seek (mongoc_gridfs_file_t *file, int64_t delta, int whence) * lazily load */ } else if (file->page) { const int64_t n = offset % file->chunk_size; - BSON_ASSERT (bson_in_range_signed (uint32_t, n)); + BSON_ASSERT (mcommon_in_range_signed (uint32_t, n)); BSON_ASSERT (_mongoc_gridfs_file_page_seek (file->page, (uint32_t) n)); } file->pos = (uint64_t) offset; - BSON_ASSERT (bson_in_range_signed (uint64_t, file->chunk_size)); + BSON_ASSERT (mcommon_in_range_signed (uint64_t, file->chunk_size)); const uint64_t n = file->pos / (uint64_t) file->chunk_size; - BSON_ASSERT (bson_in_range_unsigned (int32_t, n)); + BSON_ASSERT (mcommon_in_range_unsigned (int32_t, n)); file->n = (int32_t) n; return 0; diff --git a/src/libmongoc/src/mongoc/mongoc-handshake.c b/src/libmongoc/src/mongoc/mongoc-handshake.c index 3f591f6e65e..eb41913c483 100644 --- a/src/libmongoc/src/mongoc/mongoc-handshake.c +++ b/src/libmongoc/src/mongoc/mongoc-handshake.c @@ -38,6 +38,7 @@ #include #include +#include /* * Global handshake data instance. Initialized at startup from mongoc_init @@ -437,7 +438,7 @@ _get_env_info (mongoc_handshake_t *handshake) char *endptr; int64_t env_memory_mb = bson_ascii_strtoll (memory_str, &endptr, 10); bool parse_ok = endptr == memory_str + (strlen (memory_str)); - bool in_range = bson_in_range_int32_t_signed (env_memory_mb); + bool in_range = mcommon_in_range_int32_t_signed (env_memory_mb); if (parse_ok && in_range) { handshake->env_memory_mb.set = true; @@ -448,7 +449,7 @@ _get_env_info (mongoc_handshake_t *handshake) char *endptr; int64_t env_timeout_sec = bson_ascii_strtoll (timeout_str, &endptr, 10); bool parse_ok = endptr == timeout_str + (strlen (timeout_str)); - bool in_range = bson_in_range_int32_t_signed (env_timeout_sec); + bool in_range = mcommon_in_range_int32_t_signed (env_timeout_sec); if (parse_ok && in_range) { handshake->env_timeout_sec.set = true; @@ -579,16 +580,17 @@ _append_platform_field (bson_t *doc, const char *platform, bool truncate) * Try to drop flags first, and if there is still not enough space also * drop compiler info */ if (!truncate || - bson_cmp_greater_equal_su (max_platform_str_size, combined_platform->len + strlen (compiler_info) + 1u)) { + mcommon_cmp_greater_equal_su (max_platform_str_size, combined_platform->len + strlen (compiler_info) + 1u)) { mcd_string_append (combined_platform, compiler_info); } - if (!truncate || bson_cmp_greater_equal_su (max_platform_str_size, combined_platform->len + strlen (flags) + 1u)) { + if (!truncate || + mcommon_cmp_greater_equal_su (max_platform_str_size, combined_platform->len + strlen (flags) + 1u)) { mcd_string_append (combined_platform, flags); } /* We use the flags_index field to check if the CLAGS/LDFLAGS need to be * truncated, and if so we drop them altogether */ - BSON_ASSERT (bson_in_range_unsigned (int, combined_platform->len)); + BSON_ASSERT (mcommon_in_range_unsigned (int, combined_platform->len)); int length = truncate ? BSON_MIN (max_platform_str_size - 1, (int) combined_platform->len) : -1; bson_append_utf8 (doc, HANDSHAKE_PLATFORM_FIELD, -1, combined_platform->str, length); @@ -761,7 +763,7 @@ _append_and_truncate (char **s, const char *suffix, size_t max_len) } const size_t space_for_suffix = max_len - required_space; - BSON_ASSERT (bson_in_range_unsigned (int, space_for_suffix)); + BSON_ASSERT (mcommon_in_range_unsigned (int, space_for_suffix)); *s = bson_strdup_printf ("%s / %.*s", prefix, (int) space_for_suffix, suffix); BSON_ASSERT (strlen (*s) <= max_len); diff --git a/src/libmongoc/src/mongoc/mongoc-host-list.c b/src/libmongoc/src/mongoc/mongoc-host-list.c index 5033f6e993c..c090bcf7362 100644 --- a/src/libmongoc/src/mongoc/mongoc-host-list.c +++ b/src/libmongoc/src/mongoc/mongoc-host-list.c @@ -20,6 +20,7 @@ /* strcasecmp on windows */ #include "mongoc-util-private.h" #include "utlist.h" +#include static mongoc_host_list_t * _mongoc_host_list_find_host_and_port (mongoc_host_list_t *hosts, const char *host_and_port) @@ -314,7 +315,7 @@ _mongoc_host_list_from_hostport_with_err (mongoc_host_list_t *link_, mongoc_lowercase (link_->host, link_->host); int req = bson_snprintf (link_->host_and_port, sizeof link_->host_and_port, "[%s]:%" PRIu16, link_->host, link_->port); - BSON_ASSERT (bson_in_range_size_t_signed (req)); + BSON_ASSERT (mcommon_in_range_size_t_signed (req)); // Use `<`, not `<=` to account for NULL byte. BSON_ASSERT ((size_t) req < sizeof link_->host_and_port); } else if (strchr (host, '/') && strstr (host, ".sock")) { @@ -327,7 +328,7 @@ _mongoc_host_list_from_hostport_with_err (mongoc_host_list_t *link_, mongoc_lowercase (link_->host, link_->host); int req = bson_snprintf (link_->host_and_port, sizeof link_->host_and_port, "%s:%" PRIu16, link_->host, link_->port); - BSON_ASSERT (bson_in_range_size_t_signed (req)); + BSON_ASSERT (mcommon_in_range_size_t_signed (req)); // Use `<`, not `<=` to account for NULL byte. BSON_ASSERT ((size_t) req < sizeof link_->host_and_port); } diff --git a/src/libmongoc/src/mongoc/mongoc-http.c b/src/libmongoc/src/mongoc/mongoc-http.c index c5bd5735777..aae728c59f0 100644 --- a/src/libmongoc/src/mongoc/mongoc-http.c +++ b/src/libmongoc/src/mongoc/mongoc-http.c @@ -23,6 +23,7 @@ #include "mongoc-buffer-private.h" #include "mcd-time.h" #include +#include void _mongoc_http_request_init (mongoc_http_request_t *request) @@ -92,7 +93,7 @@ static int32_t _mongoc_http_msec_remaining (mcd_timer timer) { const int64_t msec = mcd_get_milliseconds (mcd_timer_remaining (timer)); - BSON_ASSERT (bson_in_range_signed (int32_t, msec)); + BSON_ASSERT (mcommon_in_range_signed (int32_t, msec)); return (int32_t) msec; } @@ -266,10 +267,10 @@ _mongoc_http_send (const mongoc_http_request_t *req, } const size_t headers_len = (size_t) (ptr - http_response_str); - BSON_ASSERT (bson_in_range_unsigned (int, headers_len)); + BSON_ASSERT (mcommon_in_range_unsigned (int, headers_len)); const size_t body_len = http_response_buf.len - headers_len - strlen (header_delimiter); - BSON_ASSERT (bson_in_range_unsigned (int, body_len)); + BSON_ASSERT (mcommon_in_range_unsigned (int, body_len)); res->headers_len = (int) headers_len; res->headers = bson_strndup (http_response_str, (size_t) headers_len); diff --git a/src/libmongoc/src/mongoc/mongoc-opts-helpers.c b/src/libmongoc/src/mongoc/mongoc-opts-helpers.c index b2252895a6d..89851dad1e2 100644 --- a/src/libmongoc/src/mongoc/mongoc-opts-helpers.c +++ b/src/libmongoc/src/mongoc/mongoc-opts-helpers.c @@ -19,6 +19,7 @@ #include "mongoc-write-concern-private.h" #include "mongoc-util-private.h" #include "mongoc-read-concern-private.h" +#include #define BSON_ERR(...) \ do { \ @@ -57,7 +58,7 @@ void _mongoc_timestamp_append (mongoc_timestamp_t *timestamp, bson_t *bson, char *key) { const size_t len = strlen (key); - BSON_ASSERT (bson_in_range_unsigned (int, len)); + BSON_ASSERT (mcommon_in_range_unsigned (int, len)); bson_append_timestamp (bson, key, (int) len, timestamp->timestamp, timestamp->increment); } diff --git a/src/libmongoc/src/mongoc/mongoc-read-prefs.c b/src/libmongoc/src/mongoc/mongoc-read-prefs.c index d2c39013b60..f4e5a3efcee 100644 --- a/src/libmongoc/src/mongoc/mongoc-read-prefs.c +++ b/src/libmongoc/src/mongoc/mongoc-read-prefs.c @@ -18,6 +18,7 @@ #include "mongoc-error.h" #include "mongoc-read-prefs-private.h" #include "mongoc-trace-private.h" +#include mongoc_read_prefs_t * @@ -87,7 +88,7 @@ mongoc_read_prefs_add_tag (mongoc_read_prefs_t *read_prefs, const bson_t *tag) key = bson_count_keys (&read_prefs->tags); // Expect no truncation. int req = bson_snprintf (str, sizeof str, "%d", key); - BSON_ASSERT (bson_cmp_less_su (req, sizeof str)); + BSON_ASSERT (mcommon_cmp_less_su (req, sizeof str)); if (tag) { bson_append_document (&read_prefs->tags, str, -1, tag); diff --git a/src/libmongoc/src/mongoc/mongoc-scram.c b/src/libmongoc/src/mongoc/mongoc-scram.c index cd300e74a89..2182b9d8903 100644 --- a/src/libmongoc/src/mongoc/mongoc-scram.c +++ b/src/libmongoc/src/mongoc/mongoc-scram.c @@ -31,6 +31,7 @@ #include "mongoc-memcmp-private.h" #include "common-thread-private.h" #include +#include typedef struct _mongoc_scram_cache_entry_t { /* book keeping */ @@ -663,7 +664,7 @@ _mongoc_scram_step2 (mongoc_scram_t *scram, } /* verify our nonce */ - if (bson_cmp_less_us (val_r_len, scram->encoded_nonce_len) || + if (mcommon_cmp_less_us (val_r_len, scram->encoded_nonce_len) || mongoc_memcmp (val_r, scram->encoded_nonce, scram->encoded_nonce_len)) { bson_set_error (error, MONGOC_ERROR_SCRAM, @@ -807,7 +808,7 @@ _mongoc_scram_verify_server_signature (mongoc_scram_t *scram, uint8_t *verificat if (!*scram->server_key) { const size_t key_len = strlen (MONGOC_SCRAM_SERVER_KEY); - BSON_ASSERT (bson_in_range_unsigned (int, key_len)); + BSON_ASSERT (mcommon_in_range_unsigned (int, key_len)); /* ServerKey := HMAC(SaltedPassword, "Server Key") */ mongoc_crypto_hmac (&scram->crypto, @@ -1022,7 +1023,7 @@ _mongoc_sasl_prep_impl (const char *name, const char *in_utf8, bson_error_t *err } /* convert to unicode. */ - BSON_ASSERT (bson_cmp_less_equal_su (num_chars, SIZE_MAX / sizeof (uint32_t) - 1u)); + BSON_ASSERT (mcommon_cmp_less_equal_su (num_chars, SIZE_MAX / sizeof (uint32_t) - 1u)); utf8_codepoints = bson_malloc (sizeof (uint32_t) * ((size_t) num_chars + 1u)); /* add one for trailing 0 value. */ const char *c = in_utf8; diff --git a/src/libmongoc/src/mongoc/mongoc-secure-channel.c b/src/libmongoc/src/mongoc/mongoc-secure-channel.c index 6f0e0f888df..a150281aab3 100644 --- a/src/libmongoc/src/mongoc/mongoc-secure-channel.c +++ b/src/libmongoc/src/mongoc/mongoc-secure-channel.c @@ -30,6 +30,7 @@ #include "mongoc-errno-private.h" #include "mongoc-error.h" #include +#include #undef MONGOC_LOG_DOMAIN @@ -424,7 +425,7 @@ mongoc_secure_channel_read (mongoc_stream_tls_t *tls, void *data, size_t data_le { BSON_ASSERT_PARAM (tls); - if (BSON_UNLIKELY (!bson_in_range_signed (int32_t, tls->timeout_msec))) { + if (BSON_UNLIKELY (!mcommon_in_range_signed (int32_t, tls->timeout_msec))) { // CDRIVER-4589 MONGOC_ERROR ("timeout_msec value %" PRId64 " exceeds supported 32-bit range", tls->timeout_msec); return -1; @@ -452,7 +453,7 @@ mongoc_secure_channel_write (mongoc_stream_tls_t *tls, const void *data, size_t { BSON_ASSERT_PARAM (tls); - if (BSON_UNLIKELY (!bson_in_range_signed (int32_t, tls->timeout_msec))) { + if (BSON_UNLIKELY (!mcommon_in_range_signed (int32_t, tls->timeout_msec))) { // CDRIVER-4589 MONGOC_ERROR ("timeout_msec value %" PRId64 " exceeds supported 32-bit range", tls->timeout_msec); return -1; diff --git a/src/libmongoc/src/mongoc/mongoc-server-description.c b/src/libmongoc/src/mongoc/mongoc-server-description.c index 5390f473ba3..c4217fd1a9e 100644 --- a/src/libmongoc/src/mongoc/mongoc-server-description.c +++ b/src/libmongoc/src/mongoc/mongoc-server-description.c @@ -26,6 +26,7 @@ #include "mongoc-compression-private.h" #include +#include #include @@ -487,11 +488,11 @@ mongoc_server_description_update_rtt (mongoc_server_description_t *server, int64 return; } if (server->round_trip_time_msec == MONGOC_RTT_UNSET) { - bson_atomic_int64_exchange (&server->round_trip_time_msec, rtt_msec, bson_memory_order_relaxed); + mcommon_atomic_int64_exchange (&server->round_trip_time_msec, rtt_msec, mcommon_memory_order_relaxed); } else { - bson_atomic_int64_exchange (&server->round_trip_time_msec, - (int64_t) (ALPHA * rtt_msec + (1 - ALPHA) * server->round_trip_time_msec), - bson_memory_order_relaxed); + mcommon_atomic_int64_exchange (&server->round_trip_time_msec, + (int64_t) (ALPHA * rtt_msec + (1 - ALPHA) * server->round_trip_time_msec), + mcommon_memory_order_relaxed); } } @@ -796,7 +797,8 @@ mongoc_server_description_new_copy (const mongoc_server_description_t *descripti if (description->has_hello_response) { /* calls mongoc_server_description_reset */ - int64_t last_rtt_ms = bson_atomic_int64_fetch (&description->round_trip_time_msec, bson_memory_order_relaxed); + int64_t last_rtt_ms = + mcommon_atomic_int64_fetch (&description->round_trip_time_msec, mcommon_memory_order_relaxed); mongoc_server_description_handle_hello ( copy, &description->last_hello_response, last_rtt_ms, &description->error); } else { diff --git a/src/libmongoc/src/mongoc/mongoc-server-monitor.c b/src/libmongoc/src/mongoc/mongoc-server-monitor.c index 364dedbee69..f86240d9a2b 100644 --- a/src/libmongoc/src/mongoc/mongoc-server-monitor.c +++ b/src/libmongoc/src/mongoc/mongoc-server-monitor.c @@ -26,6 +26,7 @@ #include "mongoc/mongoc-topology-background-monitoring-private.h" #include "mongoc/mongoc-topology-private.h" #include "mongoc/mongoc-trace-private.h" +#include #include @@ -774,7 +775,7 @@ _update_topology_description (mongoc_server_monitor_t *server_monitor, mongoc_se _mongoc_topology_update_cluster_time (topology, hello_response); } - if (bson_atomic_int_fetch (&topology->scanner_state, bson_memory_order_relaxed) == + if (mcommon_atomic_int_fetch (&topology->scanner_state, mcommon_memory_order_relaxed) == MONGOC_TOPOLOGY_SCANNER_SHUTTING_DOWN) { return; } diff --git a/src/libmongoc/src/mongoc/mongoc-set.c b/src/libmongoc/src/mongoc/mongoc-set.c index 4ae40f8cfd5..6f7f27a5049 100644 --- a/src/libmongoc/src/mongoc/mongoc-set.c +++ b/src/libmongoc/src/mongoc/mongoc-set.c @@ -18,6 +18,7 @@ #include #include "mongoc-set-private.h" +#include #undef MONGOC_LOG_DOMAIN #define MONGOC_LOG_DOMAIN "set" @@ -201,7 +202,7 @@ mongoc_set_for_each_with_id (mongoc_set_t *set, mongoc_set_for_each_with_id_cb_t BSON_ASSERT_PARAM (cb); BSON_ASSERT (ctx || true); - BSON_ASSERT (bson_in_range_unsigned (uint32_t, set->items_len)); + BSON_ASSERT (mcommon_in_range_unsigned (uint32_t, set->items_len)); const uint32_t items_len = (uint32_t) set->items_len; /* prevent undefined behavior of memcpy(NULL) */ @@ -230,7 +231,7 @@ mongoc_set_for_each_with_id_const (const mongoc_set_t *set, mongoc_set_for_each_ BSON_ASSERT_PARAM (cb); BSON_ASSERT (ctx || true); - BSON_ASSERT (bson_in_range_unsigned (uint32_t, set->items_len)); + BSON_ASSERT (mcommon_in_range_unsigned (uint32_t, set->items_len)); const uint32_t items_len = (uint32_t) set->items_len; /* prevent undefined behavior of memcpy(NULL) */ diff --git a/src/libmongoc/src/mongoc/mongoc-shared.c b/src/libmongoc/src/mongoc/mongoc-shared.c index 4695f5beb45..3017c67653a 100644 --- a/src/libmongoc/src/mongoc/mongoc-shared.c +++ b/src/libmongoc/src/mongoc/mongoc-shared.c @@ -18,6 +18,7 @@ #include "common-thread-private.h" #include +#include typedef struct _mongoc_shared_ptr_aux { int refcount; @@ -115,7 +116,7 @@ mongoc_shared_ptr_copy (mongoc_shared_ptr ptr) { mongoc_shared_ptr ret = ptr; if (!mongoc_shared_ptr_is_null (ptr)) { - bson_atomic_int_fetch_add (&ret._aux->refcount, 1, bson_memory_order_acquire); + mcommon_atomic_int_fetch_add (&ret._aux->refcount, 1, mcommon_memory_order_acquire); } return ret; } @@ -130,7 +131,7 @@ mongoc_shared_ptr_reset_null (mongoc_shared_ptr *ptr) return; } /* Decrement the reference count by one */ - prevcount = bson_atomic_int_fetch_sub (&ptr->_aux->refcount, 1, bson_memory_order_acq_rel); + prevcount = mcommon_atomic_int_fetch_sub (&ptr->_aux->refcount, 1, mcommon_memory_order_acq_rel); if (prevcount == 1) { /* We just decremented from one to zero, so this is the last instance. * Release the managed data. */ @@ -144,5 +145,5 @@ int mongoc_shared_ptr_use_count (mongoc_shared_ptr ptr) { BSON_ASSERT (!mongoc_shared_ptr_is_null (ptr) && "Unbound mongoc_shared_ptr given to mongoc_shared_ptr_use_count"); - return bson_atomic_int_fetch (&ptr._aux->refcount, bson_memory_order_relaxed); + return mcommon_atomic_int_fetch (&ptr._aux->refcount, mcommon_memory_order_relaxed); } diff --git a/src/libmongoc/src/mongoc/mongoc-socket.c b/src/libmongoc/src/mongoc/mongoc-socket.c index 9e6a388a03d..e417d7db64d 100644 --- a/src/libmongoc/src/mongoc/mongoc-socket.c +++ b/src/libmongoc/src/mongoc/mongoc-socket.c @@ -28,6 +28,7 @@ #include #include #endif +#include #include @@ -1191,7 +1192,7 @@ _mongoc_socket_try_sendv_slow (mongoc_socket_t *sock, /* IN */ for (size_t i = 0u; i < iovcnt; i++) { #ifdef _WIN32 - BSON_ASSERT (bson_in_range_unsigned (int, iov[i].iov_len)); + BSON_ASSERT (mcommon_in_range_unsigned (int, iov[i].iov_len)); const int wrote = send (sock->sd, iov[i].iov_base, (int) iov[i].iov_len, 0); if (wrote == SOCKET_ERROR) { #else @@ -1208,7 +1209,7 @@ _mongoc_socket_try_sendv_slow (mongoc_socket_t *sock, /* IN */ ret += wrote; - if (bson_cmp_not_equal_su (wrote, iov[i].iov_len)) { + if (mcommon_cmp_not_equal_su (wrote, iov[i].iov_len)) { RETURN (ret); } } @@ -1258,7 +1259,7 @@ _mongoc_socket_try_sendv (mongoc_socket_t *sock, /* IN */ DUMP_IOVEC (sendbuf, iov, iovcnt); #ifdef _WIN32 - BSON_ASSERT (bson_in_range_unsigned (unsigned_long, iovcnt)); + BSON_ASSERT (mcommon_in_range_unsigned (unsigned_long, iovcnt)); ret = WSASend (sock->sd, (LPWSABUF) iov, (DWORD) iovcnt, &dwNumberofBytesSent, 0, NULL, NULL); TRACE ("WSASend sent: %lu (out of: %zu), ret: %d", dwNumberofBytesSent, iov->iov_len, ret); #else diff --git a/src/libmongoc/src/mongoc/mongoc-stream-file.c b/src/libmongoc/src/mongoc/mongoc-stream-file.c index c91b5457f83..f348be5c43a 100644 --- a/src/libmongoc/src/mongoc/mongoc-stream-file.c +++ b/src/libmongoc/src/mongoc/mongoc-stream-file.c @@ -24,6 +24,7 @@ #include "mongoc-stream-file.h" #include "mongoc-trace-private.h" #include "mongoc-counters-private.h" +#include /* * TODO: This does not respect timeouts or set O_NONBLOCK. @@ -131,7 +132,7 @@ _mongoc_stream_file_readv (mongoc_stream_t *stream, /* IN */ ENTRY; for (size_t i = 0u; i < iovcnt; i++) { - BSON_ASSERT (bson_in_range_unsigned (unsigned_int, iov[i].iov_len)); + BSON_ASSERT (mcommon_in_range_unsigned (unsigned_int, iov[i].iov_len)); const int nread = _read (file->fd, iov[i].iov_base, (unsigned int) iov[i].iov_len); if (nread < 0) { ret = ret ? ret : -1; @@ -153,7 +154,7 @@ _mongoc_stream_file_readv (mongoc_stream_t *stream, /* IN */ #else { ENTRY; - BSON_ASSERT (bson_in_range_unsigned (int, iovcnt)); + BSON_ASSERT (mcommon_in_range_unsigned (int, iovcnt)); ret = readv (file->fd, iov, (int) iovcnt); GOTO (done); } @@ -180,9 +181,9 @@ _mongoc_stream_file_writev (mongoc_stream_t *stream, /* IN */ #ifdef _WIN32 { for (size_t i = 0; i < iovcnt; i++) { - BSON_ASSERT (bson_in_range_unsigned (unsigned_int, iov[i].iov_len)); + BSON_ASSERT (mcommon_in_range_unsigned (unsigned_int, iov[i].iov_len)); const int nwrite = _write (file->fd, iov[i].iov_base, (unsigned int) iov[i].iov_len); - if (bson_cmp_not_equal_su (nwrite, iov[i].iov_len)) { + if (mcommon_cmp_not_equal_su (nwrite, iov[i].iov_len)) { ret = ret ? ret : -1; goto done; } @@ -192,7 +193,7 @@ _mongoc_stream_file_writev (mongoc_stream_t *stream, /* IN */ } #else { - BSON_ASSERT (bson_in_range_unsigned (int, iovcnt)); + BSON_ASSERT (mcommon_in_range_unsigned (int, iovcnt)); ret = writev (file->fd, iov, (int) iovcnt); goto done; } diff --git a/src/libmongoc/src/mongoc/mongoc-stream-tls-openssl-bio.c b/src/libmongoc/src/mongoc/mongoc-stream-tls-openssl-bio.c index 23615352336..82d01296377 100644 --- a/src/libmongoc/src/mongoc/mongoc-stream-tls-openssl-bio.c +++ b/src/libmongoc/src/mongoc/mongoc-stream-tls-openssl-bio.c @@ -33,6 +33,7 @@ #include "mongoc-openssl-private.h" #include "mongoc-trace-private.h" #include "mongoc-log.h" +#include #undef MONGOC_LOG_DOMAIN @@ -214,7 +215,7 @@ mongoc_stream_tls_openssl_bio_read (BIO *b, char *buf, int len) RETURN (-1); } - if (BSON_UNLIKELY (!bson_in_range_signed (int32_t, tls->timeout_msec))) { + if (BSON_UNLIKELY (!mcommon_in_range_signed (int32_t, tls->timeout_msec))) { // CDRIVER-4589 MONGOC_ERROR ("timeout_msec value %" PRId64 " exceeds supported 32-bit range", tls->timeout_msec); return -1; @@ -234,7 +235,7 @@ mongoc_stream_tls_openssl_bio_read (BIO *b, char *buf, int len) BIO_set_retry_read (openssl->bio); } - BSON_ASSERT (bson_in_range_signed (int, ret)); + BSON_ASSERT (mcommon_in_range_signed (int, ret)); RETURN ((int) ret); } @@ -282,7 +283,7 @@ mongoc_stream_tls_openssl_bio_write (BIO *b, const char *buf, int len) iov.iov_base = (void *) buf; iov.iov_len = (size_t) len; - if (BSON_UNLIKELY (!bson_in_range_signed (int32_t, tls->timeout_msec))) { + if (BSON_UNLIKELY (!mcommon_in_range_signed (int32_t, tls->timeout_msec))) { // CDRIVER-4589 MONGOC_ERROR ("timeout_msec value %" PRId64 " exceeds supported 32-bit range", tls->timeout_msec); RETURN (-1); @@ -307,7 +308,7 @@ mongoc_stream_tls_openssl_bio_write (BIO *b, const char *buf, int len) BIO_set_retry_write (openssl->bio); } - BSON_ASSERT (bson_in_range_signed (int, ret)); + BSON_ASSERT (mcommon_in_range_signed (int, ret)); RETURN ((int) ret); } diff --git a/src/libmongoc/src/mongoc/mongoc-stream-tls-openssl.c b/src/libmongoc/src/mongoc/mongoc-stream-tls-openssl.c index 815e138b97d..61592183e67 100644 --- a/src/libmongoc/src/mongoc/mongoc-stream-tls-openssl.c +++ b/src/libmongoc/src/mongoc/mongoc-stream-tls-openssl.c @@ -42,6 +42,7 @@ #include "mongoc-error.h" #include +#include #include @@ -211,7 +212,7 @@ _mongoc_stream_tls_openssl_write (mongoc_stream_tls_t *tls, char *buf, size_t bu expire = bson_get_monotonic_time () + (tls->timeout_msec * 1000); } - BSON_ASSERT (bson_in_range_unsigned (int, buf_len)); + BSON_ASSERT (mcommon_in_range_unsigned (int, buf_len)); ret = BIO_write (openssl->bio, buf, (int) buf_len); if (ret <= 0) { @@ -222,7 +223,7 @@ _mongoc_stream_tls_openssl_write (mongoc_stream_tls_t *tls, char *buf, size_t bu now = bson_get_monotonic_time (); if ((expire - now) < 0) { - if (bson_cmp_less_su (ret, buf_len)) { + if (mcommon_cmp_less_su (ret, buf_len)) { mongoc_counter_streams_timeout_inc (); } @@ -333,7 +334,7 @@ _mongoc_stream_tls_openssl_writev (mongoc_stream_t *stream, mongoc_iovec_t *iov, * if we didn't buffer and have to send out of the iovec */ child_ret = _mongoc_stream_tls_openssl_write (tls, to_write, to_write_len); - if (bson_cmp_not_equal_su (child_ret, to_write_len)) { + if (mcommon_cmp_not_equal_su (child_ret, to_write_len)) { TRACE ("Got child_ret: %zd while to_write_len is: %zu", child_ret, to_write_len); } @@ -348,7 +349,7 @@ _mongoc_stream_tls_openssl_writev (mongoc_stream_t *stream, mongoc_iovec_t *iov, ret += child_ret; - if (bson_cmp_less_su (child_ret, to_write_len)) { + if (mcommon_cmp_less_su (child_ret, to_write_len)) { /* we timed out, so send back what we could send */ RETURN (ret); diff --git a/src/libmongoc/src/mongoc/mongoc-stream-tls.c b/src/libmongoc/src/mongoc/mongoc-stream-tls.c index 0f1f7653cf7..1c4b24c70e0 100644 --- a/src/libmongoc/src/mongoc/mongoc-stream-tls.c +++ b/src/libmongoc/src/mongoc/mongoc-stream-tls.c @@ -43,6 +43,7 @@ #endif #include "mongoc-stream-tls.h" #include // BEGIN_IGNORE_DEPRECATIONS +#include #undef MONGOC_LOG_DOMAIN #define MONGOC_LOG_DOMAIN "stream-tls" @@ -109,7 +110,7 @@ mongoc_stream_tls_handshake_block (mongoc_stream_t *stream, const char *host, in return false; } else { const int64_t msec = remaining / 1000; - BSON_ASSERT (bson_in_range_signed (int32_t, msec)); + BSON_ASSERT (mcommon_in_range_signed (int32_t, msec)); timeout_msec = (int32_t) msec; } } diff --git a/src/libmongoc/src/mongoc/mongoc-stream.c b/src/libmongoc/src/mongoc/mongoc-stream.c index 4dff519b8d0..7b97f43f639 100644 --- a/src/libmongoc/src/mongoc/mongoc-stream.c +++ b/src/libmongoc/src/mongoc/mongoc-stream.c @@ -29,6 +29,7 @@ #include "mongoc-stream-private.h" #include "mongoc-trace-private.h" #include "mongoc-util-private.h" +#include #undef MONGOC_LOG_DOMAIN @@ -413,7 +414,7 @@ _mongoc_stream_writev_full ( total_bytes += iov[i].iov_len; } - if (BSON_UNLIKELY (!bson_in_range_signed (int32_t, timeout_msec))) { + if (BSON_UNLIKELY (!mcommon_in_range_signed (int32_t, timeout_msec))) { // CDRIVER-4589 bson_set_error (error, MONGOC_ERROR_STREAM, @@ -444,7 +445,7 @@ _mongoc_stream_writev_full ( RETURN (false); } - if (bson_cmp_not_equal_su (r, total_bytes)) { + if (mcommon_cmp_not_equal_su (r, total_bytes)) { bson_set_error (error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_SOCKET, diff --git a/src/libmongoc/src/mongoc/mongoc-topology-background-monitoring.c b/src/libmongoc/src/mongoc/mongoc-topology-background-monitoring.c index c41a0658cf3..da34931e063 100644 --- a/src/libmongoc/src/mongoc/mongoc-topology-background-monitoring.c +++ b/src/libmongoc/src/mongoc/mongoc-topology-background-monitoring.c @@ -27,6 +27,7 @@ #include "mongoc-topology-private.h" #include "mongoc-trace-private.h" #include "mongoc-util-private.h" +#include #undef MONGOC_LOG_DOMAIN #define MONGOC_LOG_DOMAIN "monitor" @@ -36,7 +37,7 @@ static BSON_THREAD_FUN (srv_polling_run, topology_void) mongoc_topology_t *topology; topology = topology_void; - while (bson_atomic_int_fetch (&topology->scanner_state, bson_memory_order_relaxed) == + while (mcommon_atomic_int_fetch (&topology->scanner_state, mcommon_memory_order_relaxed) == MONGOC_TOPOLOGY_SCANNER_BG_RUNNING) { int64_t now_ms; int64_t scan_due_ms; @@ -64,7 +65,7 @@ static BSON_THREAD_FUN (srv_polling_run, topology_void) * topology srv_polling_mtx for the scan. The topology may have shut * down in that time. */ bson_mutex_lock (&topology->srv_polling_mtx); - if (bson_atomic_int_fetch (&topology->scanner_state, bson_memory_order_relaxed) != + if (mcommon_atomic_int_fetch (&topology->scanner_state, mcommon_memory_order_relaxed) != MONGOC_TOPOLOGY_SCANNER_BG_RUNNING) { bson_mutex_unlock (&topology->srv_polling_mtx); break; @@ -129,10 +130,10 @@ _mongoc_topology_background_monitoring_start (mongoc_topology_t *topology) return; } - prev_state = bson_atomic_int_compare_exchange_strong (&topology->scanner_state, - MONGOC_TOPOLOGY_SCANNER_OFF, - MONGOC_TOPOLOGY_SCANNER_BG_RUNNING, - bson_memory_order_relaxed); + prev_state = mcommon_atomic_int_compare_exchange_strong (&topology->scanner_state, + MONGOC_TOPOLOGY_SCANNER_OFF, + MONGOC_TOPOLOGY_SCANNER_BG_RUNNING, + mcommon_memory_order_relaxed); if (prev_state != MONGOC_TOPOLOGY_SCANNER_OFF) { /* The topology scanner is already running, or another thread is starting @@ -221,7 +222,7 @@ _mongoc_topology_background_monitoring_reconcile (mongoc_topology_t *topology, m BSON_ASSERT (!topology->single_threaded); - if (bson_atomic_int_fetch (&topology->scanner_state, bson_memory_order_relaxed) != + if (mcommon_atomic_int_fetch (&topology->scanner_state, mcommon_memory_order_relaxed) != MONGOC_TOPOLOGY_SCANNER_BG_RUNNING) { return; } @@ -250,7 +251,7 @@ _mongoc_topology_background_monitoring_request_scan (mongoc_topology_t *topology BSON_ASSERT (!topology->single_threaded); - if (bson_atomic_int_fetch (&topology->scanner_state, bson_memory_order_relaxed) == + if (mcommon_atomic_int_fetch (&topology->scanner_state, mcommon_memory_order_relaxed) == MONGOC_TOPOLOGY_SCANNER_SHUTTING_DOWN) { return; } @@ -280,7 +281,7 @@ _mongoc_topology_background_monitoring_stop (mongoc_topology_t *topology) BSON_ASSERT (!topology->single_threaded); - if (bson_atomic_int_fetch (&topology->scanner_state, bson_memory_order_relaxed) != + if (mcommon_atomic_int_fetch (&topology->scanner_state, mcommon_memory_order_relaxed) != MONGOC_TOPOLOGY_SCANNER_BG_RUNNING) { return; } @@ -289,8 +290,8 @@ _mongoc_topology_background_monitoring_stop (mongoc_topology_t *topology) /* Tell the srv polling thread to stop */ bson_mutex_lock (&topology->srv_polling_mtx); - bson_atomic_int_exchange ( - &topology->scanner_state, MONGOC_TOPOLOGY_SCANNER_SHUTTING_DOWN, bson_memory_order_relaxed); + mcommon_atomic_int_exchange ( + &topology->scanner_state, MONGOC_TOPOLOGY_SCANNER_SHUTTING_DOWN, mcommon_memory_order_relaxed); if (topology->is_srv_polling) { /* Signal the srv poller to break out of waiting */ @@ -343,7 +344,7 @@ _mongoc_topology_background_monitoring_stop (mongoc_topology_t *topology) mongoc_set_destroy (topology->rtt_monitors); topology->server_monitors = mongoc_set_new (1, NULL, NULL); topology->rtt_monitors = mongoc_set_new (1, NULL, NULL); - bson_atomic_int_exchange (&topology->scanner_state, MONGOC_TOPOLOGY_SCANNER_OFF, bson_memory_order_relaxed); + mcommon_atomic_int_exchange (&topology->scanner_state, MONGOC_TOPOLOGY_SCANNER_OFF, mcommon_memory_order_relaxed); mongoc_cond_broadcast (&topology->cond_client); bson_mutex_unlock (&topology->tpld_modification_mtx); } diff --git a/src/libmongoc/src/mongoc/mongoc-topology-private.h b/src/libmongoc/src/mongoc/mongoc-topology-private.h index 35c24dcb84d..a94b5b76788 100644 --- a/src/libmongoc/src/mongoc/mongoc-topology-private.h +++ b/src/libmongoc/src/mongoc/mongoc-topology-private.h @@ -30,6 +30,7 @@ #include "mongoc-ts-pool-private.h" #include "mongoc-shared-private.h" #include "mongoc-sleep.h" +#include #define MONGOC_TOPOLOGY_MIN_HEARTBEAT_FREQUENCY_MS 500 #define MONGOC_TOPOLOGY_SOCKET_CHECK_INTERVAL_MS 5000 @@ -448,7 +449,7 @@ _mongoc_topology_set_rr_resolver (mongoc_topology_t *topology, _mongoc_rr_resolv static BSON_INLINE void _mongoc_topology_set_srv_polling_rescan_interval_ms (mongoc_topology_t *topology, int64_t val) { - bson_atomic_int64_exchange (&topology->_atomic_srv_polling_rescan_interval_ms, val, bson_memory_order_seq_cst); + mcommon_atomic_int64_exchange (&topology->_atomic_srv_polling_rescan_interval_ms, val, mcommon_memory_order_seq_cst); } /** @@ -457,7 +458,7 @@ _mongoc_topology_set_srv_polling_rescan_interval_ms (mongoc_topology_t *topology static BSON_INLINE int64_t _mongoc_topology_get_srv_polling_rescan_interval_ms (mongoc_topology_t const *topology) { - return bson_atomic_int64_fetch (&topology->_atomic_srv_polling_rescan_interval_ms, bson_memory_order_seq_cst); + return mcommon_atomic_int64_fetch (&topology->_atomic_srv_polling_rescan_interval_ms, mcommon_memory_order_seq_cst); } /** diff --git a/src/libmongoc/src/mongoc/mongoc-topology-scanner.c b/src/libmongoc/src/mongoc/mongoc-topology-scanner.c index e5a405cdfb2..95d3c9c2901 100644 --- a/src/libmongoc/src/mongoc/mongoc-topology-scanner.c +++ b/src/libmongoc/src/mongoc/mongoc-topology-scanner.c @@ -44,6 +44,8 @@ #include "mongoc-client-private.h" #include "mongoc-util-private.h" #include +#include +#include #include @@ -297,7 +299,7 @@ _mongoc_topology_scanner_dup_handshake_cmd (mongoc_topology_scanner_t *ts, bson_ /* appname will only be changed from NULL, so a non-null pointer will never * be invalidated after this fetch. */ - appname = bson_atomic_ptr_fetch ((void *) &ts->appname, bson_memory_order_relaxed); + appname = mcommon_atomic_ptr_fetch ((void *) &ts->appname, mcommon_memory_order_relaxed); bson_mutex_lock (&ts->handshake_cmd_mtx); /* If this is the first time using the node or if it's the first time @@ -870,7 +872,7 @@ mongoc_topology_scanner_node_setup_tcp (mongoc_topology_scanner_node_t *node, bs if (!node->dns_results) { // Expect no truncation. int req = bson_snprintf (portstr, sizeof portstr, "%hu", host->port); - BSON_ASSERT (bson_cmp_less_su (req, sizeof portstr)); + BSON_ASSERT (mcommon_cmp_less_su (req, sizeof portstr)); memset (&hints, 0, sizeof hints); hints.ai_family = host->family; @@ -933,7 +935,7 @@ mongoc_topology_scanner_node_connect_unix (mongoc_topology_scanner_node_t *node, // Expect no truncation. int req = bson_snprintf (saddr.sun_path, sizeof saddr.sun_path - 1, "%s", host->host); - if (bson_cmp_greater_equal_su (req, sizeof saddr.sun_path - 1)) { + if (mcommon_cmp_greater_equal_su (req, sizeof saddr.sun_path - 1)) { bson_set_error (error, MONGOC_ERROR_STREAM, MONGOC_ERROR_STREAM_SOCKET, "Failed to define socket address path."); RETURN (false); } @@ -1249,7 +1251,7 @@ _mongoc_topology_scanner_set_appname (mongoc_topology_scanner_t *ts, const char } s = bson_strdup (appname); - prev = bson_atomic_ptr_compare_exchange_strong ((void *) &ts->appname, NULL, s, bson_memory_order_relaxed); + prev = mcommon_atomic_ptr_compare_exchange_strong ((void *) &ts->appname, NULL, s, mcommon_memory_order_relaxed); if (prev == NULL) { return true; } diff --git a/src/libmongoc/src/mongoc/mongoc-topology.c b/src/libmongoc/src/mongoc/mongoc-topology.c index 4bcc2d215f4..a5cf9e7a32a 100644 --- a/src/libmongoc/src/mongoc/mongoc-topology.c +++ b/src/libmongoc/src/mongoc/mongoc-topology.c @@ -37,6 +37,7 @@ #include #include +#include static void _topology_collect_errors (const mongoc_topology_description_t *topology, bson_error_t *error_out); @@ -615,7 +616,7 @@ mongoc_topology_new (const mongoc_uri_t *uri, bool single_threaded) size_t hl_array_size = 0u; - BSON_ASSERT (bson_in_range_signed (size_t, td->max_hosts)); + BSON_ASSERT (mcommon_in_range_signed (size_t, td->max_hosts)); const mongoc_host_list_t *const *hl_array = _mongoc_apply_srv_max_hosts (hl, (size_t) td->max_hosts, &hl_array_size); for (size_t idx = 0u; idx < hl_array_size; ++idx) { diff --git a/src/libmongoc/src/mongoc/mongoc-ts-pool.c b/src/libmongoc/src/mongoc/mongoc-ts-pool.c index f00e4e8d0ea..b8b20fbefdf 100644 --- a/src/libmongoc/src/mongoc/mongoc-ts-pool.c +++ b/src/libmongoc/src/mongoc/mongoc-ts-pool.c @@ -2,6 +2,7 @@ #include "common-thread-private.h" #include "bson/bson.h" +#include /** * Toggle this to enable/disable checks that all items are returned to the pool @@ -159,7 +160,7 @@ _new_item (mongoc_ts_pool *pool, bson_error_t *error) } } if (node && audit_pool_enabled) { - bson_atomic_int32_fetch_add (&pool->outstanding_items, 1, bson_memory_order_relaxed); + mcommon_atomic_int32_fetch_add (&pool->outstanding_items, 1, mcommon_memory_order_relaxed); } return node; } @@ -191,9 +192,9 @@ _try_get (mongoc_ts_pool *pool) } bson_mutex_unlock (&pool->mtx); if (node) { - bson_atomic_int32_fetch_sub (&pool->size, 1, bson_memory_order_relaxed); + mcommon_atomic_int32_fetch_sub (&pool->size, 1, mcommon_memory_order_relaxed); if (audit_pool_enabled) { - bson_atomic_int32_fetch_add (&pool->outstanding_items, 1, bson_memory_order_relaxed); + mcommon_atomic_int32_fetch_add (&pool->outstanding_items, 1, mcommon_memory_order_relaxed); } } return node; @@ -302,9 +303,9 @@ mongoc_ts_pool_return (mongoc_ts_pool *pool, void *item) node->next = pool->head; pool->head = node; bson_mutex_unlock (&pool->mtx); - bson_atomic_int32_fetch_add (&node->owner_pool->size, 1, bson_memory_order_relaxed); + mcommon_atomic_int32_fetch_add (&node->owner_pool->size, 1, mcommon_memory_order_relaxed); if (audit_pool_enabled) { - bson_atomic_int32_fetch_sub (&node->owner_pool->outstanding_items, 1, bson_memory_order_relaxed); + mcommon_atomic_int32_fetch_sub (&node->owner_pool->outstanding_items, 1, mcommon_memory_order_relaxed); } } } @@ -317,7 +318,7 @@ mongoc_ts_pool_drop (mongoc_ts_pool *pool, void *item) BSON_ASSERT (pool == node->owner_pool); if (audit_pool_enabled) { - bson_atomic_int32_fetch_sub (&node->owner_pool->outstanding_items, 1, bson_memory_order_relaxed); + mcommon_atomic_int32_fetch_sub (&node->owner_pool->outstanding_items, 1, mcommon_memory_order_relaxed); } _delete_item (node); } @@ -331,7 +332,7 @@ mongoc_ts_pool_is_empty (const mongoc_ts_pool *pool) size_t mongoc_ts_pool_size (const mongoc_ts_pool *pool) { - return bson_atomic_int32_fetch (&pool->size, bson_memory_order_relaxed); + return mcommon_atomic_int32_fetch (&pool->size, mcommon_memory_order_relaxed); } void diff --git a/src/libmongoc/src/mongoc/mongoc-util.c b/src/libmongoc/src/mongoc/mongoc-util.c index b53d6c5559d..243cc248d1a 100644 --- a/src/libmongoc/src/mongoc/mongoc-util.c +++ b/src/libmongoc/src/mongoc/mongoc-util.c @@ -31,6 +31,7 @@ #include "mongoc-client-session-private.h" #include "mongoc-trace-private.h" #include "mongoc-sleep.h" +#include const bson_validate_flags_t _mongoc_default_insert_vflags = BSON_VALIDATE_UTF8 | BSON_VALIDATE_UTF8_ALLOW_NULL | BSON_VALIDATE_EMPTY_KEYS; @@ -987,7 +988,7 @@ hex_to_bin (const char *hex, uint32_t *len) return NULL; } - BSON_ASSERT (bson_in_range_unsigned (uint32_t, hex_len / 2u)); + BSON_ASSERT (mcommon_in_range_unsigned (uint32_t, hex_len / 2u)); *len = (uint32_t) (hex_len / 2u); out = bson_malloc0 (*len); @@ -1000,7 +1001,7 @@ hex_to_bin (const char *hex, uint32_t *len) return NULL; } - BSON_ASSERT (bson_in_range_unsigned (uint8_t, hex_char)); + BSON_ASSERT (mcommon_in_range_unsigned (uint8_t, hex_char)); out[i / 2u] = (uint8_t) hex_char; } return out; diff --git a/src/libmongoc/src/mongoc/mongoc-write-command.c b/src/libmongoc/src/mongoc/mongoc-write-command.c index 69ebf54f99e..e6e24e6a09f 100644 --- a/src/libmongoc/src/mongoc/mongoc-write-command.c +++ b/src/libmongoc/src/mongoc/mongoc-write-command.c @@ -27,6 +27,7 @@ #include "mongoc-util-private.h" #include "mongoc-opts-private.h" #include +#include #include @@ -690,7 +691,7 @@ _mongoc_write_opmsg (mongoc_write_command_t *command, ulen = BSON_UINT32_FROM_LE (ulen); // Although messageLength is an int32, it should never be negative. - BSON_ASSERT (bson_in_range_unsigned (int32_t, ulen)); + BSON_ASSERT (mcommon_in_range_unsigned (int32_t, ulen)); const int32_t slen = (int32_t) ulen; if (slen > max_bson_obj_size + BSON_OBJECT_ALLOWANCE) { @@ -699,7 +700,7 @@ _mongoc_write_opmsg (mongoc_write_command_t *command, result->failed = true; break; - } else if (bson_cmp_less_equal_us (payload_batch_size + opmsg_overhead + ulen, max_msg_size) || + } else if (mcommon_cmp_less_equal_us (payload_batch_size + opmsg_overhead + ulen, max_msg_size) || document_count == 0) { /* The current batch is still under max batch size in bytes */ payload_batch_size += ulen; diff --git a/src/libmongoc/tests/mock_server/mock-server.c b/src/libmongoc/tests/mock_server/mock-server.c index c8f2e0a7b3b..cadf102a411 100644 --- a/src/libmongoc/tests/mock_server/mock-server.c +++ b/src/libmongoc/tests/mock_server/mock-server.c @@ -29,6 +29,7 @@ #include "../test-libmongoc.h" #include "../TestSuite.h" #include +#include #ifdef BSON_HAVE_STRINGS_H #include @@ -354,7 +355,7 @@ mock_server_run (mock_server_t *server) // socklen_t: an unsigned opaque integral type of length of at least 32 bits. // To forestall portability problems, it is recommended that applications // should not use values larger than 2^32 - 1. - BSON_ASSERT (bson_in_range_unsigned (uint32_t, bind_addr_len)); + BSON_ASSERT (mcommon_in_range_unsigned (uint32_t, bind_addr_len)); if (-1 == mongoc_socket_bind (ssock, (struct sockaddr *) bind_addr, (uint32_t) bind_addr_len)) { perror ("Failed to bind socket"); @@ -2066,7 +2067,7 @@ _mock_server_reply_with_stream (mock_server_t *server, reply_t *reply, mongoc_st const ssize_t n_written = mongoc_stream_writev (client, iov, iovcnt, -1); - BSON_ASSERT (bson_cmp_equal_su (n_written, expected)); + BSON_ASSERT (mcommon_cmp_equal_su (n_written, expected)); bson_free (iov); mcd_rpc_message_destroy (rpc); diff --git a/src/libmongoc/tests/mock_server/request.c b/src/libmongoc/tests/mock_server/request.c index 2ac702b4015..3fe6091fe21 100644 --- a/src/libmongoc/tests/mock_server/request.c +++ b/src/libmongoc/tests/mock_server/request.c @@ -22,6 +22,7 @@ #include "../test-conveniences.h" #include "../TestSuite.h" #include +#include static bool is_command_ns (const char *ns); @@ -54,7 +55,7 @@ request_new (const mongoc_buffer_t *buffer, BSON_ASSERT_PARAM (client); BSON_ASSERT_PARAM (replies); - BSON_ASSERT (bson_in_range_signed (size_t, msg_len)); + BSON_ASSERT (mcommon_in_range_signed (size_t, msg_len)); request_t *const request = (request_t *) bson_malloc0 (sizeof *request); @@ -126,7 +127,7 @@ assert_request_matches_flags (const request_t *request, uint32_t flags) BSON_ASSERT (request); const int32_t request_flags = mcd_rpc_op_query_get_flags (request->rpc); - if (bson_cmp_not_equal_su (request_flags, flags)) { + if (mcommon_cmp_not_equal_su (request_flags, flags)) { test_error ("request's query flags are %s, expected %s", query_flags_str (request_flags), query_flags_str ((int32_t) flags)); @@ -200,7 +201,7 @@ request_matches_query (const request_t *request, assert_request_matches_flags (request, flags); const int32_t request_skip = mcd_rpc_op_query_get_number_to_skip (request->rpc); - if (bson_cmp_not_equal_su (request_skip, skip)) { + if (mcommon_cmp_not_equal_su (request_skip, skip)) { test_error ("requests's skip = %" PRId32 ", expected %" PRIu32 ": %s", request_skip, skip, doc_as_json); goto done; } diff --git a/src/libmongoc/tests/test-libmongoc-main.c b/src/libmongoc/tests/test-libmongoc-main.c index f38ab7431c7..025039440de 100644 --- a/src/libmongoc/tests/test-libmongoc-main.c +++ b/src/libmongoc/tests/test-libmongoc-main.c @@ -45,6 +45,8 @@ main (int argc, char *argv[]) TEST_INSTALL (test_writer_install); TEST_INSTALL (test_b64_install); TEST_INSTALL (test_bson_cmp_install); + TEST_INSTALL (test_mcommon_cmp_install); + TEST_INSTALL (test_mcommon_atomic_install); /* libmongoc */ diff --git a/src/libmongoc/tests/test-mcd-rpc.c b/src/libmongoc/tests/test-mcd-rpc.c index 576c599d920..c9af14d674e 100644 --- a/src/libmongoc/tests/test-mcd-rpc.c +++ b/src/libmongoc/tests/test-mcd-rpc.c @@ -6,6 +6,7 @@ #include "TestSuite.h" #include +#include // clang-format off @@ -355,7 +356,7 @@ test_rpc_message_from_data_op_compressed_valid (void) mcd_rpc_message *const rpc = mcd_rpc_message_from_data (data, data_len, &data_end); ASSERT_RPC_MESSAGE_RESULT (rpc, data, data_end, data_len); - ASSERT (bson_in_range_unsigned (int32_t, data_len)); + ASSERT (mcommon_in_range_unsigned (int32_t, data_len)); ASSERT_CMPINT32 (mcd_rpc_header_get_message_length (rpc), ==, (int32_t) data_len); ASSERT_CMPINT32 (mcd_rpc_header_get_request_id (rpc), ==, 16909060); ASSERT_CMPINT32 (mcd_rpc_header_get_response_to (rpc), ==, 84281096); @@ -419,7 +420,7 @@ _test_rpc_message_from_data_op_msg_valid_kind_0 (const uint8_t *data, size_t dat mcd_rpc_message *const rpc = mcd_rpc_message_from_data (data, data_len, &data_end); ASSERT_RPC_MESSAGE_RESULT (rpc, data, data_end, data_len); - ASSERT (bson_in_range_unsigned (int32_t, data_len)); + ASSERT (mcommon_in_range_unsigned (int32_t, data_len)); ASSERT_CMPINT32 (mcd_rpc_header_get_message_length (rpc), ==, (int32_t) data_len); ASSERT_CMPINT32 (mcd_rpc_header_get_request_id (rpc), ==, 16909060); ASSERT_CMPINT32 (mcd_rpc_header_get_response_to (rpc), ==, 84281096); @@ -469,7 +470,7 @@ _test_rpc_message_from_data_op_msg_valid_kind_1_single (const uint8_t *data, siz mcd_rpc_message *const rpc = mcd_rpc_message_from_data (data, data_len, &data_end); ASSERT_RPC_MESSAGE_RESULT (rpc, data, data_end, data_len); - ASSERT (bson_in_range_unsigned (int32_t, data_len)); + ASSERT (mcommon_in_range_unsigned (int32_t, data_len)); ASSERT_CMPINT32 (mcd_rpc_header_get_message_length (rpc), ==, (int32_t) data_len); ASSERT_CMPINT32 (mcd_rpc_header_get_request_id (rpc), ==, 16909060); ASSERT_CMPINT32 (mcd_rpc_header_get_response_to (rpc), ==, 84281096); @@ -541,7 +542,7 @@ _test_rpc_message_from_data_op_msg_valid_kind_1_multiple (const uint8_t *data, s mcd_rpc_message *const rpc = mcd_rpc_message_from_data (data, data_len, &data_end); ASSERT_RPC_MESSAGE_RESULT (rpc, data, data_end, data_len); - ASSERT (bson_in_range_unsigned (int32_t, data_len)); + ASSERT (mcommon_in_range_unsigned (int32_t, data_len)); ASSERT_CMPINT32 (mcd_rpc_header_get_message_length (rpc), ==, (int32_t) data_len); ASSERT_CMPINT32 (mcd_rpc_header_get_request_id (rpc), ==, 16909060); ASSERT_CMPINT32 (mcd_rpc_header_get_response_to (rpc), ==, 84281096); @@ -658,7 +659,7 @@ test_rpc_message_from_data_op_reply_valid (void) mcd_rpc_message *const rpc = mcd_rpc_message_from_data (data, data_len, &data_end); ASSERT_RPC_MESSAGE_RESULT (rpc, data, data_end, data_len); - ASSERT (bson_in_range_unsigned (int32_t, data_len)); + ASSERT (mcommon_in_range_unsigned (int32_t, data_len)); ASSERT_CMPINT32 (mcd_rpc_header_get_message_length (rpc), ==, (int32_t) data_len); ASSERT_CMPINT32 (mcd_rpc_header_get_request_id (rpc), ==, 16909060); ASSERT_CMPINT32 (mcd_rpc_header_get_response_to (rpc), ==, 84281096); @@ -758,7 +759,7 @@ test_rpc_message_from_data_op_update_valid (void) mcd_rpc_message *const rpc = mcd_rpc_message_from_data (data, data_len, &data_end); ASSERT_RPC_MESSAGE_RESULT (rpc, data, data_end, data_len); - ASSERT (bson_in_range_unsigned (int32_t, data_len)); + ASSERT (mcommon_in_range_unsigned (int32_t, data_len)); ASSERT_CMPINT32 (mcd_rpc_header_get_message_length (rpc), ==, (int32_t) data_len); ASSERT_CMPINT32 (mcd_rpc_header_get_request_id (rpc), ==, 16909060); ASSERT_CMPINT32 (mcd_rpc_header_get_response_to (rpc), ==, 84281096); @@ -821,7 +822,7 @@ test_rpc_message_from_data_op_insert_valid (void) mcd_rpc_message *const rpc = mcd_rpc_message_from_data (data, data_len, &data_end); ASSERT_RPC_MESSAGE_RESULT (rpc, data, data_end, data_len); - ASSERT (bson_in_range_unsigned (int32_t, data_len)); + ASSERT (mcommon_in_range_unsigned (int32_t, data_len)); ASSERT_CMPINT32 (mcd_rpc_header_get_message_length (rpc), ==, (int32_t) data_len); ASSERT_CMPINT32 (mcd_rpc_header_get_request_id (rpc), ==, 16909060); ASSERT_CMPINT32 (mcd_rpc_header_get_response_to (rpc), ==, 84281096); @@ -900,7 +901,7 @@ test_rpc_message_from_data_op_query_valid (void) mcd_rpc_message *const rpc = mcd_rpc_message_from_data (data, data_len, &data_end); ASSERT_RPC_MESSAGE_RESULT (rpc, data, data_end, data_len); - ASSERT (bson_in_range_unsigned (int32_t, data_len)); + ASSERT (mcommon_in_range_unsigned (int32_t, data_len)); ASSERT_CMPINT32 (mcd_rpc_header_get_message_length (rpc), ==, (int32_t) data_len); ASSERT_CMPINT32 (mcd_rpc_header_get_request_id (rpc), ==, 16909060); ASSERT_CMPINT32 (mcd_rpc_header_get_response_to (rpc), ==, 84281096); @@ -1004,7 +1005,7 @@ test_rpc_message_from_data_op_get_more_valid (void) mcd_rpc_message *const rpc = mcd_rpc_message_from_data (data, data_len, &data_end); ASSERT_RPC_MESSAGE_RESULT (rpc, data, data_end, data_len); - ASSERT (bson_in_range_unsigned (int32_t, data_len)); + ASSERT (mcommon_in_range_unsigned (int32_t, data_len)); ASSERT_CMPINT32 (mcd_rpc_header_get_message_length (rpc), ==, (int32_t) data_len); ASSERT_CMPINT32 (mcd_rpc_header_get_request_id (rpc), ==, 16909060); ASSERT_CMPINT32 (mcd_rpc_header_get_response_to (rpc), ==, 84281096); @@ -1032,7 +1033,7 @@ test_rpc_message_from_data_op_delete_valid (void) mcd_rpc_message *const rpc = mcd_rpc_message_from_data (data, data_len, &data_end); ASSERT_RPC_MESSAGE_RESULT (rpc, data, data_end, data_len); - ASSERT (bson_in_range_unsigned (int32_t, data_len)); + ASSERT (mcommon_in_range_unsigned (int32_t, data_len)); ASSERT_CMPINT32 (mcd_rpc_header_get_message_length (rpc), ==, (int32_t) data_len); ASSERT_CMPINT32 (mcd_rpc_header_get_request_id (rpc), ==, 16909060); ASSERT_CMPINT32 (mcd_rpc_header_get_response_to (rpc), ==, 84281096); @@ -1071,7 +1072,7 @@ test_rpc_message_from_data_op_kill_cursors_valid (void) mcd_rpc_message *const rpc = mcd_rpc_message_from_data (data, data_len, &data_end); ASSERT_RPC_MESSAGE_RESULT (rpc, data, data_end, data_len); - ASSERT (bson_in_range_unsigned (int32_t, data_len)); + ASSERT (mcommon_in_range_unsigned (int32_t, data_len)); ASSERT_CMPINT32 (mcd_rpc_header_get_message_length (rpc), ==, (int32_t) data_len); ASSERT_CMPINT32 (mcd_rpc_header_get_request_id (rpc), ==, 16909060); ASSERT_CMPINT32 (mcd_rpc_header_get_response_to (rpc), ==, 84281096); diff --git a/src/libmongoc/tests/test-mongoc-bulk.c b/src/libmongoc/tests/test-mongoc-bulk.c index 393bf53e999..d4558c629a6 100644 --- a/src/libmongoc/tests/test-mongoc-bulk.c +++ b/src/libmongoc/tests/test-mongoc-bulk.c @@ -12,6 +12,7 @@ #include "mock_server/mock-server.h" #include "test-conveniences.h" #include "mock_server/mock-rs.h" +#include typedef void (*update_fn) (mongoc_bulk_operation_t *bulk, const bson_t *selector, const bson_t *document, bool upsert); @@ -3572,7 +3573,7 @@ test_bulk_max_msg_size (void) /* fill up to the 48 000 000 bytes message size */ bson_init (&doc); bson_append_int32 (&doc, "_id", -1, 3); - ASSERT (bson_in_range_unsigned (int, filler_string)); + ASSERT (mcommon_in_range_unsigned (int, filler_string)); bson_append_utf8 (&doc, "msg", -1, msg, (int) filler_string); mongoc_bulk_operation_insert (bulk, &doc); bson_destroy (&doc); @@ -3607,7 +3608,7 @@ test_bulk_max_msg_size (void) /* fill up to the 48 000 001 bytes message size */ bson_init (&doc); bson_append_int32 (&doc, "_id", -1, 3); - ASSERT (bson_in_range_unsigned (int, filler_string + 1u)); + ASSERT (mcommon_in_range_unsigned (int, filler_string + 1u)); bson_append_utf8 (&doc, "msg", -1, msg, (int) (filler_string + 1u)); mongoc_bulk_operation_insert (bulk, &doc); bson_destroy (&doc); diff --git a/src/libmongoc/tests/test-mongoc-client-side-encryption.c b/src/libmongoc/tests/test-mongoc-client-side-encryption.c index b0b822f1e02..c4c27963ff7 100644 --- a/src/libmongoc/tests/test-mongoc-client-side-encryption.c +++ b/src/libmongoc/tests/test-mongoc-client-side-encryption.c @@ -32,6 +32,7 @@ #include "mongoc/mongoc-uri.h" #include "mongoc/mongoc-http-private.h" +#include static void _before_test (json_test_ctx_t *ctx, const bson_t *test) @@ -601,7 +602,7 @@ test_datakey_and_double_encryption_creating_and_using (mongoc_client_encryption_ to_encrypt.value.v_utf8.str = bson_strdup (hello); const size_t len = strlen (to_encrypt.value.v_utf8.str); - ASSERT (bson_in_range_unsigned (uint32_t, len)); + ASSERT (mcommon_in_range_unsigned (uint32_t, len)); to_encrypt.value.v_utf8.len = (uint32_t) len; } @@ -4736,7 +4737,7 @@ decryption_events_setup (void) plaintext.value.v_utf8.str = "hello"; const size_t len = strlen (plaintext.value.v_utf8.str); - ASSERT (bson_in_range_unsigned (uint32_t, len)); + ASSERT (mcommon_in_range_unsigned (uint32_t, len)); plaintext.value.v_utf8.len = (uint32_t) len; diff --git a/src/libmongoc/tests/test-mongoc-counters.c b/src/libmongoc/tests/test-mongoc-counters.c index 8db16ad2563..76a805862a2 100644 --- a/src/libmongoc/tests/test-mongoc-counters.c +++ b/src/libmongoc/tests/test-mongoc-counters.c @@ -21,6 +21,8 @@ #include "test-libmongoc.h" #include "TestSuite.h" #include "mock_server/future-functions.h" +#include +#include /* test statistics counters excluding OP_INSERT, OP_UPDATE, and OP_DELETE since * those were superseded by write commands in 2.6. */ @@ -33,7 +35,7 @@ /* helper to reset a prev_* counter */ #define RESET(ident) \ - bson_atomic_int32_exchange (&prev_##ident, mongoc_counter_##ident##_count (), bson_memory_order_seq_cst) + mcommon_atomic_int32_exchange (&prev_##ident, mongoc_counter_##ident##_count (), mcommon_memory_order_seq_cst) /* helper to compare and reset a prev_* counter. */ #define DIFF_AND_RESET(ident, cmp, expected) \ @@ -1262,7 +1264,7 @@ _test_counters_auth (bool with_op_msg, bool pooled) // Number of messages sent by background threads depend on the number of // members in the replica set. const size_t member_count_zu = test_framework_replset_member_count (); - ASSERT (bson_in_range_unsigned (int32_t, member_count_zu)); + ASSERT (mcommon_in_range_unsigned (int32_t, member_count_zu)); const int32_t member_count = (int32_t) member_count_zu; // MongoDB Handshake Spec: Since MongoDB server 4.4, the initial handshake diff --git a/src/libmongoc/tests/test-mongoc-crud.c b/src/libmongoc/tests/test-mongoc-crud.c index 7ca17add3f2..4823d56bfc4 100644 --- a/src/libmongoc/tests/test-mongoc-crud.c +++ b/src/libmongoc/tests/test-mongoc-crud.c @@ -4,6 +4,7 @@ #include "json-test-operations.h" #include "test-libmongoc.h" #include "mongoc-bulkwrite.h" +#include static bool crud_test_operation_cb (json_test_ctx_t *ctx, const bson_t *test, const bson_t *operation) @@ -287,7 +288,7 @@ prose_test_3 (void *ctx) static char * repeat_char (char c, int32_t count) { - ASSERT (bson_in_range_size_t_signed (count)); + ASSERT (mcommon_in_range_size_t_signed (count)); char *str = bson_malloc (count + 1); memset (str, c, count); str[count] = '\0'; @@ -530,7 +531,7 @@ prose_test_6 (void *ctx) // Count write errors. { const bson_t *writeErrors = mongoc_bulkwriteexception_writeerrors (ret.exc); - ASSERT (bson_in_range_uint32_t_signed (maxWriteBatchSize + 1)); + ASSERT (mcommon_in_range_uint32_t_signed (maxWriteBatchSize + 1)); ASSERT_CMPUINT32 (bson_count_keys (writeErrors), ==, (uint32_t) maxWriteBatchSize + 1); } @@ -1061,7 +1062,7 @@ prose_test_11 (void *ctx) { mongoc_bulkwritereturn_t bwr = mongoc_bulkwrite_execute (tf->bw, NULL /* opts */); ASSERT_NO_BULKWRITEEXCEPTION (bwr); - ASSERT (bson_in_range_int64_t_unsigned (tf->numModels)); + ASSERT (mcommon_in_range_int64_t_unsigned (tf->numModels)); ASSERT_CMPINT64 (mongoc_bulkwriteresult_insertedcount (bwr.res), ==, (int64_t) tf->numModels + 1); mongoc_bulkwriteresult_destroy (bwr.res); mongoc_bulkwriteexception_destroy (bwr.exc); @@ -1073,7 +1074,7 @@ prose_test_11 (void *ctx) bson_t *first = _mongoc_array_index (&tf->captured, bson_t *, 0); { bson_t *ops = bson_lookup_bson (first, "ops"); - ASSERT (bson_in_range_uint32_t_unsigned (tf->numModels)); + ASSERT (mcommon_in_range_uint32_t_unsigned (tf->numModels)); ASSERT_CMPUINT32 (bson_count_keys (ops), ==, (uint32_t) tf->numModels + 1); bson_destroy (ops); @@ -1111,7 +1112,7 @@ prose_test_11 (void *ctx) { mongoc_bulkwritereturn_t bwr = mongoc_bulkwrite_execute (tf->bw, NULL /* opts */); ASSERT_NO_BULKWRITEEXCEPTION (bwr); - ASSERT (bson_in_range_int64_t_unsigned (tf->numModels)); + ASSERT (mcommon_in_range_int64_t_unsigned (tf->numModels)); ASSERT_CMPINT64 (mongoc_bulkwriteresult_insertedcount (bwr.res), ==, (int64_t) tf->numModels + 1); mongoc_bulkwriteresult_destroy (bwr.res); mongoc_bulkwriteexception_destroy (bwr.exc); @@ -1123,7 +1124,7 @@ prose_test_11 (void *ctx) bson_t *first = _mongoc_array_index (&tf->captured, bson_t *, 0); { bson_t *ops = bson_lookup_bson (first, "ops"); - ASSERT (bson_in_range_uint32_t_unsigned (tf->numModels)); + ASSERT (mcommon_in_range_uint32_t_unsigned (tf->numModels)); ASSERT_CMPUINT32 (bson_count_keys (ops), ==, (uint32_t) tf->numModels); bson_destroy (ops); diff --git a/src/libmongoc/tests/test-mongoc-cursor.c b/src/libmongoc/tests/test-mongoc-cursor.c index 886a6e9838b..1e69c7cc747 100644 --- a/src/libmongoc/tests/test-mongoc-cursor.c +++ b/src/libmongoc/tests/test-mongoc-cursor.c @@ -12,6 +12,7 @@ #include "mongoc/mongoc-write-concern-private.h" #include "test-conveniences.h" #include +#include typedef mongoc_cursor_t *(*make_cursor_fn) (mongoc_collection_t *); @@ -1153,7 +1154,7 @@ test_cursor_int64_t_maxtimems (void) bson_append_bool (max_await_time_ms, "awaitData", 9, true); bson_append_int64 ( max_await_time_ms, MONGOC_CURSOR_MAX_AWAIT_TIME_MS, MONGOC_CURSOR_MAX_AWAIT_TIME_MS_LEN, ms_int64); - ASSERT (bson_in_range_int32_t_unsigned (server_id)); + ASSERT (mcommon_in_range_int32_t_unsigned (server_id)); BSON_APPEND_INT32 (max_await_time_ms, "serverId", (uint32_t) server_id); cursor = mongoc_cursor_new_from_command_reply_with_opts (client, diff --git a/src/libmongoc/tests/test-mongoc-dns.c b/src/libmongoc/tests/test-mongoc-dns.c index 79c47da4a91..a7f28552595 100644 --- a/src/libmongoc/tests/test-mongoc-dns.c +++ b/src/libmongoc/tests/test-mongoc-dns.c @@ -13,6 +13,7 @@ #include "json-test.h" #include "test-libmongoc.h" +#include static void _assert_options_match (const bson_t *test, mongoc_uri_t *uri) @@ -202,7 +203,7 @@ _host_list_matches (const bson_t *test, context_t *ctx) ctx->hosts = NULL; bson_mutex_unlock (&ctx->mutex); - ret = bson_cmp_equal_su (expected, actual); + ret = mcommon_cmp_equal_su (expected, actual); } return ret; @@ -897,7 +898,7 @@ _mock_rr_resolver_prose_test_10 (const char *service, if (rr_type == MONGOC_RR_SRV) { const size_t count = _mongoc_host_list_length (rr_data->hosts); - BSON_ASSERT (bson_in_range_unsigned (uint32_t, count)); + BSON_ASSERT (mcommon_in_range_unsigned (uint32_t, count)); rr_data->hosts = MAKE_HOSTS ("localhost.test.build.10gen.cc:27017", "localhost.test.build.10gen.cc:27019", @@ -999,7 +1000,7 @@ _mock_rr_resolver_prose_test_11 (const char *service, if (rr_type == MONGOC_RR_SRV) { const size_t count = _mongoc_host_list_length (rr_data->hosts); - BSON_ASSERT (bson_in_range_unsigned (uint32_t, count)); + BSON_ASSERT (mcommon_in_range_unsigned (uint32_t, count)); rr_data->hosts = MAKE_HOSTS ("localhost.test.build.10gen.cc:27019", "localhost.test.build.10gen.cc:27020"); rr_data->count = (uint32_t) count; @@ -1098,7 +1099,7 @@ _mock_rr_resolver_prose_test_12 (const char *service, if (rr_type == MONGOC_RR_SRV) { const size_t count = _mongoc_host_list_length (rr_data->hosts); - BSON_ASSERT (bson_in_range_unsigned (uint32_t, count)); + BSON_ASSERT (mcommon_in_range_unsigned (uint32_t, count)); rr_data->hosts = MAKE_HOSTS ("localhost.test.build.10gen.cc:27017", "localhost.test.build.10gen.cc:27019", @@ -1249,7 +1250,7 @@ _mock_rr_resolver_with_override (const char *service, if (rr_type == MONGOC_RR_SRV) { bson_mutex_lock (&rr_override.lock); const size_t count = _mongoc_host_list_length (rr_override.hosts); - BSON_ASSERT (bson_in_range_unsigned (uint32_t, count)); + BSON_ASSERT (mcommon_in_range_unsigned (uint32_t, count)); rr_data->hosts = _mongoc_host_list_copy_all (rr_override.hosts); rr_data->count = (uint32_t) count; rr_data->txt_record_opts = NULL; diff --git a/src/libmongoc/tests/test-mongoc-gridfs.c b/src/libmongoc/tests/test-mongoc-gridfs.c index 672dd969d8f..4c713602da2 100644 --- a/src/libmongoc/tests/test-mongoc-gridfs.c +++ b/src/libmongoc/tests/test-mongoc-gridfs.c @@ -13,6 +13,7 @@ #include "mock_server/future.h" #include "mock_server/future-functions.h" #include +#include static mongoc_gridfs_t * @@ -841,14 +842,14 @@ test_write_past_end (void) ASSERT (file); r = mongoc_gridfs_file_writev (file, iov, 1, 0); - ASSERT (bson_in_range_signed (size_t, r)); + ASSERT (mcommon_in_range_signed (size_t, r)); ASSERT_CMPSIZE_T ((size_t) r, ==, len); ASSERT_CMPINT (mongoc_gridfs_file_seek (file, (int64_t) delta, SEEK_SET), ==, 0); ASSERT_CMPUINT64 (mongoc_gridfs_file_tell (file), ==, delta); r = mongoc_gridfs_file_writev (file, iov, 1, 0); - ASSERT (bson_in_range_signed (size_t, r)); + ASSERT (mcommon_in_range_signed (size_t, r)); ASSERT_CMPSIZE_T ((size_t) r, ==, len); mongoc_gridfs_file_save (file); @@ -856,17 +857,17 @@ test_write_past_end (void) mongoc_collection_count_documents (mongoc_gridfs_get_chunks (gridfs), tmp_bson (NULL), NULL, NULL, NULL, &error); ASSERT_OR_PRINT (cnt != -1, error); - ASSERT (bson_cmp_equal_us (expected_chunks, cnt)); + ASSERT (mcommon_cmp_equal_us (expected_chunks, cnt)); mongoc_gridfs_file_destroy (file); file = mongoc_gridfs_find_one (gridfs, tmp_bson (NULL), &error); ASSERT_OR_PRINT (file, error); - BSON_ASSERT (bson_in_range_unsigned (size_t, delta + len)); + BSON_ASSERT (mcommon_in_range_unsigned (size_t, delta + len)); const size_t total_bytes = (size_t) (delta + len); r = mongoc_gridfs_file_readv (file, &riov, 1, total_bytes, 0); - ASSERT (bson_in_range_signed (size_t, r)); + ASSERT (mcommon_in_range_signed (size_t, r)); ASSERT_CMPSIZE_T ((size_t) r, ==, total_bytes); mongoc_gridfs_file_destroy (file); @@ -962,7 +963,7 @@ test_stream (void) stream = mongoc_stream_gridfs_new (file); - ASSERT (bson_in_range_signed (size_t, file->length)); + ASSERT (mcommon_in_range_signed (size_t, file->length)); const ssize_t r = mongoc_stream_readv (stream, &iov, 1, (size_t) file->length, 0); ASSERT_CMPINT64 ((int64_t) r, ==, file->length); @@ -1516,7 +1517,7 @@ test_reading_multiple_chunks (void) ssize_t got = mongoc_gridfs_file_readv (file, &iov, 1 /* iovcnt */, 1 /* min_bytes */, 0 /* timeout_msec */); ASSERT_CMPSSIZE_T (got, >=, 0); - ASSERT (bson_in_range_int_signed (got)); + ASSERT (mcommon_in_range_int_signed (got)); mcd_string_append_printf (str, "%.*s", (int) got, (char *) buf); ASSERT_CMPSSIZE_T (got, ==, 4); } @@ -1526,7 +1527,7 @@ test_reading_multiple_chunks (void) ssize_t got = mongoc_gridfs_file_readv (file, &iov, 1 /* iovcnt */, 1 /* min_bytes */, 0 /* timeout_msec */); ASSERT_CMPSSIZE_T (got, >=, 0); - ASSERT (bson_in_range_int_signed (got)); + ASSERT (mcommon_in_range_int_signed (got)); mcd_string_append_printf (str, "%.*s", (int) got, (char *) buf); ASSERT_CMPSSIZE_T (got, ==, 3); } diff --git a/src/libmongoc/tests/test-mongoc-sdam-monitoring.c b/src/libmongoc/tests/test-mongoc-sdam-monitoring.c index 3954de280dd..252e630c597 100644 --- a/src/libmongoc/tests/test-mongoc-sdam-monitoring.c +++ b/src/libmongoc/tests/test-mongoc-sdam-monitoring.c @@ -9,6 +9,7 @@ #include "mock_server/future.h" #include "mock_server/future-functions.h" #include "json-test-monitoring.h" +#include #ifdef BSON_HAVE_STRINGS_H #include @@ -134,7 +135,7 @@ td_to_bson (const mongoc_topology_description_t *td, bson_t *bson) mongoc_set_t const *servers_set = mc_tpld_servers_const (td); for (size_t i = 0; i < servers_set->items_len; i++) { - BSON_ASSERT (bson_in_range_unsigned (uint32_t, i)); + BSON_ASSERT (mcommon_in_range_unsigned (uint32_t, i)); bson_uint32_to_string ((uint32_t) i, &key, str, sizeof str); sd_to_bson (mongoc_set_get_item_const (servers_set, i), &server); BSON_APPEND_DOCUMENT (&servers, key, &server); diff --git a/src/libmongoc/tests/test-mongoc-socket.c b/src/libmongoc/tests/test-mongoc-socket.c index 0e6ddbdc17a..25284efb028 100644 --- a/src/libmongoc/tests/test-mongoc-socket.c +++ b/src/libmongoc/tests/test-mongoc-socket.c @@ -6,6 +6,7 @@ #include "mongoc/mongoc-thread-private.h" #include "mongoc/mongoc-errno-private.h" #include "TestSuite.h" +#include #include "test-libmongoc.h" @@ -231,10 +232,10 @@ static BSON_THREAD_FUN (sendv_test_server, data_) /* Start reading everything off the socket to unblock the client */ do { - ASSERT (bson_in_range_signed (size_t, amount)); + ASSERT (mcommon_in_range_signed (size_t, amount)); const ssize_t r = mongoc_stream_readv (stream, &iov, 1, (size_t) amount, WAIT); if (r > 0) { - ASSERT (bson_in_range_signed (int, r)); + ASSERT (mcommon_in_range_signed (int, r)); amount -= (int) r; } } while (amount > 0); @@ -251,10 +252,10 @@ static BSON_THREAD_FUN (sendv_test_server, data_) bson_mutex_unlock (&data->cond_mutex); do { - ASSERT (bson_in_range_signed (size_t, amount)); + ASSERT (mcommon_in_range_signed (size_t, amount)); const ssize_t r = mongoc_stream_readv (stream, &iov, 1, (size_t) amount, WAIT); if (r > 0) { - ASSERT (bson_in_range_signed (int, r)); + ASSERT (mcommon_in_range_signed (int, r)); amount -= (int) r; } } while (amount > 0); @@ -310,11 +311,11 @@ static BSON_THREAD_FUN (sendv_test_client, data_) const ssize_t r = mongoc_stream_writev (stream, &iov, 1, WAIT); if (r > 0) { - BSON_ASSERT (bson_in_range_signed (int, r)); + BSON_ASSERT (mcommon_in_range_signed (int, r)); amount += (int) r; } - if (bson_cmp_not_equal_su (r, gFourMB)) { + if (mcommon_cmp_not_equal_su (r, gFourMB)) { if (!done) { bson_mutex_lock (&data->cond_mutex); data->amount = amount; diff --git a/src/libmongoc/tests/test-mongoc-topology-reconcile.c b/src/libmongoc/tests/test-mongoc-topology-reconcile.c index 6fe9c1bf0b7..02276f47546 100644 --- a/src/libmongoc/tests/test-mongoc-topology-reconcile.c +++ b/src/libmongoc/tests/test-mongoc-topology-reconcile.c @@ -11,6 +11,7 @@ #include "TestSuite.h" #include "test-conveniences.h" #include "test-libmongoc.h" +#include #undef MONGOC_LOG_DOMAIN #define MONGOC_LOG_DOMAIN "topology-reconcile-test" @@ -401,7 +402,7 @@ test_topology_reconcile_from_handshake (void *ctx) ASSERT_CMPINT (count, ==, 0); /* allow pool to start scanner thread */ - bson_atomic_int_exchange (&topology->scanner_state, MONGOC_TOPOLOGY_SCANNER_OFF, bson_memory_order_seq_cst); + mcommon_atomic_int_exchange (&topology->scanner_state, MONGOC_TOPOLOGY_SCANNER_OFF, mcommon_memory_order_seq_cst); mongoc_client_pool_push (pool, client); client = mongoc_client_pool_pop (pool); diff --git a/src/libmongoc/tests/unified/operation.c b/src/libmongoc/tests/unified/operation.c index edb161c8362..f76b23523df 100644 --- a/src/libmongoc/tests/unified/operation.c +++ b/src/libmongoc/tests/unified/operation.c @@ -25,6 +25,7 @@ #include "util.h" #include "utlist.h" #include "bson-dsl.h" +#include typedef struct { char *name; @@ -554,7 +555,7 @@ operation_create_datakey (test_t *test, operation_t *op, result_t *result, bson_ _mongoc_array_append_val (&arr, key_alt_name); } - BSON_ASSERT (bson_in_range_unsigned (uint32_t, arr.len)); + BSON_ASSERT (mcommon_in_range_unsigned (uint32_t, arr.len)); mongoc_client_encryption_datakey_opts_set_keyaltnames (datakey_opts, arr.data, (uint32_t) arr.len); @@ -2635,13 +2636,13 @@ operation_download (test_t *test, operation_t *op, result_t *result, bson_error_ if (stream) { while ((bytes_read = mongoc_stream_read (stream, buf, sizeof (buf), 1, 0)) > 0) { - ASSERT (bson_in_range_signed (uint32_t, bytes_read)); + ASSERT (mcommon_in_range_signed (uint32_t, bytes_read)); _mongoc_array_append_vals (&all_bytes, buf, (uint32_t) bytes_read); } mongoc_gridfs_bucket_stream_error (stream, &op_error); } - ASSERT (bson_in_range_unsigned (uint32_t, all_bytes.len)); + ASSERT (mcommon_in_range_unsigned (uint32_t, all_bytes.len)); val = bson_val_from_bytes (all_bytes.data, (uint32_t) all_bytes.len); result_from_val_and_reply (result, val, NULL, &op_error); diff --git a/src/libmongoc/tests/unified/runner.c b/src/libmongoc/tests/unified/runner.c index 0c6e7444c61..55e05dbd489 100644 --- a/src/libmongoc/tests/unified/runner.c +++ b/src/libmongoc/tests/unified/runner.c @@ -25,7 +25,7 @@ #include "utlist.h" #include "util.h" #include - +#include typedef struct { const char *file_description; @@ -1332,7 +1332,7 @@ test_check_outcome (test_t *test, bson_error_t *error) static void append_size_t (bson_t *doc, const char *key, size_t value) { - BSON_ASSERT (bson_in_range_unsigned (int64_t, value)); + BSON_ASSERT (mcommon_in_range_unsigned (int64_t, value)); BSON_ASSERT (BSON_APPEND_INT64 (doc, key, (int64_t) value)); } diff --git a/src/libmongoc/tests/unified/util.c b/src/libmongoc/tests/unified/util.c index a481e63c284..6cf2eed6508 100644 --- a/src/libmongoc/tests/unified/util.c +++ b/src/libmongoc/tests/unified/util.c @@ -18,6 +18,7 @@ #include "test-conveniences.h" #include "TestSuite.h" +#include static int cmp_key (const void *a, const void *b) @@ -148,8 +149,8 @@ usecs_since_epoch (void) struct timeval tv; BSON_ASSERT (bson_gettimeofday (&tv) == 0); - BSON_ASSERT (bson_in_range_signed (int64_t, tv.tv_sec)); - BSON_ASSERT (bson_in_range_signed (int64_t, tv.tv_usec)); + BSON_ASSERT (mcommon_in_range_signed (int64_t, tv.tv_sec)); + BSON_ASSERT (mcommon_in_range_signed (int64_t, tv.tv_usec)); const int64_t secs = (int64_t) tv.tv_sec; const int64_t usecs = (int64_t) tv.tv_usec;