util.c: assert that format strings are not empty

If any of these format strings are empty, things can go very wrong at
runtime, from garbage output to segfaults (e.g. see #22375).

This is a static check, so it could be placed in any source file. I
chose util.c because according to the comment at the top, it is the home
of "any stuff that people couldn't think of a better place for".
This commit is contained in:
Lukas Mai 2024-07-04 20:14:12 +02:00 committed by mauke
parent 335f06f026
commit c5df4fd101

17
util.c
View File

@ -121,6 +121,23 @@ S_maybe_protect_ro(pTHX_ struct perl_memory_debug_header *header)
# endif
#endif
/* Sanity check: Format strings must not be empty */
STATIC_ASSERT_DECL(sizeof I32df > 1);
STATIC_ASSERT_DECL(sizeof U32of > 1);
STATIC_ASSERT_DECL(sizeof U32uf > 1);
STATIC_ASSERT_DECL(sizeof U32xf > 1);
STATIC_ASSERT_DECL(sizeof U32Xf > 1);
STATIC_ASSERT_DECL(sizeof IVdf > 1);
STATIC_ASSERT_DECL(sizeof UVuf > 1);
STATIC_ASSERT_DECL(sizeof UVof > 1);
STATIC_ASSERT_DECL(sizeof UVxf > 1);
STATIC_ASSERT_DECL(sizeof UVXf > 1);
STATIC_ASSERT_DECL(sizeof NVef > 1);
STATIC_ASSERT_DECL(sizeof NVff > 1);
STATIC_ASSERT_DECL(sizeof NVgf > 1);
STATIC_ASSERT_DECL(sizeof Gid_t_f > 1);
STATIC_ASSERT_DECL(sizeof Uid_t_f > 1);
/*
=for apidoc_section $memory
=for apidoc safesysmalloc