nlohmann / json

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

Fix the -fstack-protector warning #4273

Closed ZhksB closed 5 months ago

ZhksB commented 5 months ago

There is a warning appeared when the json library is compiled with -fstack-protector flag using g++ 11th version in Linux Fedora 37. In our case it turns into error since we use -Werror flag. The commit aims to fix the issue by adjusting the array size to 8 bytes in lexer.hpp in lines 1553, 1558 and 1563.

Example: main.cpp

#include <nlohmann/json.hpp>
using json = nlohmann::json;

int main()
{
    json ex1 = json::parse(R"(
    {
        "pi": 3.141,
        "happy": true
    })");
   return 0;
}

Build: g++ main.cpp -o out -fstack-protector -Wstack-protector -I nlohmann/include

Output: In file included from nlohmann/include/nlohmann/detail/input/binary_reader.hpp:27, from nlohmann/include/nlohmann/json.hpp:40, from main.cpp:1: nlohmann/include/nlohmann/detail/input/lexer.hpp: In member function 'nlohmann::json_abi_v3_11_3::detail::lexer<BasicJsonType, InputAdapterType>::token_type nlohmann::json_abi_v3_11_3::detail::lexer<BasicJsonType, InputAdapterType>::scan() [with BasicJsonType = nlohmann::json_abi_v3_11_3::basic_json<>; InputAdapterType = nlohmann::json_abi_v3_11_3::detail::iterator_input_adapter<const char*>]': nlohmann/include/nlohmann/detail/input/lexer.hpp:1510:16: warning: stack protector not protecting function: all local arrays are less than 8 bytes long [-Wstack-protector] token_type scan()

The PR fixes the warning.

coveralls commented 5 months ago

Coverage Status

coverage: 100.0%. remained the same when pulling 7daa367d793a2bfa97e61ad0098fc56d83097e53 on ZhksB:develop into 7efe875495a3ed7d805ddbb01af0c7725f50c88b on nlohmann:develop.

github-actions[bot] commented 5 months ago

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @ZhksB Please read and follow the Contribution Guidelines.

nlohmann commented 5 months ago

What's wrong with setting --param ssp-buffer-size=4?

ZhksB commented 5 months ago

What's wrong with setting --param ssp-buffer-size=4?

I didn't change it. I have the error with make amalgamation on Fedora and for this reason I cannot finish the PR. I will try to make an update using a different OS.

nlohmann commented 5 months ago

What's wrong with setting --param ssp-buffer-size=4?

I didn't change it.

I meant: why do you want to change the (correct) sizes in the library code when you can use the parameter above to suppress the warning?

I have the error with make amalgamation on Fedora and for this reason I cannot finish the PR. I will try to make an update using a different OS.

Please see #4275.

ZhksB commented 5 months ago

I meant: why do you want to change the (correct) sizes in the library code when you can use the parameter above to suppress the warning?

I will check. If it works, then there is no sense to change the array size.

ZhksB commented 5 months ago

What's wrong with setting --param ssp-buffer-size=4?

I made the tests and this param works for me. There is no sense in PR more, and I close it.