[SERVER-4536] BSONObjBuilder does not handle int64_t on LP64 data model Created: 21/Dec/11  Updated: 05/Jul/16  Resolved: 09/Jun/16

Status: Closed
Project: Core Server
Component/s: Internal Client
Affects Version/s: None
Fix Version/s: 3.3.9

Type: Improvement Priority: Major - P3
Reporter: Balint Szente Assignee: Mathias Stearn
Resolution: Done Votes: 2
Labels: cxxcopy, int64_t, long, longlong, stdint
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Linux x86_64 / g++


Backwards Compatibility: Fully Compatible
Sprint: Integrate+Tuning 16 (06/24/16)
Participants:

 Description   

The following code does not compile on Linux x86_64:

main.cpp

#include <mongo/client/dbclient.h>
 
using namespace mongo;
 
int main() {
    int64_t value = 1;
    BSONObj obj = BSON("value" << value);
}

Output:

In file included from /usr/include/mongo/client/../util/../util/../db/jsobj.h:43:0,
                 from /usr/include/mongo/client/../util/../util/sock.h:25,
                 from /usr/include/mongo/client/../util/message.h:20,
                 from /usr/include/mongo/client/dbclient.h:21,
                 from main.cpp:1:
/usr/include/mongo/client/../util/../util/../db/../bson/bson-inl.h: In member function 'mongo::BSONObjBuilder& mongo::BSONObjBuilderValueStream::operator<<(T) [with T = long int]':
main.cpp:7:19:   instantiated from here
/usr/include/mongo/client/../util/../util/../db/../bson/bson-inl.h:165:9: error: call of overloaded 'append(const char*&, long int&)' is ambiguous
/usr/include/mongo/client/../util/../util/../db/../bson/bson-inl.h:165:9: note: candidates are:
/usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:188:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, bool)
/usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:196:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, int)
/usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:204:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, unsigned int)
/usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:209:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, long long int)
/usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:265:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, double)
/usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:345:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, mongo::Date_t)
/usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:378:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, const char*) <near match>
/usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:378:25: note:   no known conversion for argument 2 from 'long int' to 'const char*'
/usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:382:25: note: mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, const string&) <near match>
/usr/include/mongo/client/../util/../util/../db/../bson/bsonobjbuilder.h:382:25: note:   no known conversion for argument 2 from 'long int' to 'const string& {aka const std::basic_string<char>&}'

The problem is that MongoDB is using long long for 64 bit values. On Linux x86_64 (LP64 data model) long is used for the int64_t and boost::int64_t types and this yields to the compilation error, where long long is not considered the same 64 bit signed type as long.

Possible solutions:

  1. Add the proper appender for the long type only for LP64 data model:

    mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::StringData&, long int);

  2. Avoid these implementation dependent data types and use exact data types from standard C99 integer types. MongoDB depends on Boost, so <boost/integer.hpp> could be used without any problem.

I would recommend the 2nd option, because it is a clean solution. Using int, long, etc. data types might generate several unexpected issues in the future, especially when MongoDB has to be ported to various architectures.



 Comments   
Comment by Githook User [ 09/Jun/16 ]

Author:

{u'username': u'RedBeard0531', u'name': u'Mathias Stearn', u'email': u'mathias@10gen.com'}

Message: SERVER-4536 Support appending an int64_t to a BSONObjBuilder
Branch: master
https://github.com/mongodb/mongo/commit/b354fbae71aea007759e174c31a869f55fe6502f

Comment by Jeremy Fergason [ 13/Jul/12 ]

This also fails on Linux i686

Generated at Thu Feb 08 03:06:16 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.