[CDRIVER-3810] Including bson-compat.h before inttypes.h may result in duplicate constant definitions Created: 27/Oct/20  Updated: 02/Nov/20  Resolved: 02/Nov/20

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

Type: Bug Priority: Major - P3
Reporter: Kevin Albertson Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Reported in this comment of CDRIVER-2245.



 Comments   
Comment by Scott Willis [ 28/Oct/20 ]

This is not a blocker for us at all, now that I know that adding #include <inttypes.h> before #include "bson.h" prevents it from manifesting.

Comment by Kevin Albertson [ 28/Oct/20 ]

bson-stdint-win32.h was removed in CDRIVER-2473 in this commit: https://github.com/mongodb/mongo-c-driver/commit/abc882dec9eb5c67fc6e344440ee81e9d33d3459

Not sure what version of MongoDB the client has

CDRIVER-2473 was done in version 1.10.0. They can determine the version of libbson by inspecting the installed bson-version.h header's BSON_VERSION macro. We do not backport bug fixes to previous minor versions unless they have high impact to limit the risk of users upgrading to the latest patch release. Are they able to upgrade libmongoc and libbson to a newer version?

Comment by Scott Willis [ 27/Oct/20 ]

Title is currently incorrect; should say "before inttypes.h".

Comment by Scott Willis [ 27/Oct/20 ]

The version we have #includes <stdint.h>; it does NOT #include <inttypes.h>.

Snippet from bson-stdint-win32.h:
#if _MSC_VER >= 1600 // [
#include <stdint.h>
#else // ] _MSC_VER >= 1600 [

Comment by Scott Willis [ 27/Oct/20 ]

Correction:  <inttypes.h> not <stdint.h>!  bson-compat.h does not #include <inttypes.h>.  It does #include <stdint.h>, however <inttypes.h> is what defines PRIi32 etc.

This issue is due to the following sequence:

  1. bson.h is #included; it #includes bson-compat.h. 
    If bson.h is #included before <inttypes.h> is #included in user code:
  2. In bson-compat.h there is code defining PRI macros with naming that collides with the macros in <inttypes.h> such as:
    #ifndef PRIi32
    #define PRIi32 "d"
    #endif
    Because <inttypes.h> has not yet been #included, this will #define PRIi32 to "d".
  3. <inttypes.h> is then #included in user code, and (re-)#defines PRIi32 etc.:
    #define PRIi32 "i"

Using Visual Studio 2017 and a version of inttypes.h that appears to be from a Windows 10? SDK; C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt\inttypes.h.

Build is done inside Visual Studio.

Not sure what version of MongoDB the client has; bson.h, bson-compat.h show 2013 copyrights. 

_MSC_VER displayed as 1916.

I do see:
#if _MSC_VER >= 1600 // [
#include <stdint.h>
#else // ] _MSC_VER >= 1600 [
And find that this <stdint.h> has path:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\stdint.h

One possible (minimal) solution would be to #include <inttypes.h> in bson-compat.h, so that the standard definitions are already present before the possibly-conflicting #defines.

Comment by Kevin Albertson [ 27/Oct/20 ]

scottw@attinc.com, bson-compat.h does attempt to include inttypes.h for non-Windows and VS 2013 or newer before conditionally defining those constants:

#if !defined(_MSC_VER) || (_MSC_VER >= 1800)
#include <inttypes.h>
#endif

Perhaps that check is too conservative. Are you compiling with Visual Studio earlier than 2013?

Additionally, can you include the cmake and build commands you are using to build, as well as the output and relevant warning?

Thank you,
Kevin

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