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

libbson allows any non-zero value for boolean type

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 1.5.0
    • Affects Version/s: None
    • Component/s: libbson
    • None

      The BSON spec defines boolean true as having character 0x01. libbson appears to allow any non-zero value. This test code finds 0xff (i.e. -1) to be true. This invalid bson is not caught by bson_validate.

      #include <stdio.h>
      #include <bson.h>
      
      int main(int argc, char *argv[]) {
          bson_t bson;
          bson_iter_t iter;
          size_t offset;
          const uint8_t data[] = "\x09\x00\x00\x00\x08\x62\x00\xFF\x00";
      
          if (!bson_init_static(&bson, data, 9)) {
              fprintf(stderr, "bson_init_static failed\n");
              return EXIT_FAILURE;
          }
      
          /* Should this should fail for invalid boolean? */
          if (!bson_validate(&bson, '\xff', &offset)) {
              fprintf(stderr, "bson_init_static failed\n");
              return EXIT_FAILURE;
          }
      
          if (!bson_iter_init(&iter, &bson)) {
              fprintf(stderr, "bson_iter_init failed.\n");
              return EXIT_FAILURE;
          }
      
          if (!bson_iter_next(&iter)) {
              fprintf(stderr, "bson_iter_next failed.\n");
              return EXIT_FAILURE;
          }
      
          if (bson_iter_type(&iter) != BSON_TYPE_BOOL) {
              fprintf(stderr, "key isn't boolean.\n");
              return EXIT_FAILURE;
          }
      
          bool b = bson_iter_bool(&iter);
      
          printf(b ? "boolean is true\n" : "boolean is false\n" );
      
          return EXIT_SUCCESS;
      }
      

            Assignee:
            jesse@mongodb.com A. Jesse Jiryu Davis
            Reporter:
            david.golden@mongodb.com David Golden
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: