jorgen / json_struct

json_struct is a single header only C++ library for parsing JSON directly to C++ structs and vice versa
Other
422 stars 57 forks source link

Compilation errors with GCC 14.1.1 #57

Closed kyku closed 5 months ago

kyku commented 5 months ago

I got these after just caling ./build.sh:

In file included from /tmp/json_struct/examples/11_calling_functions.cpp:5:
In function 'int JS::Internal::ft::integer::to_buffer(T, char*, int, int*) [with T = int]',
    inlined from 'static void JS::TypeHandlerIntType<T>::from(const T&, JS::Token&, JS::Serializer&) [with T = int]' at /tmp/json_struct/include/json_struct/json_struct.h:7027:48,
    inlined from 'void JS::Internal::serializeMember(const T&, MemberInfo<MI_T, MI_M, MI_NC>&, JS::Token&, JS::Serializer&, const char*) [with T = FunctionCReturn; MI_T = int; MI_M = FunctionCReturn; MI_NC = JS::Tuple<StringLiteral<11> >]' at /tmp/json_struct/include/json_struct/json_struct.h:3061:26,
    inlined from 'static void JS::Internal::MemberChecker<T, Members, PAGE, INDEX>::serializeMembers(const T&, const Members&, JS::Token&, JS::Serializer&, const char*) [with T = FunctionCReturn; Members = JS::Tuple<JS::MI<int, FunctionCReturn, JS::Tuple<JS::Internal::StringLiteral<11> > >, JS::MI<int, FunctionCReturn, JS::Tuple<JS::Internal::StringLiteral<13> > >, JS::MI<int, FunctionCReturn, JS::Tuple<JS::Internal::StringLiteral<13> > >, JS::MI<int, FunctionCReturn, JS::Tuple<JS::Internal::StringLiteral<10> > > >; long unsigned int PAGE = 0; long unsigned int INDEX = 3]' at /tmp/json_struct/include/json_struct/json_struct.h:3171:20,
    inlined from 'static void JS::TypeHandler<T, Enable>::from(const T&, JS::Token&, JS::Serializer&) [with T = FunctionCReturn; Enable = void]' at /tmp/json_struct/include/json_struct/json_struct.h:4512:86,
    inlined from 'static JS::Error JS::Internal::FunctionCaller<T, U, Ret, Arg, NAME_COUNT, 1>::callFunctionAndSerializeReturn(T&, JS::FunctionInfo<U, Ret, Arg, NAME_COUNT, 1>&, JS::CallFunctionContext&) [with T = JsonFunctions; U = JsonFunctions; Ret = FunctionCReturn; Arg = const FunctionCArguments&; long unsigned int NAME_COUNT = 1]' at /tmp/json_struct/include/json_struct/json_struct.h:3829:29,
    inlined from 'JS::Error JS::matchAndCallFunction(T&, CallFunctionContext&, FunctionInfo<U, Ret, Arg, NAME_COUNT, TAKES_CONTEXT>&, bool) [with T = JsonFunctions; U = JsonFunctions; Ret = FunctionCReturn; Arg = const FunctionCArguments&; long unsigned int NAME_COUNT = 1; long unsigned int TAKES_CONTEXT = 1]' at /tmp/json_struct/include/json_struct/json_struct.h:4033:111,
    inlined from 'JS::Error JS::matchAndCallFunction(T&, CallFunctionContext&, FunctionInfo<U, Ret, Arg, NAME_COUNT, TAKES_CONTEXT>&, bool) [with T = JsonFunctions; U = JsonFunctions; Ret = FunctionCReturn; Arg = const FunctionCArguments&; long unsigned int NAME_COUNT = 1; long unsigned int TAKES_CONTEXT = 1]' at /tmp/json_struct/include/json_struct/json_struct.h:4027:7,
    inlined from 'static JS::Error JS::Internal::FunctionObjectTraverser<T, Functions, INDEX>::call(T&, JS::CallFunctionContext&, Functions&, bool) [with T = JsonFunctions; Functions = const JS::Tuple<JS::FunctionInfo<JsonFunctions, void, const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, 1, 0>, JS::FunctionInfo<JsonFunctions, FunctionBReturn, const FunctionBArguments&, 1, 0>, JS::FunctionInfo<JsonFunctions, FunctionCReturn, const FunctionCArguments&, 1, 1>, JS::FunctionInfo<JsonFunctions, bool, int, 1, 0> >; long unsigned int INDEX = 2]' at /tmp/json_struct/include/json_struct/json_struct.h:4085:39,
    inlined from 'static JS::Error JS::Internal::FunctionObjectTraverser<T, Functions, INDEX>::call(T&, JS::CallFunctionContext&, Functions&, bool) [with T = JsonFunctions; Functions = const JS::Tuple<JS::FunctionInfo<JsonFunctions, void, const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, 1, 0>, JS::FunctionInfo<JsonFunctions, FunctionBReturn, const FunctionBArguments&, 1, 0>, JS::FunctionInfo<JsonFunctions, FunctionCReturn, const FunctionCArguments&, 1, 1>, JS::FunctionInfo<JsonFunctions, bool, int, 1, 0> >; long unsigned int INDEX = 3]' at /tmp/json_struct/include/json_struct/json_struct.h:4090:66:
