Skip to content

Commit cf1257c

Browse files
Micah Scott @ MongoDBkevinAlbs
andcommitted
CDRIVER-5754 always write output on error in bson_validate_with_error_and_offset (#1767)
* bson_validate_with_error_and_offset: write outputs when err_offset==0 * regression test, test_bson_validate_with_error_and_offset --------- Co-authored-by: Kevin Albertson <[email protected]>
1 parent ff764e4 commit cf1257c

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/libbson/src/bson/bson.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3321,16 +3321,17 @@ bson_validate_with_error_and_offset (const bson_t *bson,
33213321
state.flags = flags;
33223322
_bson_validate_internal (bson, &state);
33233323

3324-
if (state.err_offset > 0) {
3324+
if (state.err_offset >= 0) {
33253325
if (offset) {
33263326
*offset = (size_t) state.err_offset;
33273327
}
33283328
if (error) {
33293329
memcpy (error, &state.error, sizeof *error);
33303330
}
3331+
return false;
33313332
}
33323333

3333-
return state.err_offset < 0;
3334+
return true;
33343335
}
33353336

33363337

src/libbson/tests/test-bson.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,18 @@ test_bson_validate_dbpointer (void)
11191119
}
11201120

11211121

1122+
static void
1123+
test_bson_validate_with_error_and_offset (void)
1124+
{
1125+
size_t err_offset = 12345;
1126+
bson_error_t err = {67890};
1127+
bson_t bson = {0};
1128+
ASSERT (!bson_validate_with_error_and_offset (&bson, BSON_VALIDATE_NONE, &err_offset, &err));
1129+
ASSERT_CMPSIZE_T (err_offset, ==, 0);
1130+
ASSERT_CMPUINT32 (err.domain, !=, 67890); // domain is overwritten.
1131+
}
1132+
1133+
11221134
static void
11231135
test_bson_validate (void)
11241136
{
@@ -3228,6 +3240,7 @@ test_bson_install (TestSuite *suite)
32283240
TestSuite_Add (suite, "/bson/validate/dbref", test_bson_validate_dbref);
32293241
TestSuite_Add (suite, "/bson/validate/bool", test_bson_validate_bool);
32303242
TestSuite_Add (suite, "/bson/validate/dbpointer", test_bson_validate_dbpointer);
3243+
TestSuite_Add (suite, "/bson/validate/with_error_and_offset", test_bson_validate_with_error_and_offset);
32313244
TestSuite_Add (suite, "/bson/new_1mm", test_bson_new_1mm);
32323245
TestSuite_Add (suite, "/bson/init_1mm", test_bson_init_1mm);
32333246
TestSuite_Add (suite, "/bson/build_child", test_bson_build_child);

0 commit comments

Comments
 (0)