dvidelabs / flatcc

FlatBuffers Compiler and Library in C for C
Apache License 2.0
651 stars 186 forks source link

Unintended dead code in binary schema generation #295

Open mikkelfj opened 1 month ago

mikkelfj commented 1 month ago

There seems to be unreachable code in src/compiler/codegen_schema.c.

msvc looks at this block between the two breaks and raises a warning. From a glance at the code, its analysis seems valid. I know this isn't strictly related to this PR, but I thought I would bring it to your attention as the code definitely looks strange to me.

Originally posted by @amcn in https://github.com/dvidelabs/flatcc/issues/262#issuecomment-2335731813

amcn commented 1 month ago

The different compilers frustratingly each find totally disjoint sets of unreachable blocks, attesting to just how unreliable these warnings are, but I raised this one from msvc to your attention because it definitely seemed both valid and a genuine bug. Looks a bit like a merge that went awry.

clang found a bunch of other unreachable blocks which seem genuinely unreachable but don't appear malign: the gen_panic macro in src/compiler/codegen_c.h which internally asserts and then exits, so any code after it in a basic block is unreachable.

Incidentally, gcc accepts the -Wunreachable-code flag but it has no effect on the compiler's behaviour, only being accepted for reasons of backwards compatibility according to their doc.

mikkelfj commented 1 month ago

There is a some standard expression or attribute to indicate a function that does not (always) return. I don't recall, but if added, portable library would need to manage it for when compilers don't know about it.