1

I have write a program with 3 threads using pthread in C++. When buffer overflow occurs in one of the threads, whole the program terminates and other threads couldn't to be run and this message is shown: *** stack smashing detected ***: ./a.out terminated
I want to stack smashing only kills the thread that BOF occurs in it and other threads remain alive. So, I try to ignore signals, but it didn't solve my problem.
Here is my program:

#include <unistd.h>
#include <pthread.h>
#include <iostream>
#include <signal.h>
#include <string.h>

using namespace std;

int a = 0;

void sig_func(int sig)
{
}

void *func (void *arg)
{
  int c = a++;
  cout << "start thread " << c << endl;
  if (c == 1)
  {
    char stuff[8];
    strcpy(stuff, "123456789");
  }
  cout << "end thread " << c << endl;
}

int main ()
{
  pthread_t tid1, tid2, tid3;
  for(int i = 1; i <=31 ; i++)  //this line ignores all signals from 1 to 31.
     signal(i,sig_func);
  pthread_create (&tid1, 0, func, 0);
  sleep (1);
  pthread_create (&tid2, 0, func, 0);
  sleep (1);
  pthread_create (&tid3, 0, func, 0);
  sleep (1);
  return 0;
}

when I compile it with g++ a.cpp -lpthread, the output is like this:

start thread 0
end thread 0
start thread 1
end thread 1
*** stack smashing detected ***: ./a.out terminated
Aborted (core dumped)

Is there any way to stack smashing only leads to killing the thread that BOF occurs in it, and the program doesn't terminate?
Note that I don't want to compile my program with -fno-stack-protector option to avoid protection by canaries.

  • 2
    I am curious as to why you deliberately want to ship a code that [smashes its stack](http://stackoverflow.com/questions/1345670/stack-smashing-detected) (overrun's its buffer) and invoke UB. – WhiZTiM Mar 25 '17 at 09:37
  • 2
    There's no way to control what happens when you cause undefined behavior. – Barmar Mar 25 '17 at 09:47

1 Answers1

0

Is there any way to avoid terminating the program when stack smashing occurs?

No

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142