2

I need to prevent all php output, https://stackoverflow.com/a/617184/1695680 covers how to do this in general and for exception, errors, syntax errors so far it has worked, but.

php to produce a segfault in the pcre extension: https://ilia.ws/archives/5_Top_10_ways_to_crash_PHP.html

<?php
# prce-bug.php

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

In my testing, this still outputs:

user@host ~/crash-php $ php pcre-bug.php 
Segmentation fault (core dumped)

user@host ~/crash-php $ php pcre-bug.php  >/dev/null 2>&1
Segmentation fault (core dumped)

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

Community
  • 1
  • 1
ThorSummoner
  • 16,657
  • 15
  • 135
  • 147

3 Answers3

1

I found that using a new sh shell instance will capture system reported process deaths, like Segmentation Fault and Killed.

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

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

ThorSummoner
  • 16,657
  • 15
  • 135
  • 147
  • 1
    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. – John Kugelman Sep 01 '15 at 22:51
1

Output redirection is applied to the process, however the segfault message is being generated by bash itself as a result of the child process dying with a segfault.

One solution would be to do something like this:

echo `php pcre-bug.php >/dev/null 2>&1`
wojtow
  • 864
  • 5
  • 11
1

You can use compound commands { }:

$ { php pcre-bug.php; } &>/dev/null
$
$ echo $?
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
Segmentation fault (core dumped)
fedorqui
  • 275,237
  • 103
  • 548
  • 598