Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-14936

Remove undefined behavior around BSONObj::Holder

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.7.6
    • Affects Version/s: None
    • Component/s: Internal Code
    • None
    • Fully Compatible

      Currently, we assume that by reserving four bytes at the beginning of a buffer, we are permitted to re-interpret cast that region of memory to a BSONObj::Holder.

      In fact, it is only legal to reinterpret_cast a region of memory to a class type if the memory originally held a properly constructed object of that type.

      An additional problem is that the region of memory owned by the BSONObjBuilder may be realloc'ed. We cannot place a class at the beginning of the region and assume that it will survive realloc.

      Instead, we should refactor the relationship between BSONObjBuilder and BSONObj so that the builder reserves but does not initialize a prefix region of appropriate size and alignment to hold a BSONObj::Holder, and then BSONObj placement new's a Holder into that region when it takes ownership of the buffer.

            andrew.morrow@mongodb.com Andrew Morrow (Inactive)
            andrew.morrow@mongodb.com Andrew Morrow (Inactive)
            0 Vote for this issue
            3 Start watching this issue