Uploaded image for project: 'C++ Driver'
  1. C++ Driver
  2. CXX-1246

bsoncxx::builder::stream::single_context should not permit appending of multiple values

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.2.0-rc0
    • Affects Version/s: None
    • Component/s: API, BSON
    • Labels:

      bsoncxx::builder::stream::single_context is written to accept a single value to be appended to the stream, but it is currently possible to append multiple values through use of open_document and open_array:

      using namespace bsoncxx::builder::stream;
      auto doc = document{} << "x" << [](single_context s) {
          s << open_array << 0 << close_array << open_array << 1 << close_array;
      } << finalize;

      The above code currently compiles, and throws an exception with a confusing message "expected a key but found none" at runtime. Instead, this code should not compile.

      The code erroneously compiles due to the fact that the following functions have the wrong return type. The return type of these functions are currently key_context<single_context> and array_context<single_context>, respectively, but they should be key_context<closed_context> and array_context<closed_context> instead.

      • bsoncxx::builder::stream::single_context::operator<<(open_document_type)
      • bsoncxx::builder::stream::single_context::operator<<(open_array_type)

            rassi J Rassi
            rassi J Rassi
            0 Vote for this issue
            1 Start watching this issue