2

I need to prevent all PHP interpreter output. How can I suppress all output from a command using Bash? covers how to do this in general and for exceptions, errors, and syntax errors so far it has worked, but.

The PHP interpreter to produce a segfault in the PCRE extension: Top 10 ways to crash PHP

<?php
    # prce-bug.php

    preg_match('/(.(?!b))*/', str_repeat("a", 10000));

In my testing, this still outputs:

cd ~/crash-php
php pcre-bug.php

Output:

Segmentation fault (core dumped)

And:

php pcre-bug.php  >/dev/null 2>&1

Output:

Segmentation fault (core dumped)

So even with shell output redirection, output is getting to my terminal.

3 Answers 3

2

I found that using a new sh shell instance will capture system-reported process deaths, like segmentation faults and killed.

sh -c 'php pcre-bug.php' >/dev/null 2>&1

However, input arguments do not go to the PHP interpreter, but rather to the sh instance which does nothing with them.

Sign up to request clarification or add additional context in comments.

3 Comments

I believe it is the shell which prints Segmentation fault, not the php process (which has already crashed). That's why redirecting the shell's output fixes it but redirecting PHP's has no effect.
What do you mean by "input arguments"? If you want to pass the shell's command-line arguments to the php process, you could use php pcre-bug.php "$@"
Pretty sure he solved his problem since 2015.
1

Output redirection is applied to the process. However, the segmentation fault message is being generated by Bash itself as a result of the child process dying with a segmentation fault.

One solution would be to do something like this:

echo `php pcre-bug.php >/dev/null 2>&1`

Comments

1

You can use compound commands { }:

{ php pcre-bug.php; } &>/dev/null
echo $?

Output:

139

From Bash Manual -> 3.2.4 Compound Commands -> 3.2.4.3 Grouping Commands and man bash:

Compound Commands

{ list; }

list is simply executed in the current shell environment. list must be terminated with a newline or semicolon.

This is known as a group command. The return status is the exit status of list. Note that unlike the metacharacters ( and ), { and } are reserved words and must occur where a reserved word is permitted to be recognized.

Since they do not cause a word break, they must be separated from list by whitespace or another shell metacharacter.

However, using ( ) doesn't work and I don't know why:

( php pcre-bug.php ) &>/dev/null

Output:

Segmentation fault (core dumped)

1 Comment

It doesn't work because the message is being generated by the shell itself, not PHP. Using grouping doesn't change that. But if you run it in a subprocess with (), it's being generated by the subshell, and the parent shell's redirection captures it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.