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; }
- related to
-
PHPC-714 Implement BSON corpus test suite
- Closed