Skip to content

CDRIVER-2928 assert returned values of bson_init_static #1351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 35 additions & 33 deletions src/libbson/src/bson/bson-dsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ enum {
};

#define _bson_thread_local \
BSON_IF_GNU_LIKE (__thread) BSON_IF_MSVC (__declspec(thread))
BSON_IF_GNU_LIKE (__thread) BSON_IF_MSVC (__declspec (thread))

#define _bson_comdat \
BSON_IF_WINDOWS (__declspec(selectany)) \
#define _bson_comdat \
BSON_IF_WINDOWS (__declspec (selectany)) \
BSON_IF_POSIX (__attribute__ ((weak)))

#ifdef __GNUC__
Expand Down Expand Up @@ -89,7 +89,7 @@ BSON_IF_GNU_LIKE (_Pragma ("GCC diagnostic ignored \"-Wshadow\""))
#define _bsonDSL_end \
--_bson_dsl_indent; \
} \
else((void) 0)
else ((void) 0)

/**
* @brief Expands to a call to bson_append_{Kind}, with the three first
Expand Down Expand Up @@ -286,7 +286,7 @@ BSON_IF_GNU_LIKE (_Pragma ("GCC diagnostic ignored \"-Wshadow\""))
const bool _bvHalt = false; /* Required for _bsonVisitEach() */ \
_bsonVisitEach ( \
OtherBSON, \
if (Pred, then (do(_bsonDocOperation_iterElement (bsonVisitIter))))); \
if (Pred, then (do (_bsonDocOperation_iterElement (bsonVisitIter))))); \
_bsonDSL_end

#define _bsonDocOperation_insertFromIter(Iter, Pred) \
Expand All @@ -312,16 +312,16 @@ BSON_IF_GNU_LIKE (_Pragma ("GCC diagnostic ignored \"-Wshadow\""))

/// Insert the given BSON document into the parent array. Keys of the given
/// document are discarded and it is treated as an array of values.
#define _bsonArrayOperation_insert(OtherArr, Pred) \
_bsonDSL_begin ("Insert other array: [%s]", _bsonDSL_str (OtherArr)); \
_bsonVisitEach ( \
OtherArr, \
if (Pred, then (do(_bsonArrayOperation_iterValue (bsonVisitIter))))); \
#define _bsonArrayOperation_insert(OtherArr, Pred) \
_bsonDSL_begin ("Insert other array: [%s]", _bsonDSL_str (OtherArr)); \
_bsonVisitEach ( \
OtherArr, \
if (Pred, then (do (_bsonArrayOperation_iterValue (bsonVisitIter))))); \
_bsonDSL_end

#define _bsonArrayAppendValue(ValueOperation) \
_bsonDSL_begin ("[%d] => [%s]", \
(int) bsonBuildContext.index, \
(int) bsonBuildContext.index, \
_bsonDSL_strElide (30, ValueOperation)); \
/* Set the doc key to the array index as a string: */ \
_bsonBuild_setKeyToArrayIndex (bsonBuildContext.index); \
Expand Down Expand Up @@ -396,9 +396,11 @@ BSON_IF_GNU_LIKE (_Pragma ("GCC diagnostic ignored \"-Wshadow\""))
_bsonValueOperationIf_##Else; \
}

#define _bsonBuild_setKeyToArrayIndex(Idx) \
_bbCtx.key_len = bson_snprintf ( \
_bbCtx.index_key_str, sizeof _bbCtx.index_key_str, "%d",(int) _bbCtx.index); \
#define _bsonBuild_setKeyToArrayIndex(Idx) \
_bbCtx.key_len = bson_snprintf (_bbCtx.index_key_str, \
sizeof _bbCtx.index_key_str, \
"%d", \
(int) _bbCtx.index); \
_bbCtx.key = _bbCtx.index_key_str

/// Handle an element of array()
Expand Down Expand Up @@ -611,7 +613,7 @@ BSON_IF_GNU_LIKE (_Pragma ("GCC diagnostic ignored \"-Wshadow\""))
break; \
} \
bson_t inner; \
bson_init_static (&inner, data, len); \
BSON_ASSERT (bson_init_static (&inner, data, len)); \
_bsonVisitEach (inner, __VA_ARGS__); \
} while (0); \
_bsonDSL_end
Expand All @@ -631,7 +633,7 @@ BSON_IF_GNU_LIKE (_Pragma ("GCC diagnostic ignored \"-Wshadow\""))
break; \
} \
bson_t inner; \
bson_init_static (&inner, data, len); \
BSON_ASSERT (bson_init_static (&inner, data, len)); \
_bsonParse (inner, __VA_ARGS__); \
} while (0);

