What is it with PHP programmers always doing silly things?

# php -r '$file = fopen("/does/not/exist/test.txt", "r"); while (!feof($file)) 
{ $text = fgets($file); } fclose($file);'
Warning: fopen(/does/not/exist/test.txt): failed to open stream: No such file 
or directory in Command line code on line 1
Warning: feof(): supplied argument is not a valid stream resource in Command 
line code on line 1
Warning: fgets(): supplied argument is not a valid stream resource in Command 
line code on line 1
Warning: feof(): supplied argument is not a valid stream resource in Command 
line code on line 1
Warning: fgets(): supplied argument is not a valid stream resource in Command 
line code on line 1
...and so on...

This code needs a conditional to check $file as to whether or not it's
false, prior to calling feof() and fgets(), because feof(FALSE) and
fgets(FALSE) are obviously wrong.  The code will also fclose(FALSE)
which is also wrong.  Try:

  $file = fopen('stupidpath/test.txt', 'r');
  if ($file === FALSE) {
    exit(1);
  }

Regarding the "over the max execution limit time period" clause: I
cannot reproduce this problem.  FreeBSD 6.2, Apache 2.2.6, suphp 0.5.2,
PHP 5.2.4:

Command-line:

# time php -r 'ini_set("max_execution_time", 5); $file = 
fopen("/does/not/exist/test.txt", "r"); while (!feof($file)) { $text = 
fgets($file); } fclose($file);' >/dev/null

real    0m5.004s
user    0m3.129s
sys     0m0.612s
#

And from Apache natively:


# echo '<?php ini_set("max_execution_time", 5); $file = 
fopen("/does/not/exist/test.txt", "r"); while (!feof($file)) { $text = 
fgets($file); } fclose($file); ?>' > www/bad.php
# chmod 644 www/bad.php
# time curl -s http://www.home.lan/bad.php >/dev/null

real    0m6.003s
user    0m0.258s
sys     0m0.648s
# time curl -s http://www.home.lan/bad.php >/dev/null

real    0m8.630s
user    0m0.461s
sys     0m1.661s
# time curl -s http://www.home.lan/bad.php >/dev/null

real    0m7.712s
user    0m0.358s
sys     0m1.441s

Yes, the times differ here, but I believe this is due to the hand-off
time between Apache and a fork()'d executable.

My recommendation: since you know the customer who's doing it, tell
them to stop writing bad code.  The fact that you know the customer
and know what script should help you be able to properly administrate
them.

-- 
| Jeremy Chadwick                                    jdc at parodius.com |
| Parodius Networking                           http://www.parodius.com/ |
| UNIX Systems Administrator                      Mountain View, CA, USA |
| Making life hard for others since 1977.                  PGP: 4BD6C0CB |

On Fri, Oct 12, 2007 at 01:32:03PM +0200, Gregory Agerba wrote:
> ----------------------------------------------------
> Bug is :
> 
> When a customer run a script that tries to read a file that does not 
> exists thru a PHP script, the script goes looping error and over the max 
> execution limit time period set in the php.ini. This causes the server 
> to hung.
> 
> ----------------------------------------------------
> 
> ----------------------------------------------------
> Proof of concept :
> 
> <?php
> $file = fopen('stupidpath/test.txt', 'r');
> while (!feof($file))
> {
>    $text = fgets($file);
> }
> fclose($file);
> ?>
> 
> ----------------------------------------------------
> 
> Gregory Agerba
> 
> _______________________________________________
> suPHP mailing list
> [email protected]
> http://lists.marsching.biz/mailman/listinfo/suphp

_______________________________________________
suPHP mailing list
[email protected]
http://lists.marsching.biz/mailman/listinfo/suphp

Reply via email to