https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128191
Bug ID: 128191
Summary: #embed with a large gnu::offset past the end of the
file fails with "Invalid argument" instead of using
if_empty
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: preprocessor
Assignee: unassigned at gcc dot gnu.org
Reporter: yuki.tanaka at defaultcase dot de
Target Milestone: ---
#if __has_embed(__FILE__ gnu::offset(0x100000000000)) != __STDC_EMBED_EMPTY__
#error "__has_embed does not say empty"
#endif
const unsigned char a[] = {
#embed __FILE__ gnu::offset(0x100000000000) if_empty(42)
};
_Static_assert(sizeof a == 1, "");
<source>:6:8: error: <source>: Invalid argument
6 | #embed __FILE__ gnu::offset(0x100000000000) if_empty(42)
| ^~~~~~~~
<source>:9:1: error: static assertion failed: ""
9 | _Static_assert(sizeof a == 1, "");
| ^~~~~~~~~~~~~~
Compiler returned: 1
The offset skips past the end of the file, so the resource is empty and the
#embed should expand to if_empty(42). __has_embed already says the resource is
empty, and smaller offsets past the end of the file (on Compiler Explorer up to
0xffffffff000) do give 42. Instead GCC fails with "Invalid argument", because
it still tries to seek to the offset and the seek fails. C++ with -std=c++26
behaves the same, with gnu::offset and with offset.
Compiler Explorer:
https://godbolt.org/z/Gxh137err