Expand Down Expand Up @@ -716,19 +718,19 @@ BSON_IF_GNU_LIKE (_Pragma ("GCC diagnostic ignored \"-Wshadow\""))
(NthInt < _bpNumVisitBitInts && \
(_bpVisitBits[NthInt] & (UINT64_C (1) << NthBit)))

#define _bsonParseOperation_find(Predicate, ...) \
_bsonDSL_begin ("find(%s)", _bsonDSL_str (Predicate)); \
_bpFoundElement = false; \
_bsonVisitEach ( \
*_bpDoc, \
if (Predicate, \
then (do(_bsonParseMarkVisited (bsonVisitContext.index); \
_bpFoundElement = true), \
__VA_ARGS__, \
break))); \
if (!_bpFoundElement && !bsonParseError) { \
_bsonDSLDebug ("[not found]"); \
} \
#define _bsonParseOperation_find(Predicate, ...) \
_bsonDSL_begin ("find(%s)", _bsonDSL_str (Predicate)); \
_bpFoundElement = false; \
_bsonVisitEach ( \
*_bpDoc, \
if (Predicate, \
then (do (_bsonParseMarkVisited (bsonVisitContext.index); \
_bpFoundElement = true), \
__VA_ARGS__, \
break))); \
if (!_bpFoundElement && !bsonParseError) { \
_bsonDSLDebug ("[not found]"); \
} \
_bsonDSL_end

#define _bsonParseOperation_require(Predicate, ...) \
Expand All @@ -737,8 +739,8 @@ BSON_IF_GNU_LIKE (_Pragma ("GCC diagnostic ignored \"-Wshadow\""))
_bsonVisitEach ( \
*_bpDoc, \
if (Predicate, \
then (do(_bsonParseMarkVisited (bsonVisitContext.index); \
_bpFoundElement = true), \
then (do (_bsonParseMarkVisited (bsonVisitContext.index); \
_bpFoundElement = true), \
__VA_ARGS__, \
break))); \
if (!_bpFoundElement && !bsonParseError) { \
Expand Down Expand Up @@ -1070,7 +1072,7 @@ _bson_dsl_iter_as_doc (bson_t *into, const bson_iter_t *it)
bson_iter_document (it, &len, &dataptr);
}
if (dataptr) {
bson_init_static (into, dataptr, len);
BSON_ASSERT (bson_init_static (into, dataptr, len));
}
}

Expand Down Expand Up @@ -1140,7 +1142,7 @@ _bson_dsl_dupPath (char **into)
char *prev = acc;
if (ctx->parent && BSON_ITER_HOLDS_ARRAY (&ctx->parent->iter)) {
// We're an array element
acc = bson_strdup_printf ("[%d]%s", (int)ctx->index, prev);
acc = bson_strdup_printf ("[%d]%s", (int) ctx->index, prev);
} else {
// We're a document element
acc = bson_strdup_printf (".%s%s", bson_iter_key (&ctx->iter), prev);
Expand Down
60 changes: 30 additions & 30 deletions src/libbson/tests/test-bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@ test_bson_init_static (void)
static const uint8_t data[5] = {5};
bson_t b;

bson_init_static (&b, data, sizeof data);
ASSERT (bson_init_static (&b, data, sizeof data));
BSON_ASSERT ((b.flags & BSON_FLAG_RDONLY));
bson_destroy (&b);
}
Expand Down Expand Up @@ -2519,21 +2519,21 @@ static void
test_bson_dsl_parse (void)
{
// Do nothing:
bsonParse (*TMP_BSON_FROM_JSON ({}), do());
bsonParse (*TMP_BSON_FROM_JSON ({}), do ());
BSON_ASSERT (!bsonParseError);

// Generate an error
bsonParse (*TMP_BSON_FROM_JSON ({}), error ("failed 1"));
ASSERT_CMPSTR (bsonParseError, "failed 1");

// Error is reset on each entry
bsonParse (*TMP_BSON_FROM_JSON ({}), do());
bsonParse (*TMP_BSON_FROM_JSON ({}), do ());
BSON_ASSERT (!bsonParseError);

// Find an element
bson_t *simple_foo_bar = TMP_BSON_FROM_JSON ({"foo" : "bar"});
bool found = false;
bsonParse (*simple_foo_bar, find (key ("foo"), do(found = true)));
bsonParse (*simple_foo_bar, find (key ("foo"), do (found = true)));
BSON_ASSERT (found);

// Store a reference to the string
Expand All @@ -2545,40 +2545,40 @@ test_bson_dsl_parse (void)
found = false;
bool not_found = false;
bsonParse (*simple_foo_bar,
find (key ("bad"), do(found = true)),
else(do(not_found = true)));
find (key ("bad"), do (found = true)),
else (do (not_found = true)));
BSON_ASSERT (!found);
BSON_ASSERT (not_found);

