【问题标题】:gcc -fstack-protector does not throw errorgcc -fstack-protector 不抛出错误
【发布时间】:2019-03-18 15:49:06
【问题描述】:

有人知道为什么下面的代码行会抛出 *** stack smashing detected *** 错误

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
  char x[16];
  strcpy(x,"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
}

但是下面的代码没有抛出?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
    char x[16];
    x[17] = 'a';
}

谢谢!!

【问题讨论】:

  • 两种情况下的行为都是未定义的。
  • 我不明白。你说的未定义是什么意思? @EOF
  • “未定义”表示任何事情都可能发生。编译器没有义务发出会崩溃的代码,也没有义务发出将实际执行越界访问的代码。 任何事情都可能发生,编译器不需要对此保持一致。例如,我的 gcc-O3 完全优化了越界访问,因此甚至完全避免了发出堆栈 cookie 代码。

标签: c gcc stack-overflow stack-smash


【解决方案1】:

覆盖x[17] 不会覆盖gcc 放在返回地址之前的canary-值。

【讨论】:

  • 你能解释一下为什么 x[17] 不覆盖它吗? @剑鱼
  • 您必须查看程序集输出...... gcc 如何管理堆栈。
猜你喜欢
  • 2011-12-10
  • 2012-01-20
  • 2018-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-16
  • 2012-03-23
  • 1970-01-01
相关资源
最近更新 更多