/tmp/json_struct/include/json_struct/json_struct.h:6836:43: error: writing 1 byte into a region of size 0 [-Werror=stringop-overflow=]
 6836 |     target_buffer[chars_to_write - 1 - i] = '0' + char(remainder);
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
/tmp/json_struct/include/json_struct/json_struct.h: In static member function 'static JS::Error JS::Internal::FunctionObjectTraverser<T, Functions, INDEX>::call(T&, JS::CallFunctionContext&, Functions&, bool) [with T = JsonFunctions; Functions = const JS::Tuple<JS::FunctionInfo<JsonFunctions, void, const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, 1, 0>, JS::FunctionInfo<JsonFunctions, FunctionBReturn, const FunctionBArguments&, 1, 0>, JS::FunctionInfo<JsonFunctions, FunctionCReturn, const FunctionCArguments&, 1, 1>, JS::FunctionInfo<JsonFunctions, bool, int, 1, 0> >; long unsigned int INDEX = 3]':
/tmp/json_struct/include/json_struct/json_struct.h:7025:10: note: at offset -1 into destination object 'buf' of size 40
 7025 |     char buf[40];
      |          ^~~
jorgen commented 5 months ago

I have tried to reproduce. I don't have a gcc 14.1.1 environment available, but I used godbolt: https://godbolt.org/z/Yvv85Evof Can you modify this so that it produces the error?

If not, could you tell me the platform and maybe give the full command line for compiling 11_calling_functions.cpp, (should be in your Makefile or build.ninja).

kyku commented 5 months ago

I'm building on Arch Linux.

I'm attaching compile commands where you can find all the invocations of the compiler… compile_commands.json

…as well as the build.log: build.log

kyku commented 5 months ago

It seems the error gets triggered when you add -O3 flag: https://godbolt.org/z/zE6noM8zT

jorgen commented 5 months ago

Yeah, its pretty amazing how the compiler can shave away the abstractions.

I will fix it.

jorgen commented 5 months ago

I'm tempted at adding a

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-overflow="
#pragma GCC diagnostic pop

around the the code in question. This looks like a gcc bug to me. The warning as I understand the warning message is that a buffer is indexed with -1. The code that does this is this: bilde

Can anyone chime in if they see that I'm missing the point here?

jorgen commented 5 months ago

I have spent way to much on this. GCC is giving some false positives on these warnings. I have pushed a patch to resolve it somewhat, however I had to disable array-bounds checking with -Wno-array-bounds. I have checked the code where it complains and the warnings are not correct. I tried to make a small compilable example so I could send it to the gcc guys, but I was either in a docker env or in godbolt, and it was just too tedious. I would be very grateful if someone out there managed to do this.