Skip to content

Commit

Permalink
util.c: assert that format strings are not empty
Browse files Browse the repository at this point in the history
If any of these format strings are empty, things can go very wrong at
runtime, from garbage output to segfaults (e.g. see Perl#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".
  • Loading branch information
mauke committed Jul 5, 2024
1 parent 335f06f commit c5df4fd
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c5df4fd

Please sign in to comment.