-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
compiler error: directive output may be truncated writing between 2 and 8 bytes #2572
Comments
Workaround for now:
|
I think this is evaluating this code and not taking into account that the max value of
|
Probably. Note I'm happy with the workaround I posted a few hours ago if you're sure there isn't a bug. Other than that 1 compiler issue, everything seems to be working fine, I've already got it parsing and outputting json files. |
I don't see anything we can do here:
Therefore, we will print exactly two digits, and a buffer of size 3 is sufficient. So the warning is a false positive, because the output will never be truncated. However, as the code is only called in case of an exception (i.e., it is not performance critical), we may refactor it to silence the warning. Could it make sense to change |
Also see: https://stackoverflow.com/a/57780456/13022 where using % can tell the compiler that the range of value is limited. But like I said, I'm happy with my current workaround. |
As I never saw this warning (though testing with GCC 11 and (std::snprintf)(&sn[0], sn.size(), "%.2X", byte); to (std::snprintf)(&sn[0], sn.size(), "%.2X", byte % 256); ? |
I went to try your proposed fix...and I cannot reproduce anymore. I'm very confused. I've changed quite a lot in the project over the last few days, added several Boost libraries and other things, so our CMake files have also changed considerably. I tried to pare it down to see if I can get it to happen again, but I'm sorry Niels, I don't know exactly how to reproduce the error any more. Regardless, thanks for the great JSON C++ library. |
You're welcome! |
So this just happened again! I figured out how to get it to repro. In debug mode, I'm not seeing it. Only when I make a release mode builds do I see this error. I tried several different ways to fix it, first with "% 256" and "(byte % 256)", but was not successful. In the end, I've gone back to my workaround as described in my 2nd comment. |
I think changing 3 to 8 in the size is probably fine. It is still within the small buffer optimization of MSVC, libc++ and libstdc++ and so should not trigger an allocation. |
Can you please try if the error is gone in branch https://github.com/nlohmann/json/tree/issue2572? |
I get exactly the same error with the original json.hpp, and with the one in that branch:
I see the following changes when I diff the old and new file:
|
Hm. Maybe I should set it to 9 bytes - I overlooked that number in the previous warnings. |
I updated the branch to use 9 bytes to address
@stephanecharette Can you please try again? |
I ran into the exact same issue with g++ on 64-bit ARM Nvidia Jetson TX2. |
Sorry to come a bit late to this, also sorry if I'm missing something, but... Am I right in thinking that you have, nominally, a byte (8-bits) that you want to represent as a 2-digit, zero-prefixed, hex value stored into a 3-character string? You say, correctly, that "%.2X" means you want to print at least 2 digits which, based on the "." will give you results that are prefixed with "0" if the value can be represented in one character (i.e. value 0 to F hex will result in 00 to 0F), which, if my assumption is correct, is what you want to achieve. I'm slightly confused by the "want to print at least 2 digits"; why not "want to print 2 digits"? In which case, wouldn't:
have the desired effect rather than going down the route of making the string longer for no real reason other than to work round what looks like an issue with the compiler (to me - who's having the same problem). If I change that line to %02X I can compile with gcc 7.5 (ish) for ARM (32-bit in our case) without the format-truncation warning. I realise that this issue is closed, but I wondered if, perhaps, the intention would be clearer by using %02X with a 3 character string than %.2X with a 9 character string. (Mmm - maybe I should've checked that this still exists in the most recent code; I see this has changed to:
) |
When attempting to build on a 64-bit ARM device (NVIDIA Jetson Xavier NX) I'm getting the following compiler error:
What is the issue you have?
I'm using the "single include" updated a few days ago with the year update, 085d497
I see this problem has been reported in the past, but as a side issue in another ticket, and without resolution: #1117.
Build with the following g++ flags on ARM device:
Including json.hpp causes our build to fail. Will need to setup exceptions around the json include file.
Which compiler and operating system are you using?
This is the standard NVIDIA image for the Jetson Xavier NX device.
Which version of the library did you use?
The text was updated successfully, but these errors were encountered: