[CDRIVER-1412] convert json to bson lost integer width Created: 25/Jul/16  Updated: 07/May/18  Resolved: 28/Oct/16

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

Type: Bug Priority: Critical - P2
Reporter: swigger Assignee: Hannes Magnusson
Resolution: Done Votes: 0
Labels: Bug, neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

ALL.


Backwards Compatibility: Fully Compatible

 Description   

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.



 Comments   
Comment by Hannes Magnusson [ 28/Oct/16 ]

Thanks for the detailed diagnosis and fix !

Comment by Githook User [ 28/Oct/16 ]

Author:

{u'username': u'bjori', u'name': u'Hannes Magnusson', u'email': u'bjori@php.net'}

Message: CDRIVER-1412 convert json to bson lost integer width
Branch: master
https://github.com/mongodb/libbson/commit/51847052c4ce48181ebdc550b05fc6ed3fe2235e

Comment by A. Jesse Jiryu Davis [ 25/Jul/16 ]

Good catch, thanks for the report.

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