json: compiler error: directive output may be truncated writing between 2 and 8 bytes
When attempting to build on a 64-bit ARM device (NVIDIA Jetson Xavier NX) I’m getting the following compiler error:
src-json/json.hpp: In member function ‘void nlohmann::detail::serializer<BasicJsonType>::dump_escaped(const string_t&, bool) [with BasicJsonType = nlohmann::basic_json<>]’:
src-json/json.hpp:15935:10: error: ‘%.2X’ directive output may be truncated writing between 2 and 8 bytes into a region of size 3 [-Werror=format-truncation=]
void dump_escaped(const string_t& s, const bool ensure_ascii)
^~~~~~~~~~~~
src-json/json.hpp:15935:10: note: directive argument in the range [0, 2147483647]
In file included from /usr/include/stdio.h:862:0,
from /usr/include/c++/7/cstdio:42,
from /usr/include/c++/7/ext/string_conversions.h:43,
from /usr/include/c++/7/bits/basic_string.h:6361,
from /usr/include/c++/7/string:52,
from /usr/include/c++/7/bits/locale_classes.h:40,
from /usr/include/c++/7/bits/ios_base.h:41,
from /usr/include/c++/7/ios:42,
from /usr/include/c++/7/istream:38,
from /usr/include/c++/7/fstream:38,
from src-common/Project.hpp:11,
from src-config/Config.cpp:3:
/usr/include/aarch64-linux-gnu/bits/stdio2.h:65:44: note: ‘__builtin___snprintf_chk’ output between 3 and 9 bytes into a destination of size 3
__bos (__s), __fmt, __va_arg_pack ());
^
cc1plus: all warnings being treated as errors
What is the issue you have?
I’m using the “single include” updated a few days ago with the year update, https://github.com/nlohmann/json/commit/085d497bf76fa13eaef381881e1c6f51f26afa85
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:
ADD_DEFINITIONS ("-Wall -Wextra -Werror -Wno-unused-parameter")
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?
> g++ --version
g++ (Ubuntu/Linaro 7.5.0-3ubuntu1~18.04) 7.5.0
> cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.5 LTS"
This is the standard NVIDIA image for the Jetson Xavier NX device.
Which version of the library did you use?
- latest release version 3.9.1
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16 (8 by maintainers)
Commits related to this issue
- :rotating_light: fix format-truncation warning #2572 — committed to nlohmann/json by nlohmann 3 years ago
- :rotating_light: fix warning #2572 — committed to nlohmann/json by nlohmann 3 years ago
- :rotating_light: fix format-truncation warning #2572 — committed to harry75369/json by nlohmann 3 years ago
I ran into the exact same issue with g++ on 64-bit ARM Nvidia Jetson TX2. I can confirm that the 9 byte modification makes this release build warning disappear. The warning doesn’t appear for debug builds either way.
I don’t see anything we can do here:
%.2X
, means we want to print at least 2 digits.std::uint8_t
, so the maximal value is 255, or 0xFF.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
std::string sn(3, '\0');
tostd::string sn(8, '\0');
here?