Page MenuHomePhabricator

fix_magic_quotes in includes/WebRequest.php generates warnings most of the time
Closed, InvalidPublic

Description

Author: nickk2

Description:
At line 66, the function fix_magic_quotes generates warnings at foreach when
given an empty array. Fix - Add a check for empty arrays before foreach:

function &fix_magic_quotes( &$arr ) {

if (!empty($arr)) {
    foreach( $arr as $key => $val ) {
    ....
    }
}

Version: unspecified
Severity: minor

Details

Reference
bz8187

Event Timeline

bzimport raised the priority of this task from to Low.Nov 21 2014, 9:33 PM
bzimport set Reference to bz8187.
bzimport added a subscriber: Unknown Object (MLST).

The current code would not produce a warning for an empty array.

It would however produce a warning if you pass it something *that is not an array*.

Can you clarify exactly under what conditions you
see a warning, and what the warning is?

nickk2 wrote:

I doubt it's going to get something that's not an array. Currently it's only
being used in one place - in the function checkMagicQuotes in WebRequest.php,
which checks a bunch of arrays, some of which can be empty at any given time.

The warning is "PHP Warning: Invalid argument supplied for foreach()", which was
happening when it was being given an empty array.

This is with PHP 5 so maybe the behavior has changed since version 4.

nickk2 wrote:

I've checked this function a bit more, and you're right about foreach being able
to take empty arrays. It seems that the function is sometimes getting values
that are not arrays which is causing the problem.

It seems that sometimes NULLs are being given to fix_magic_quotes instead of
arrays. It might have something to do with APC because I'm getting some really
strange behavior. I put in a bunch of statements to check the types of each
array that was being passed to the function sort of like:

(in checkMagicQuotes):

if (!is_array($_COOKIE)) { .... }
if (!is_array($_ENV)) { .... }

etc

then the warnings go away.

Actually, I suspected this might have something to do with the PHP setting
auto_globals_jit. Seems this is a bug with APC:
http://pecl.php.net/bugs/bug.php?id=4772

Setting auto_globals_jit to off seems to solve the problem. I'm using the latest
version of APC, so I don't know why this is a problem.

nickk2 wrote:

Maybe a check for an array should be added to the fix_magic_quotes function anyway?

Something like:

function &fix_magic_quotes( &$arr ) {
if (is_array($arr)) {
foreach( $arr as $key => $val ) {
....
}
}

ayg wrote:

I wouldn't be comfortable doing that until I know why it's not passing an array. It could be the
symptom of some other problem.

Sounds like an upstream bug; if it's that bad (eg, COMPLETELY BROKEN) there's
not much we can do about it. Get it fixed and install a fixed version of the PHP
plugin.