Skip to content

Commit 521e6c4

Browse files
committed
Minor fixes for signal handling: make global variables static, enhance stack vs. heap detection for recognizing stack overflows
1 parent abde80f commit 521e6c4

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

cli/cppcheckexecutor.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -311,20 +311,21 @@ static void print_stacktrace(FILE* f, bool demangling, int maxdepth)
311311
#endif
312312
}
313313

314+
static const size_t MYSTACKSIZE = 16*1024+SIGSTKSZ;
315+
static char mytstack[MYSTACKSIZE]; // alternative stack for signal handler
316+
static bool bStackBelowHeap=false;
317+
314318
/*
315-
* Neither conclusive, nor portable
316-
* Though one might to make it work beyond Linux x64
317-
* \return true if address is supposed to be on stack (contrary to heap or elswhere).
318-
* If unknown better retun false.
319+
* \return true if address is supposed to be on stack (contrary to heap or elsewhere).
320+
* If unknown better return false.
319321
*/
320322
static bool isAddressOnStack(const void* ptr)
321323
{
322-
#if defined(__linux) && defined(__amd64)
323324
char a;
324-
return ptr > &a;
325-
#else
326-
return false;
327-
#endif
325+
if (bStackBelowHeap)
326+
return ptr < &a;
327+
else
328+
return ptr > &a;
328329
}
329330

330331
/*
@@ -706,10 +707,6 @@ static int filterException(int code, PEXCEPTION_POINTERS ex)
706707
* TODO Check for multi-threading issues!
707708
*
708709
*/
709-
#if defined(USE_UNIX_SIGNAL_HANDLING)
710-
const size_t MYSTACKSIZE = 64*1024+SIGSTKSZ;
711-
char mytstack[MYSTACKSIZE];
712-
#endif
713710
int CppCheckExecutor::check_wrapper(CppCheck& cppcheck, int argc, const char* const argv[])
714711
{
715712
#ifdef USE_WINDOWS_SEH
@@ -722,12 +719,20 @@ int CppCheckExecutor::check_wrapper(CppCheck& cppcheck, int argc, const char* co
722719
return -1;
723720
}
724721
#elif defined(USE_UNIX_SIGNAL_HANDLING)
722+
// determine stack vs. heap
723+
char stackVariable;
724+
char *heapVariable=(char*)malloc(1);
725+
bStackBelowHeap = &stackVariable < heapVariable;
726+
free(heapVariable);
727+
728+
// set up alternative stack for signal handler
725729
stack_t segv_stack;
726730
segv_stack.ss_sp = mytstack;
727731
segv_stack.ss_flags = 0;
728732
segv_stack.ss_size = MYSTACKSIZE;
729733
sigaltstack(&segv_stack, NULL);
730734

735+
// install signal handler
731736
struct sigaction act;
732737
memset(&act, 0, sizeof(act));
733738
act.sa_flags=SA_SIGINFO|SA_ONSTACK;
@@ -944,7 +949,7 @@ const std::string& CppCheckExecutor::getExceptionOutput()
944949

945950
bool CppCheckExecutor::tryLoadLibrary(Library& destination, const char* basepath, const char* filename)
946951
{
947-
Library::Error err = destination.load(basepath, filename);
952+
const Library::Error err = destination.load(basepath, filename);
948953

949954
if (err.errorcode == Library::UNKNOWN_ELEMENT)
950955
std::cout << "cppcheck: Found unknown elements in configuration file '" << filename << "': " << err.reason << std::endl;

0 commit comments

Comments
 (0)