Skip to content

Commit

Permalink
added separate program to test signal handler [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Oct 20, 2023
1 parent 353f9a1 commit 5da59fd
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cli/signalhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ static void CppcheckSignalHandler(int signo, siginfo_t * info, void * context)
fputs(signame, output);
fputs(
#ifdef NDEBUG
" - out of memory?\n",
" - abort\n",
#else
" - out of memory or assertion?\n",
" - abort or assertion\n",
#endif
output);
#ifdef USE_UNIX_BACKTRACE_SUPPORT
Expand Down
2 changes: 2 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
if (BUILD_TESTS)
add_subdirectory(signal)

file(GLOB hdrs "*.h")
file(GLOB srcs "*.cpp")
list(APPEND testrunner_SOURCES ${hdrs} ${srcs} $<TARGET_OBJECTS:cli_objs>)
Expand Down
7 changes: 7 additions & 0 deletions test/signal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
add_executable(test-signalhandler
test-signalhandler.cpp
${PROJECT_SOURCE_DIR}/cli/signalhandler.cpp
${PROJECT_SOURCE_DIR}/cli/stacktrace.cpp)
target_include_directories(test-signalhandler PRIVATE ${PROJECT_SOURCE_DIR}/cli ${PROJECT_SOURCE_DIR}/lib)
# required for backtrace() to produce function names
target_compile_options(test-signalhandler PRIVATE -rdynamic)
55 changes: 55 additions & 0 deletions test/signal/test-signalhandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2023 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "signalhandler.h"

#include <cassert>
#include <cstring>

static void my_assert()
{
assert(false);
}

static void my_abort()
{
abort();
}

static void my_segv()
{
// cppcheck-suppress nullPointer
++*(int*)0;
}

int main(int argc, const char * const argv[])
{
if (argc != 2)
return 1;

register_signal_handler();

if (strcmp(argv[1], "assert") == 0)
my_assert();
if (strcmp(argv[1], "abort") == 0)
my_abort();
if (strcmp(argv[1], "segv") == 0)
my_segv();

return 0;
}

0 comments on commit 5da59fd

Please sign in to comment.