Skip to content

CDRIVER-4678 Fix $code and $dbPointer array parsing #1356

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 3 commits into from
Jul 26, 2023

Conversation

joshbsiegel
Copy link
Contributor

@joshbsiegel joshbsiegel commented Jul 24, 2023

Summary

This PR will fix an error when parsing an array with $code or $dbPointer where the implicit keys (0, 1, 2, etc.) would not get correctly set on the values when converting to BSON.

Background

Previously, when parsing JSON such as

 [{"$code" : "A"}]

the BSON outputted would not correctly attach a key of 0 to the value of {"$code" : "A"}. The same issue was caused when attempting to parse JSON with $dbpointer.

Fix

The $code and $dbPointer handlers in bson-json.c were the only two types that called _bson_json_buf_set to save the value of the parent key (e.g. {"key": {"$code" : "A"}}, where we want to save "key" somewhere). These function calls were made by passing through bson->key_buf.buf as const void *from. However, bson->key_buf.buf wasn't set to 0, 1, 2, etc., bson->key was. By passing through bson->key instead and still using bson->key_buf.len as the length, the implicit keys will get set correctly.
Although mixing key with key_buf seems unorthodox, this is a practice done elsewhere in the codebase:

STACK_PUSH_DOC (bson_append_document_begin (STACK_BSON_PARENT,

What's New

  • Fixed calls to _bson_json_buf_set by passing through bson->key
  • Added tests for $code and $dbPointer arrays with 1 and 2 elements

@joshbsiegel joshbsiegel marked this pull request as draft July 24, 2023 20:02
@joshbsiegel joshbsiegel requested a review from kevinAlbs July 24, 2023 21:02
@joshbsiegel joshbsiegel marked this pull request as ready for review July 24, 2023 21:14
Copy link
Collaborator

@kevinAlbs kevinAlbs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with comment removed.

Although mixing key with key_buf seems unorthodox

I think it is expected to use key to refer to the key string. Consider this call:

      bson->key_buf.len = bson_uint32_to_string (
         STACK_I, &bson->key, (char *) bson->key_buf.buf, 12);

bson_uint32_to_string documents that the buffer may or may not be used. The out pointer is always set to the resulting string:

 size_t
  bson_uint32_to_string (uint32_t value,
                         const char **strptr,
                         char *str,
                         size_t size);

Converts value to a string.

If value is from 0 to 999, it will use a constant string in the data section of the library.

If not, a string will be formatted using str and snprintf().

strptr will always be set. It will either point to str or a constant string. Use this as your key.

Co-authored-by: Kevin Albertson <[email protected]>
@joshbsiegel joshbsiegel merged commit 81631fd into mongodb:master Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants