Skip to content

Commit 3a3b3f7

Browse files
authored
CDRIVER-2928 assert returned values of bson_init_static (#1351)
* format test-bson.c * assert return value of `bson_init_static` * format bson-dsl.h
1 parent ffdc47e commit 3a3b3f7

File tree

7 files changed

+72
-68
lines changed

7 files changed

+72
-68
lines changed

src/libbson/src/bson/bson-dsl.h

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ enum {
2222
};
2323

2424
#define _bson_thread_local \
25-
BSON_IF_GNU_LIKE (__thread) BSON_IF_MSVC (__declspec(thread))
25+
BSON_IF_GNU_LIKE (__thread) BSON_IF_MSVC (__declspec (thread))
2626

27-
#define _bson_comdat \
28-
BSON_IF_WINDOWS (__declspec(selectany)) \
27+
#define _bson_comdat \
28+
BSON_IF_WINDOWS (__declspec (selectany)) \
2929
BSON_IF_POSIX (__attribute__ ((weak)))
3030

3131
#ifdef __GNUC__
@@ -89,7 +89,7 @@ BSON_IF_GNU_LIKE (_Pragma ("GCC diagnostic ignored \"-Wshadow\""))
8989
#define _bsonDSL_end \
9090
--_bson_dsl_indent; \
9191
} \
92-
else((void) 0)
92+
else ((void) 0)
9393

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

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

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

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

399-
#define _bsonBuild_setKeyToArrayIndex(Idx) \
400-
_bbCtx.key_len = bson_snprintf ( \
401-
_bbCtx.index_key_str, sizeof _bbCtx.index_key_str, "%d",(int) _bbCtx.index); \
399+
#define _bsonBuild_setKeyToArrayIndex(Idx) \
400+
_bbCtx.key_len = bson_snprintf (_bbCtx.index_key_str, \
401+
sizeof _bbCtx.index_key_str, \
402+
"%d", \
403+
(int) _bbCtx.index); \
402404
_bbCtx.key = _bbCtx.index_key_str
403405

404406
/// Handle an element of array()
@@ -611,7 +613,7 @@ BSON_IF_GNU_LIKE (_Pragma ("GCC diagnostic ignored \"-Wshadow\""))
611613
break; \
612614
} \
613615
bson_t inner; \
614-
bson_init_static (&inner, data, len); \
616+
BSON_ASSERT (bson_init_static (&inner, data, len)); \
615617
_bsonVisitEach (inner, __VA_ARGS__); \
616618
} while (0); \
617619
_bsonDSL_end
@@ -631,7 +633,7 @@ BSON_IF_GNU_LIKE (_Pragma ("GCC diagnostic ignored \"-Wshadow\""))
631633
break; \
632634
} \
633635
bson_t inner; \
634-
bson_init_static (&inner, data, len); \
636+
BSON_ASSERT (bson_init_static (&inner, data, len)); \
635637
_bsonParse (inner, __VA_ARGS__); \
636638
} while (0);
637639

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

719-
#define _bsonParseOperation_find(Predicate, ...) \
720-
_bsonDSL_begin ("find(%s)", _bsonDSL_str (Predicate)); \
721-
_bpFoundElement = false; \
722-
_bsonVisitEach ( \
723-
*_bpDoc, \
724-
if (Predicate, \
725-
then (do(_bsonParseMarkVisited (bsonVisitContext.index); \
726-
_bpFoundElement = true), \
727-
__VA_ARGS__, \
728-
break))); \
729-
if (!_bpFoundElement && !bsonParseError) { \
730-
_bsonDSLDebug ("[not found]"); \
731-
} \
721+
#define _bsonParseOperation_find(Predicate, ...) \
722+
_bsonDSL_begin ("find(%s)", _bsonDSL_str (Predicate)); \
723+
_bpFoundElement = false; \
724+
_bsonVisitEach ( \
725+
*_bpDoc, \
726+
if (Predicate, \
727+
then (do (_bsonParseMarkVisited (bsonVisitContext.index); \
728+
_bpFoundElement = true), \
729+
__VA_ARGS__, \
730+
break))); \
731+
if (!_bpFoundElement && !bsonParseError) { \
732+
_bsonDSLDebug ("[not found]"); \
733+
} \
732734
_bsonDSL_end
733735

734736
#define _bsonParseOperation_require(Predicate, ...) \
@@ -737,8 +739,8 @@ BSON_IF_GNU_LIKE (_Pragma ("GCC diagnostic ignored \"-Wshadow\""))
737739
_bsonVisitEach ( \
738740
*_bpDoc, \
739741
if (Predicate, \
740-
then (do(_bsonParseMarkVisited (bsonVisitContext.index); \
741-
_bpFoundElement = true), \
742+
then (do (_bsonParseMarkVisited (bsonVisitContext.index); \
743+
_bpFoundElement = true), \
742744
__VA_ARGS__, \
743745
break))); \
744746
if (!_bpFoundElement && !bsonParseError) { \
@@ -1070,7 +1072,7 @@ _bson_dsl_iter_as_doc (bson_t *into, const bson_iter_t *it)
10701072
bson_iter_document (it, &len, &dataptr);
10711073
}
10721074
if (dataptr) {
1073-
bson_init_static (into, dataptr, len);
1075+
BSON_ASSERT (bson_init_static (into, dataptr, len));
10741076
}
10751077
}
10761078

@@ -1140,7 +1142,7 @@ _bson_dsl_dupPath (char **into)
11401142
char *prev = acc;
11411143
if (ctx->parent && BSON_ITER_HOLDS_ARRAY (&ctx->parent->iter)) {
11421144
// We're an array element
1143-
acc = bson_strdup_printf ("[%d]%s", (int)ctx->index, prev);
1145+
acc = bson_strdup_printf ("[%d]%s", (int) ctx->index, prev);
11441146
} else {
11451147
// We're a document element
11461148
acc = bson_strdup_printf (".%s%s", bson_iter_key (&ctx->iter), prev);

src/libbson/tests/test-bson.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,7 @@ test_bson_init_static (void)
13421342
static const uint8_t data[5] = {5};
13431343
bson_t b;
13441344

1345-
bson_init_static (&b, data, sizeof data);
1345+
ASSERT (bson_init_static (&b, data, sizeof data));
13461346
BSON_ASSERT ((b.flags & BSON_FLAG_RDONLY));
13471347
bson_destroy (&b);
13481348
}
@@ -2519,21 +2519,21 @@ static void
25192519
test_bson_dsl_parse (void)
25202520
{
25212521
// Do nothing:
2522-
bsonParse (*TMP_BSON_FROM_JSON ({}), do());
2522+
bsonParse (*TMP_BSON_FROM_JSON ({}), do ());
25232523
BSON_ASSERT (!bsonParseError);
25242524

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

25292529
// Error is reset on each entry
2530-
bsonParse (*TMP_BSON_FROM_JSON ({}), do());
2530+
bsonParse (*TMP_BSON_FROM_JSON ({}), do ());
25312531
BSON_ASSERT (!bsonParseError);
25322532

25332533
// Find an element
25342534
bson_t *simple_foo_bar = TMP_BSON_FROM_JSON ({"foo" : "bar"});
25352535
bool found = false;
2536-
bsonParse (*simple_foo_bar, find (key ("foo"), do(found = true)));
2536+
bsonParse (*simple_foo_bar, find (key ("foo"), do (found = true)));
25372537
BSON_ASSERT (found);
25382538

25392539
// Store a reference to the string
@@ -2545,40 +2545,40 @@ test_bson_dsl_parse (void)
25452545
found = false;
25462546
bool not_found = false;
25472547
bsonParse (*simple_foo_bar,
2548-
find (key ("bad"), do(found = true)),
2549-
else(do(not_found = true)));
2548+
find (key ("bad"), do (found = true)),
2549+
else (do (not_found = true)));
25502550
BSON_ASSERT (!found);
25512551
BSON_ASSERT (not_found);
25522552

25532553
// We can find two items
25542554
int32_t a = 0, b = 0;
25552555
bsonParse (*TMP_BSON_FROM_JSON ({"foo" : 1729, "bar" : 42}),
2556-
find (key ("foo"), do(a = bsonAs (int32))),
2557-
find (key ("bar"), do(b = bsonAs (int32))));
2556+
find (key ("foo"), do (a = bsonAs (int32))),
2557+
find (key ("bar"), do (b = bsonAs (int32))));
25582558
ASSERT_CMPINT (a, ==, 1729);
25592559
ASSERT_CMPINT (b, ==, 42);
25602560

25612561
// Wrong types are zeroed
25622562
a = 91;
25632563
found = false;
25642564
bsonParse (*TMP_BSON_FROM_JSON ({"foo" : "string"}),
2565-
find (key ("foo"), do(found = true; a = bsonAs (int32))));
2565+
find (key ("foo"), do (found = true; a = bsonAs (int32))));
25662566
BSON_ASSERT (found);
25672567
ASSERT_CMPINT (a, ==, 0);
25682568

25692569
// Nested errors do not continue
25702570
found = false;
25712571
bsonParse (*TMP_BSON_FROM_JSON ({"foo" : null, "bar" : null}),
25722572
find (key ("foo"), error ("got foo")),
2573-
find (key ("bar"), do(found = true)));
2573+
find (key ("bar"), do (found = true)));
25742574
ASSERT_CMPSTR (bsonParseError, "got foo");
25752575
BSON_ASSERT (!found);
25762576

25772577
// Halting does not continue
25782578
found = false;
25792579
bsonParse (*TMP_BSON_FROM_JSON ({"foo" : null, "bar" : null}),
25802580
find (key ("foo"), halt),
2581-
find (key ("bar"), do(found = true)));
2581+
find (key ("bar"), do (found = true)));
25822582
BSON_ASSERT (!bsonParseError);
25832583
BSON_ASSERT (!found);
25842584

@@ -2587,14 +2587,14 @@ test_bson_dsl_parse (void)
25872587
b = 0;
25882588
bsonParse (*TMP_BSON_FROM_JSON ({"foo" : 1, "bar" : 2}),
25892589
if (a == 812,
2590-
then (find (key ("foo"), do(b = bsonAs (int32)))),
2591-
else(find (key ("bar"), do(b = bsonAs (int32))))));
2590+
then (find (key ("foo"), do (b = bsonAs (int32)))),
2591+
else (find (key ("bar"), do (b = bsonAs (int32))))));
25922592
ASSERT_CMPINT (b, ==, 1);
25932593
a = 4;
25942594
bsonParse (*TMP_BSON_FROM_JSON ({"foo" : 1, "bar" : 2}),
25952595
if (a == 812,
2596-
then (find (key ("foo"), do(b = bsonAs (int32)))),
2597-
else(find (key ("bar"), do(b = bsonAs (int32))))));
2596+
then (find (key ("foo"), do (b = bsonAs (int32)))),
2597+
else (find (key ("bar"), do (b = bsonAs (int32))))));
25982598
ASSERT_CMPINT (b, ==, 2);
25992599

26002600
bson_t tmp = BSON_INITIALIZER;
@@ -2603,7 +2603,7 @@ test_bson_dsl_parse (void)
26032603
}
26042604
BSON_APPEND_BOOL (&tmp, "final", true);
26052605
int unvisited = 0;
2606-
bsonParse (tmp, find (key ("final"), nop), visitOthers (do(++unvisited)));
2606+
bsonParse (tmp, find (key ("final"), nop), visitOthers (do (++unvisited)));
26072607
ASSERT_CMPINT (unvisited, ==, 1024);
26082608
bson_destroy (&tmp);
26092609
}
@@ -2613,15 +2613,15 @@ test_bson_dsl_visit (void)
26132613
{
26142614
// Count elements
26152615
int count = 0;
2616-
bsonVisitEach (*TMP_BSON_FROM_JSON ({"foo" : 1, "bar" : 1}), do(++count));
2616+
bsonVisitEach (*TMP_BSON_FROM_JSON ({"foo" : 1, "bar" : 1}), do (++count));
26172617
ASSERT_CMPINT (count, ==, 2);
26182618

26192619
// Branch on keys
26202620
int foo_val = 0;
26212621
int bar_val = 0;
26222622
bsonVisitEach (*TMP_BSON_FROM_JSON ({"foo" : 61, "bar" : 951}),
2623-
if (key ("foo"), then (do(foo_val = bsonAs (int32)))),
2624-
if (key ("bar"), then (do(bar_val = bsonAs (int32)))));
2623+
if (key ("foo"), then (do (foo_val = bsonAs (int32)))),
2624+
if (key ("bar"), then (do (bar_val = bsonAs (int32)))));
26252625
ASSERT_CMPINT (foo_val, ==, 61);
26262626
ASSERT_CMPINT (bar_val, ==, 951);
26272627

@@ -2630,7 +2630,7 @@ test_bson_dsl_visit (void)
26302630
bsonVisitEach (*TMP_BSON_FROM_JSON ({"foo" : {"bar" : 42}}),
26312631
storeDocRef (subdoc));
26322632
bar_val = 0;
2633-
bsonVisitEach (subdoc, do(bar_val = bsonAs (int32)));
2633+
bsonVisitEach (subdoc, do (bar_val = bsonAs (int32)));
26342634
ASSERT_CMPINT (bar_val, ==, 42);
26352635

