Skip to content

CDRIVER-5754 always write output on error in bson_validate_with_error_and_offset #1767

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
4 commits merged into from
Oct 15, 2024
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
5 changes: 3 additions & 2 deletions src/libbson/src/bson/bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -3385,16 +3385,17 @@ bson_validate_with_error_and_offset (const bson_t *bson,
state.flags = flags;
_bson_validate_internal (bson, &state);

if (state.err_offset > 0) {
if (state.err_offset >= 0) {
if (offset) {
*offset = (size_t) state.err_offset;
}
if (error) {
memcpy (error, &state.error, sizeof *error);
}
return false;
}

return state.err_offset < 0;
return true;
}


Expand Down
13 changes: 13 additions & 0 deletions src/libbson/tests/test-bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,18 @@ test_bson_validate_dbpointer (void)
}


static void
test_bson_validate_with_error_and_offset (void)
{
size_t err_offset = 12345;
bson_error_t err = {67890};
bson_t bson = {0};
ASSERT (!bson_validate_with_error_and_offset (&bson, BSON_VALIDATE_NONE, &err_offset, &err));
ASSERT_CMPSIZE_T (err_offset, ==, 0);
ASSERT_CMPUINT32 (err.domain, !=, 67890); // domain is overwritten.
}


static void
test_bson_validate (void)
{
Expand Down Expand Up @@ -3227,6 +3239,7 @@ test_bson_install (TestSuite *suite)
TestSuite_Add (suite, "/bson/validate/dbref", test_bson_validate_dbref);
TestSuite_Add (suite, "/bson/validate/bool", test_bson_validate_bool);
TestSuite_Add (suite, "/bson/validate/dbpointer", test_bson_validate_dbpointer);
TestSuite_Add (suite, "/bson/validate/with_error_and_offset", test_bson_validate_with_error_and_offset);
TestSuite_Add (suite, "/bson/new_1mm", test_bson_new_1mm);
TestSuite_Add (suite, "/bson/init_1mm", test_bson_init_1mm);
TestSuite_Add (suite, "/bson/build_child", test_bson_build_child);
Expand Down