5

Our apache error_log was recently filled up with lines similar to the following (about 50GB):

[Wed Feb 01 16:50:15 2012] [error] [client 123.123.123.123] PHP Warning:
unpack() [<a href='function.unpack'>function.unpack</a>]:  
 Type V: not enough input,  need 4, have 1
    in /var/www/vhosts/domain.com/httpdocs/imagecreatefrombmp.php on line 52

Line 52 in imagecreatefrombmp.bmp is as follows:

$COLOR = unpack("V",substr($IMG,$P,3).$VIDE);

This line is buried in a while loop.

If this issue happens again I want the code to quietly exit the while loop.

The problem is I cannot replicate the problem myself so I sort of need to solve it blind.

I've devised the following little solution. Would it serve the purpose? If the "Type V not input..." error occurs again would the try catch block catch it and return false?

    try{
        $COLOR = unpack("V",substr($IMG,$P,3).$VIDE);            
    }catch (Exception $e) {
        return FALSE;        
    }
6
  • 1
    Check this one stackoverflow.com/a/1241751/1164491 Commented Feb 1, 2012 at 22:16
  • Warnings and exceptions are unrelated. Commented Feb 1, 2012 at 22:21
  • @Cheery: Please go ahead and vote to close if you find a duplicate. Commented Feb 1, 2012 at 22:24
  • @LightnessRacesinOrbit I would like to, but this privilege requires 3k Commented Feb 1, 2012 at 22:26
  • @Cheery: Oh sorry, so it does. I wonder whether that's increased; I don't remember it being so high. Commented Feb 1, 2012 at 22:35

3 Answers 3

3

You can't catch a PHP error or warning as it is not an exception.

You can test, after calling unpack, if an error was raised with error_get_last(), but that's not really practical.

Another solution is to set an error handler to catch the warning, and then throw an ErrorException for that warning. You will then be able to use try/catch and return false;.

function my_error_handler($errno = 0, $errstr = null, $errfile = null, $errline = null) {
    // If error is suppressed with @, don't throw an exception
    if (error_reporting() === 0) {
        return true; // return true to continue through the others error handlers
    }
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler('my_error_handler');

Attention: all you errors, warnings, notices, etc... will be converted to an exception. That can potentially crash your program if you had one of those before.

Now you can catch the exception:

try {
    $COLOR = unpack("V",substr($IMG,$P,3).$VIDE);            
} catch (ErrorException $e) {
    return false;        
}
Sign up to request clarification or add additional context in comments.

1 Comment

Within all the solutions proposed here. This solution fixed the problem without a glitch. Thank you!
2

In this instance (this isn't always the case with unpack, it depends on the type) it will always set $COLOR to boolean false on error.

You can check for that like this:

$COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
if ($COLOR === FALSE) { /* error handling */ }

Note the use of === instead of ==, it checks that the type matches to. It prevents instances where 0 == false returns true.

1 Comment

This looks like a neat fix. @mario 's solution looks good too.
1

No. You are getting ordinary error messages, not exceptions.
(See also PHP: exceptions vs errors?)

You will have to assert the input string length to avoid these warnings. - Should they really be irrelevant to further execution.

 if (strlen($IMG) >= $P+4)) {  // message said it needs 4 bytes

2 Comments

So the following would pretty much do the trick? if (strlen($IMG) >= $P+4)) { $COLOR = unpack("V",substr($IMG,$P,3).$VIDE); }else{return FALSE;}
I believe so. It would eschew the problem, thus the warning being logged.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.