Uploaded image for project: 'C Driver'
  1. C Driver
  2. CDRIVER-1579

bson_malloc needs same count test as bson_malloc0

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.5.0
    • Affects Version/s: 1.4.2
    • Component/s: libbson
    • Labels:
      None
    • Environment:
      C++ Builder 10.1 Berlin
    • Fully Compatible

      bson_malloc() aborts on a zero length malloc.

      Compilers aren't required to return a ptr for 0 length, they may return NULL instead. Solution is to use same test as used in bson_malloc0 (somebody appears to have looked into this previously but it didn't get applied to bson_malloc).

      This is relevant because there is code in the mongodb c driver that expects to get a valid pointer on a 0 length call to malloc (mongoc_set_for_each() is one example). On a susceptible compiler you can't even get past mongoc_client_new() without the patch applied.

      Corrected version:

      void *
      bson_malloc (size_t num_bytes) /* IN */
      {
         void *mem = NULL;
      
         if (BSON_LIKELY (num_bytes)) {
            if (BSON_UNLIKELY (!(mem = gMemVtable.malloc (num_bytes)))) {
               abort ();
            }
         }
      
         return mem;
      }
      

            Assignee:
            bjori Hannes Magnusson
            Reporter:
            tomschm Tom Schmidt
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: