nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
41.3k stars 6.58k forks source link

Segfault on parse when using "#pragma pack (push, 1)" #4284

Closed goversr closed 5 months ago

goversr commented 5 months ago

Description

Hi,

I just wanted to share this with the community. I do not see this as a bug or real issue. More a project related issue, but it took me several hours to find out what was the cause. You can probably directly close the issue. But this way other people with same kind of issue could have an idea what to look for (i had a hard time to find info on segfault with the lib on the internet; which is a compliment :-))

I had a project that required to a #pragma pack (push, 1) in an include file for one of my structures. This was incompatible with this json module and resulted in a segfault when calling parse()

Pretty hard to find the issue. I was thinking about os related issues, memory leak in my program, compiler options, ... none of that helped to solve the issue. Wrote another small program that parsed json and this worked fine. So rules out all of these ideas.

Finally found out by copying some json parsing code in my project's main() function and comment all other code parts and includes. By uncommenting and re-including step by step, I drilled down to this #pragma directive that was the root cause.

Probably it's better not to use too much of this pragma pack (push,1) stuff anyway, or to normalize after the particular structure that needs it... lesson learned :-)

Reproduction steps

add a structure that need special packing and add "#prama pack (push, 1)" in front of it

Expected vs. actual results

no segfault when calling parse()

Minimal code example

Not applicable.

Error messages

segfault 11

Compiler and operating system

g++ / Mint 21.1 - 64bit

Library version

initially happens on 3.10.5

Validation

gregmarr commented 5 months ago

Were you using this library in multiple files and only doing the pragma in some of them? That would definitely cause ODR (one definition rule) failures, as you would get different declarations of the same structure in different translation units (files), which is not allowed by the standard, but isn't required to be diagnosed.

goversr commented 5 months ago

Intresting.

I was using the library in different files, but they all include the same header file with the pragma.

gregmarr commented 5 months ago

Did they all include the two headers in the same order? The pragma takes effect from that point forward, so if some files included the json header first, then they wouldn't be affected by it.

goversr commented 5 months ago

indeed. as written not a bug report, but rather doc for somebody getting the same segfault.