From 89e54f5594a917c0d064b426e1ee77a9241c7d85 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Thu, 23 Sep 2021 18:27:34 -0700 Subject: [PATCH] Handle NULL arguments in sigaction Fixes: #15062 --- system/lib/libc/sigaction.c | 5 ++++- tests/test_signals.c | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/system/lib/libc/sigaction.c b/system/lib/libc/sigaction.c index e76456559a848..080ad45c5a815 100644 --- a/system/lib/libc/sigaction.c +++ b/system/lib/libc/sigaction.c @@ -22,7 +22,10 @@ int __sigaction(int sig, const struct sigaction *restrict sa, struct sigaction * *old = __sig_actions[sig]; } - __sig_actions[sig] = *sa; + if (sa) { + __sig_actions[sig] = *sa; + } + return 0; } diff --git a/tests/test_signals.c b/tests/test_signals.c index 711493215ecb7..10f7839319c5e 100644 --- a/tests/test_signals.c +++ b/tests/test_signals.c @@ -127,7 +127,25 @@ void test_sigwaitinfo() { assert(!recieved1); } +void test_sigaction() { + // Use sigaction to find the existing handlers + struct sigaction action; + sigaction(SIGUSR1, NULL, &action); + assert(action.sa_handler == SIG_DFL); + assert((void (*)(int))action.sa_sigaction == SIG_DFL); + + // Now install a new handler + action.sa_handler = handler1; + sigaction(SIGUSR1, &action, NULL); + + // Verify that the new handler is returned + struct sigaction action2; + sigaction(SIGUSR1, NULL, &action2); + assert(action2.sa_handler == handler1); +} + int main() { + test_sigaction(); test_bad_signal(); test_raise_sigusr1(); test_sigpenging();