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

convert json to bson lost integer width

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Critical - P2 Critical - P2
    • 1.5.0
    • Affects Version/s: None
    • Component/s: json, libbson
    • Environment:
      ALL.
    • Fully Compatible

      simple prove code:

              const char * sd = "{\"v\":-1234567890123, \"x\":12345678901234}";
              bson_error_t err;
              bson_t * bs = bson_new_from_json((const uint8_t*)sd, strlen(sd), &err);
              printf("%s\n", bson_as_json(bs, 0));
      

      the code will got:

      { "v" : -1912276171, "x" : 12345678901234 }

      -1234567890123 losts its width, narow down to int32.

      The bug is caused by src/bson/bson-json.c:431:

            if (val <= INT32_MAX) {
               bson_append_int32 (STACK_BSON_CHILD, key, (int)len, (int)val);
            } else {
               bson_append_int64 (STACK_BSON_CHILD, key, (int)len, val);
            }
      

      change
      if (val<=INT32_MAX)
      to
      if (val<=INT32_MAX && val>=INT32_MIN)
      can fix this bug.

            Assignee:
            bjori Hannes Magnusson
            Reporter:
            swigger swigger
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: