Skip to content

Commit 4b22616

Browse files
Micah Scott @ MongoDBkevinAlbs
andauthored
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 68d2b7f commit 4b22616

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
@@ -3386,16 +3386,17 @@ bson_validate_with_error_and_offset (const bson_t *bson,
33863386
state.flags = flags;
33873387
_bson_validate_internal (bson, &state);
33883388

3389-
if (state.err_offset > 0) {
3389+
if (state.err_offset >= 0) {
33903390
if (offset) {
33913391
*offset = (size_t) state.err_offset;
33923392
}
33933393
if (error) {
33943394
memcpy (error, &state.error, sizeof *error);
33953395
}
3396+
return false;
33963397
}
33973398

3398-
return state.err_offset < 0;
3399+
return true;
33993400
}
34003401

34013402

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
{
@@ -3227,6 +3239,7 @@ test_bson_install (TestSuite *suite)
32273239
TestSuite_Add (suite, "/bson/validate/dbref", test_bson_validate_dbref);
32283240
TestSuite_Add (suite, "/bson/validate/bool", test_bson_validate_bool);
32293241
TestSuite_Add (suite, "/bson/validate/dbpointer", test_bson_validate_dbpointer);
3242+
TestSuite_Add (suite, "/bson/validate/with_error_and_offset", test_bson_validate_with_error_and_offset);
32303243
TestSuite_Add (suite, "/bson/new_1mm", test_bson_new_1mm);
32313244
TestSuite_Add (suite, "/bson/init_1mm", test_bson_init_1mm);
32323245
TestSuite_Add (suite, "/bson/build_child", test_bson_build_child);

0 commit comments

Comments
 (0)