问题
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.
- What stream is being printed to here?
- How could I write code that prints to this special stream (without smashing my stack deliberately).
- 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