Which stream does “stack smashing detected” message get printed to?

戏子无情 提交于 2019-11-28 06:09:02

问题


Consider the following very basic program, which has appeared in many forms on other questions here.

#include <string.h>

int main() {
    char message[8];
    strcpy(message, "Hello, world!");
}

On my system, if I put this in a file called Classic.c, compile it with no special flags and run it, I get the following output.

$ gcc -o Classic Class.c 
$ ./Classic
*** stack smashing detected ***: ./Classic terminated
Aborted (core dumped)

Normally, program output goes to stderr or stdout, so I expected that the following would produce no output.

./Classic  2> /dev/null > /dev/null

However, the output is exactly the same, so I have three questions to this scenario.

  1. What stream is being printed to here?
  2. How could I write code that prints to this special stream (without smashing my stack deliberately).
  3. How can I redirect the output of this stream?

Note I am running on a Linux system. Specifically, Ubuntu 14.04.


回答1:


Since it's not stderr or stdout, there's only one remaining option: The controlling tty.

You can write to this with your code by opening /dev/tty.

Redirecting its output is intentionally very difficult (this is why /dev/tty is also used for password prompts). That said, if you really want to do it, expect can be used for this purpose, as can emPTY.


The easiest approach with expect is to use the included helper unbuffer, which will effectively redirect this content to stdout:

$ sh -c 'echo hello >/dev/tty' >/dev/null 2>&1
hello
$ unbuffer sh -c 'echo hello >/dev/tty' >/dev/null 2>&1
$


来源:https://stackoverflow.com/questions/29979965/which-stream-does-stack-smashing-detected-message-get-printed-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!