Which stream does “stack smashing detected” messag

2019-02-19 13:24发布

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条回答
可以哭但决不认输i
2楼-- · 2019-02-19 14:01

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
$
查看更多
登录 后发表回答