// We can find two items
int32_t a = 0, b = 0;
bsonParse (*TMP_BSON_FROM_JSON ({"foo" : 1729, "bar" : 42}),
find (key ("foo"), do(a = bsonAs (int32))),
find (key ("bar"), do(b = bsonAs (int32))));
find (key ("foo"), do (a = bsonAs (int32))),
find (key ("bar"), do (b = bsonAs (int32))));
ASSERT_CMPINT (a, ==, 1729);
ASSERT_CMPINT (b, ==, 42);

// Wrong types are zeroed
a = 91;
found = false;
bsonParse (*TMP_BSON_FROM_JSON ({"foo" : "string"}),
find (key ("foo"), do(found = true; a = bsonAs (int32))));
find (key ("foo"), do (found = true; a = bsonAs (int32))));
BSON_ASSERT (found);
ASSERT_CMPINT (a, ==, 0);

// Nested errors do not continue
found = false;
bsonParse (*TMP_BSON_FROM_JSON ({"foo" : null, "bar" : null}),
find (key ("foo"), error ("got foo")),
find (key ("bar"), do(found = true)));
find (key ("bar"), do (found = true)));
ASSERT_CMPSTR (bsonParseError, "got foo");
BSON_ASSERT (!found);

// Halting does not continue
found = false;
bsonParse (*TMP_BSON_FROM_JSON ({"foo" : null, "bar" : null}),
find (key ("foo"), halt),
find (key ("bar"), do(found = true)));
find (key ("bar"), do (found = true)));
BSON_ASSERT (!bsonParseError);
BSON_ASSERT (!found);

Expand All @@ -2587,14 +2587,14 @@ test_bson_dsl_parse (void)
b = 0;
bsonParse (*TMP_BSON_FROM_JSON ({"foo" : 1, "bar" : 2}),
if (a == 812,
then (find (key ("foo"), do(b = bsonAs (int32)))),
else(find (key ("bar"), do(b = bsonAs (int32))))));
then (find (key ("foo"), do (b = bsonAs (int32)))),
else (find (key ("bar"), do (b = bsonAs (int32))))));
ASSERT_CMPINT (b, ==, 1);
a = 4;
bsonParse (*TMP_BSON_FROM_JSON ({"foo" : 1, "bar" : 2}),
if (a == 812,
then (find (key ("foo"), do(b = bsonAs (int32)))),
else(find (key ("bar"), do(b = bsonAs (int32))))));
then (find (key ("foo"), do (b = bsonAs (int32)))),
else (find (key ("bar"), do (b = bsonAs (int32))))));
ASSERT_CMPINT (b, ==, 2);

bson_t tmp = BSON_INITIALIZER;
Expand All @@ -2603,7 +2603,7 @@ test_bson_dsl_parse (void)
}
BSON_APPEND_BOOL (&tmp, "final", true);
int unvisited = 0;
bsonParse (tmp, find (key ("final"), nop), visitOthers (do(++unvisited)));
bsonParse (tmp, find (key ("final"), nop), visitOthers (do (++unvisited)));
ASSERT_CMPINT (unvisited, ==, 1024);
bson_destroy (&tmp);
}
Expand All @@ -2613,15 +2613,15 @@ test_bson_dsl_visit (void)
{
// Count elements
int count = 0;
bsonVisitEach (*TMP_BSON_FROM_JSON ({"foo" : 1, "bar" : 1}), do(++count));
bsonVisitEach (*TMP_BSON_FROM_JSON ({"foo" : 1, "bar" : 1}), do (++count));
ASSERT_CMPINT (count, ==, 2);

// Branch on keys
int foo_val = 0;
int bar_val = 0;
bsonVisitEach (*TMP_BSON_FROM_JSON ({"foo" : 61, "bar" : 951}),
if (key ("foo"), then (do(foo_val = bsonAs (int32)))),
if (key ("bar"), then (do(bar_val = bsonAs (int32)))));
if (key ("foo"), then (do (foo_val = bsonAs (int32)))),
if (key ("bar"), then (do (bar_val = bsonAs (int32)))));
ASSERT_CMPINT (foo_val, ==, 61);
ASSERT_CMPINT (bar_val, ==, 951);

Expand All @@ -2630,7 +2630,7 @@ test_bson_dsl_visit (void)
bsonVisitEach (*TMP_BSON_FROM_JSON ({"foo" : {"bar" : 42}}),
storeDocRef (subdoc));
bar_val = 0;
bsonVisitEach (subdoc, do(bar_val = bsonAs (int32)));
bsonVisitEach (subdoc, do (bar_val = bsonAs (int32)));
ASSERT_CMPINT (bar_val, ==, 42);

// Visit subdocs directly
Expand Down Expand Up @@ -2692,21 +2692,21 @@ test_bson_dsl_predicate (void)
require (type (doc)),
visitEach (if (lastElement,
then (require (key ("b")), require (type (utf8))),
else(require (key ("a")), require (type (null)))))),
else (require (key ("a")), require (type (null)))))),
require (
key ("with_last"),
visitEach (case (when (key ("a"), require (type (null))),
when (key ("b"), require (strEqual ("lastElement"))),
else(do(abort ()))))),
else (do (abort ()))))),
require (key ("string"),
case (when (strEqual ("goodbye"), do(abort ())),
case (when (strEqual ("goodbye"), do (abort ())),
when (strEqual ("hello"), nop),
// Not eached since the prior case matched:
when (strEqual ("hello"), do(abort ())),
else(do(abort ())))),
when (strEqual ("hello"), do (abort ())),
else (do (abort ())))),
visitOthers (if (key ("unhandled"),
then (do(saw_other = true)),
else(do(abort ())))));
then (do (saw_other = true)),
else (do (abort ())))));
BSON_ASSERT (saw_other);
}

Expand Down Expand Up @@ -2738,7 +2738,7 @@ static void
test_bson_dsl_build (void)
{
// Create a very simple empty document
bsonBuildDecl (doc, do());
bsonBuildDecl (doc, do ());
BSON_ASSERT (!bsonBuildError);
ASSERT_BSON_EQUAL (doc, {});
bson_destroy (&doc);
Expand All @@ -2756,8 +2756,8 @@ test_bson_dsl_build (void)
bson_destroy (&doc);

// Conditional insert
bsonBuild (doc,
if (0, then (kv ("never", null)), else(kv ("truth", int32 (1)))));
bsonBuild (
doc, if (0, then (kv ("never", null)), else (kv ("truth", int32 (1)))));
ASSERT_BSON_EQUAL (doc, {"truth" : 1});
bson_destroy (&doc);

Expand Down
3 changes: 2 additions & 1 deletion src/libmongoc/src/mongoc/mongoc-client-side-encryption.c
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,8 @@ _prep_for_auto_encryption (const mongoc_cmd_t *cmd, bson_t *out)
{
/* If there is no type=1 payload, return the command unchanged. */
if (!cmd->payload || !cmd->payload_size) {
bson_init_static (out, bson_get_data (cmd->command), cmd->command->len);
BSON_ASSERT (bson_init_static (
out, bson_get_data (cmd->command), cmd->command->len));
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/libmongoc/src/mongoc/mongoc-collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1404,7 +1404,7 @@ _mongoc_collection_create_index_if_not_exists (mongoc_collection_t *collection,
}

bson_iter_document (&iter, &data_len, &data);
bson_init_static (&inner_doc, data, data_len);
BSON_ASSERT (bson_init_static (&inner_doc, data, data_len));

if (_mongoc_collection_index_keys_equal (keys, &inner_doc)) {
index_exists = true;
Expand Down
3 changes: 2 additions & 1 deletion src/libmongoc/src/mongoc/mongoc-server-description.c
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,8 @@ mongoc_server_description_handle_hello (mongoc_server_description_t *sd,
}

bson_iter_document (&iter, &len, &bytes);
bson_init_static (&incoming_topology_version, bytes, len);
BSON_ASSERT (
bson_init_static (&incoming_topology_version, bytes, len));
mongoc_server_description_set_topology_version (
sd, &incoming_topology_version);
bson_destroy (&incoming_topology_version);
Expand Down
2 changes: 1 addition & 1 deletion src/libmongoc/src/mongoc/mongoc-topology-description.c
Original file line number Diff line number Diff line change
Expand Up @@ -2204,7 +2204,7 @@ mongoc_topology_description_handle_hello (
uint32_t len;

bson_iter_document (&iter, &len, &bytes);
bson_init_static (&incoming_topology_version, bytes, len);
BSON_ASSERT (bson_init_static (&incoming_topology_version, bytes, len));

if (mongoc_server_description_topology_version_cmp (
&sd->topology_version, &incoming_topology_version) == 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/libmongoc/src/mongoc/mongoc-topology.c
Original file line number Diff line number Diff line change
Expand Up @@ -1704,7 +1704,7 @@ _find_topology_version (const bson_t *reply, bson_t *topology_version)
return;
}
bson_iter_document (&iter, &len, &bytes);
bson_init_static (topology_version, bytes, len);
BSON_ASSERT (bson_init_static (topology_version, bytes, len));
}


Expand Down