簡體   English   中英

有沒有辦法避免在發生堆棧粉碎時終止程序?

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

我在C ++中使用pthread編寫了一個包含3個線程的程序。 當其中一個線程發生緩沖區溢出時,整個程序終止,其他線程無法運行,並顯示以下消息: *** stack smashing detected ***: ./a.out terminated
我想堆棧粉碎只會殺死BOF發生在其中的線程,其他線程仍然存活。 所以,我試圖忽略信號,但它沒有解決我的問題。
這是我的計划:

#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;
}

當我使用g++ a.cpp -lpthread編譯它時,輸出如下:

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

有沒有辦法堆棧粉碎只導致殺死BOF發生在其中的線程,程序不會終止?
請注意,我不想使用-fno-stack-protector選項編譯我的程序以避免金絲雀的保護。

有沒有辦法避免在發生堆棧粉碎時終止程序?

沒有

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM