[CDRIVER-1875] libbson allows any non-zero value for boolean type Created: 22/Oct/16  Updated: 15/Nov/16  Resolved: 14/Nov/16

Status: Closed
Project: C Driver
Component/s: libbson
Affects Version/s: None
Fix Version/s: 1.5.0

Type: Bug Priority: Minor - P4
Reporter: David Golden Assignee: A. Jesse Jiryu Davis
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to PHPC-714 Implement BSON corpus test suite Closed

 Description   

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;
}



 Comments   
Comment by Githook User [ 15/Nov/16 ]

Author:

{u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@mongodb.com'}

Message: CDRIVER-1875 validate bool is 0 or 1
Branch: master
https://github.com/mongodb/libbson/commit/94f90f905d13554f832f8ff9444005ac1335d69c

Generated at Wed Feb 07 21:13:29 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.