Cannot Error Trap gzdeflate

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mpetrovich
Forum Commoner
Posts: 55
Joined: Fri Oct 19, 2007 2:02 am
Location: Vancouver, WA, USA

Cannot Error Trap gzdeflate

Post by mpetrovich »

I have a function that uses gzdeflate to decrypt certain data. I am trying to error trap the function using PHP 5.1.6

Code: Select all

try {
        $RESULT = gzinflate($RESULT);
} catch (Exception $e) {
        $RESULT = '';
 }
or

Code: Select all

$RESULT = @gzinflate($RESULT);
Both methods fail and give me "gzinflate() [function.gzinflate]: data error" when the data happen to be bad. Any way to stop this error?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Cannot Error Trap gzdeflate

Post by josh »

What if you do

Code: Select all

ob_start();
ini_set('display_errors',0);
$RESULT = @gzinflate($RESULT);
ini_set('display_errors',1);
$potentialErrorMsg = ob_get_clean();
Should not need to resort to such drastic measures, but it would be an interesting to narrow down the problem
mpetrovich
Forum Commoner
Posts: 55
Joined: Fri Oct 19, 2007 2:02 am
Location: Vancouver, WA, USA

Re: Cannot Error Trap gzinflate

Post by mpetrovich »

Oh, my subject title is wrong. It is actually gzinflate that is the problem.

Thanks for the reply.

If there is no solution to this, since I am using a custom error handler, similar to what you are suggesting, I could modify the handler to ignore gzinflate, but that does not seem very appropriate.

By the way, it also fails in 5.2.6.
mpetrovich
Forum Commoner
Posts: 55
Joined: Fri Oct 19, 2007 2:02 am
Location: Vancouver, WA, USA

Re: Cannot Error Trap gzdeflate

Post by mpetrovich »

One of my possible workarounds was to use gzuncompress(), but it gives the same error. Both of these produce nonfatal runtime errors with bad data.

Maybe my problem is that my custom error handler takes priority over the try-catch block or the @?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Cannot Error Trap gzdeflate

Post by josh »

Can you post the full code to reproduce, maybe by base64 encoding the file and defining the $RESULT variable, so I can copy paste and test myself.
mpetrovich
Forum Commoner
Posts: 55
Joined: Fri Oct 19, 2007 2:02 am
Location: Vancouver, WA, USA

Re: Cannot Error Trap gzdeflate

Post by mpetrovich »

Thanks Josh. Here some simple code to test this. To see errors you need:

Code: Select all

ini_set('display_errors','1');
error_reporting(E_ALL);

Code: Select all

$test = 'asdfasdf';
 
try {
  $RESULT = gzinflate($test);
} catch (Exception $e) {
  $RESULT = '';
}
 
$RESULT = @gzinflate($test);
When you run the above code you will see two errors.

I have functions, EncryptString and DecryptString, that happen to use compression as an option. I use them quite a bit to create encrypted query strings for security. For query strings it is good to deflate them to minimize size. If you get an error in these cases, you did something wrong, and need to check.

I am working on a different application, where I have been requested to store encrypted data, and when you enter the right password you can see it. That is fine, except if you do not enter the right password, the errors pop-up instead of my notice that decryption failed.

Per your original idea, there is another way to trap this:

Code: Select all

set_error_handler('my_handler');
... code here ...
restore_error_handler();
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Cannot Error Trap gzdeflate

Post by josh »

I see the error, when I change it so it uses error supression

Code: Select all

 $RESULT = @gzinflate($test);
I get no error (blank white page ). I am running PHP 5.2.11
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Cannot Error Trap gzdeflate

Post by requinix »

Just making sure here:

We all know that gzinflate will raise an error and not an exception, right? And that errors cannot be handled with a try/catch block and only handled with something specified via set_error_handler?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Cannot Error Trap gzdeflate

Post by josh »

It is important to remember that the standard PHP error handler is completely bypassed. error_reporting() settings will have no effect and your error handler will be called regardless - however you are still able to read the current value of error_reporting and act appropriately. Of particular note is that this value will be 0 if the statement that caused the error was prepended by the @ error-control operator.
I think @ counts as an error reporting setting, and thus has no effect, your error handler gets called, which is "sorta" documented behavior

Seems like you did not provide enough of your code to reproduce the problem. FYI in the future, your whole problem here was you never took your code out of it's original environment. When facing programs ALWAYS try to isolate things so you can look in the right place.

This is why I am a proponent of OOP. You should crate a Compressor class that has methods that use this function. Then only use this Compressor object to compress gz documents. Later on you can go and edit that Compressor class to temporarily disable the error handler before calling the function. A huge benefit to this, it is commonly referred to as "wrapping the API" or "decoupling code", or "writing an adapter class". The same could be done with a function, to create a function called my_gz_compress(), but the object is a nicer more extensible way to do it
mpetrovich
Forum Commoner
Posts: 55
Joined: Fri Oct 19, 2007 2:02 am
Location: Vancouver, WA, USA

Re: Cannot Error Trap gzdeflate

Post by mpetrovich »

Thanks Josh and thanks tasairis.

Josh, you are right. It's the environment that sometimes get's you. I was actually testing this in another environment, but that also had the same error handling, and I missed it. The use of this function happens to be within a class, which is where I temporarily removed the error handling.

I learned a few things here. And Tasairis, no, I did not realize the try-catch did not trap errors like I have used in other programming languages, but it is obvious now. So, thanks for pointing that out.
Post Reply