26362636
// Visit subdocs directly
@@ -2692,21 +2692,21 @@ test_bson_dsl_predicate (void)
26922692
require (type (doc)),
26932693
visitEach (if (lastElement,
26942694
then (require (key ("b")), require (type (utf8))),
2695-
else(require (key ("a")), require (type (null)))))),
2695+
else (require (key ("a")), require (type (null)))))),
26962696
require (
26972697
key ("with_last"),
26982698
visitEach (case (when (key ("a"), require (type (null))),
26992699
when (key ("b"), require (strEqual ("lastElement"))),
2700-
else(do(abort ()))))),
2700+
else (do (abort ()))))),
27012701
require (key ("string"),
2702-
case (when (strEqual ("goodbye"), do(abort ())),
2702+
case (when (strEqual ("goodbye"), do (abort ())),
27032703
when (strEqual ("hello"), nop),
27042704
// Not eached since the prior case matched:
2705-
when (strEqual ("hello"), do(abort ())),
2706-
else(do(abort ())))),
2705+
when (strEqual ("hello"), do (abort ())),
2706+
else (do (abort ())))),
27072707
visitOthers (if (key ("unhandled"),
2708-
then (do(saw_other = true)),
2709-
else(do(abort ())))));
2708+
then (do (saw_other = true)),
2709+
else (do (abort ())))));
27102710
BSON_ASSERT (saw_other);
27112711
}
27122712

@@ -2738,7 +2738,7 @@ static void
27382738
test_bson_dsl_build (void)
27392739
{
27402740
// Create a very simple empty document
2741-
bsonBuildDecl (doc, do());
2741+
bsonBuildDecl (doc, do ());
27422742
BSON_ASSERT (!bsonBuildError);
27432743
ASSERT_BSON_EQUAL (doc, {});
27442744
bson_destroy (&doc);
@@ -2756,8 +2756,8 @@ test_bson_dsl_build (void)
27562756
bson_destroy (&doc);
27572757

27582758
// Conditional insert
2759-
bsonBuild (doc,
2760-
if (0, then (kv ("never", null)), else(kv ("truth", int32 (1)))));
2759+
bsonBuild (
2760+
doc, if (0, then (kv ("never", null)), else (kv ("truth", int32 (1)))));
27612761
ASSERT_BSON_EQUAL (doc, {"truth" : 1});
27622762
bson_destroy (&doc);
27632763

src/libmongoc/src/mongoc/mongoc-client-side-encryption.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,8 @@ _prep_for_auto_encryption (const mongoc_cmd_t *cmd, bson_t *out)
10531053
{
10541054
/* If there is no type=1 payload, return the command unchanged. */
10551055
if (!cmd->payload || !cmd->payload_size) {
1056-
bson_init_static (out, bson_get_data (cmd->command), cmd->command->len);
1056+
BSON_ASSERT (bson_init_static (
1057+
out, bson_get_data (cmd->command), cmd->command->len));
10571058
return;
10581059
}
10591060

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,7 @@ _mongoc_collection_create_index_if_not_exists (mongoc_collection_t *collection,
14041404
}
14051405

14061406
bson_iter_document (&iter, &data_len, &data);
1407-
bson_init_static (&inner_doc, data, data_len);
1407+
BSON_ASSERT (bson_init_static (&inner_doc, data, data_len));
14081408

14091409
if (_mongoc_collection_index_keys_equal (keys, &inner_doc)) {
14101410
index_exists = true;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,8 @@ mongoc_server_description_handle_hello (mongoc_server_description_t *sd,
728728
}
729729

730730
bson_iter_document (&iter, &len, &bytes);
731-
bson_init_static (&incoming_topology_version, bytes, len);
731+
BSON_ASSERT (
732+
bson_init_static (&incoming_topology_version, bytes, len));
732733
mongoc_server_description_set_topology_version (
733734
sd, &incoming_topology_version);
734735
bson_destroy (&incoming_topology_version);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2204,7 +2204,7 @@ mongoc_topology_description_handle_hello (
22042204
uint32_t len;
22052205

22062206
bson_iter_document (&iter, &len, &bytes);
2207-
bson_init_static (&incoming_topology_version, bytes, len);
2207+
BSON_ASSERT (bson_init_static (&incoming_topology_version, bytes, len));
22082208

22092209
if (mongoc_server_description_topology_version_cmp (
22102210
&sd->topology_version, &incoming_topology_version) == 1) {

src/libmongoc/src/mongoc/mongoc-topology.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1704,7 +1704,7 @@ _find_topology_version (const bson_t *reply, bson_t *topology_version)
17041704
return;
17051705
}
17061706
bson_iter_document (&iter, &len, &bytes);
1707-
bson_init_static (topology_version, bytes, len);
1707+
BSON_ASSERT (bson_init_static (topology_version, bytes, len));
17081708
}
17091709

17101710

0 commit comments

Comments
 (0)