-
-
Notifications
You must be signed in to change notification settings - Fork 520
Open
Labels
code quality ♻️Code quality enhancementCode quality enhancementgood first issue 🥇Good for newcomersGood for newcomers
Description
While compiling I noticed the following warning for CWE-479 from GCC 14:
depbase=`echo CRT.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\
gcc-14 -DHAVE_CONFIG_H -I. -DNDEBUG -std=c99 -pedantic -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600 -I/usr/include/libnl3 -Wall -Wcast-align -Wcast-qual -Wextra -Wfloat-equal -Wformat=2 -Winit-self -Wmissing-format-attribute -Wmissing-noreturn -Wmissing-prototypes -Wpointer-arith -Wshadow -Wstrict-prototypes -Wundef -Wunused -Wwrite-strings -Wnull-dereference -D_XOPEN_SOURCE_EXTENDED -DSYSCONFDIR="\"/usr/local/etc\"" -I"./linux" -fanalyzer -MT CRT.o -MD -MP -MF $depbase.Tpo -c -o CRT.o CRT.c &&\
mv -f $depbase.Tpo $depbase.Po
CRT.c: In function ‘CRT_handleSIGTERM’:
CRT.c:846:4: warning: call to ‘snprintf’ from within signal handler [CWE-479] [-Wanalyzer-unsafe-call-within-signal-handler]
846 | snprintf(err_buf, sizeof(err_buf),
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
847 | "A signal %d (%s) was received, exiting without persisting settings to htoprc.\n",
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
848 | sgn, signal_str);
| ~~~~~~~~~~~~~~~~
‘CRT_installSignalHandlers’: events 1-2
|
| 952 | static void CRT_installSignalHandlers(void) {
| | ^~~~~~~~~~~~~~~~~~~~~~~~~
| | |
| | (1) entry to ‘CRT_installSignalHandlers’
|......
| 966 | signal(SIGINT, CRT_handleSIGTERM);
| | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| | |
| | (2) registering ‘CRT_handleSIGTERM’ as signal handler
|
event 3
|
|cc1:
| (3): later on, when the signal is delivered to the process
|
+--> ‘CRT_handleSIGTERM’: events 4-7
|
| 835 | static void CRT_handleSIGTERM(int sgn) {
| | ^~~~~~~~~~~~~~~~~
| | |
| | (4) entry to ‘CRT_handleSIGTERM’
|......
| 838 | if (!CRT_settings->changed)
| | ~
| | |
| | (5) following ‘false’ branch...
|......
| 841 | const char* signal_str = strsignal(sgn);
| | ~~~~~~~~~~~~~~
| | |
| | (6) ...to here
|......
| 846 | snprintf(err_buf, sizeof(err_buf),
| | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| | |
| | (7) call to ‘snprintf’ from within signal handler
| 847 | "A signal %d (%s) was received, exiting without persisting settings to htoprc.\n",
| | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| 848 | sgn, signal_str);
| | ~~~~~~~~~~~~~~~~
|
Metadata
Metadata
Assignees
Labels
code quality ♻️Code quality enhancementCode quality enhancementgood first issue 🥇Good for newcomersGood for newcomers
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
cgzones commentedon Nov 15, 2024
Out of curiosity I asked an AI:
Based on the search results, snprintf is not considered async-signal-safe in C. Here’s a breakdown of the reasons:
To work around these issues, consider the following alternatives:
In summary, while snprintf is not inherently async-signal-safe, there are workarounds and alternatives available to ensure safe and reliable signal handling in C programs.
Explorer09 commentedon Nov 24, 2024
https://stackoverflow.com/questions/67839938/why-is-snprintf-not-considered-async-safe
Explorer09 commentedon Nov 25, 2024
Since the
CRT_handleSIGTERM
function simply exits after printing the diagnostic message, I wonder if the problem can be fixed by simply blocking other signals before callingsnprintf(3)
?I mean, will the warning go away if we fix that way?
BenBE commentedon Nov 25, 2024
This wont do …
Explorer09 commentedon Mar 11, 2025
Related to this bug. The
signal_safe_fprintf()
function in Settings.c is not quite "signal safe" as the function says, and should be fixed as well.