Search code examples
phpbashio-redirection

Bash PHP silence segfault


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.


Solution

  • 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.