php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #32192 preg_grep does not work with arrays containing references to themselves.
Submitted: 2005-03-04 17:23 UTC Modified: 2019-03-18 16:15 UTC
From: bolk at hitv dot ru Assigned: nikic (profile)
Status: Closed Package: PCRE related
PHP Version: 5.0.3 OS: Linux RH 9 and Windows XP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: bolk at hitv dot ru
New email:
PHP Version: OS:

 

 [2005-03-04 17:23 UTC] bolk at hitv dot ru
Description:
------------
preg_grep damage GLOBALS

Bug in PHP 4.3.10 and 5.0.3

Reproduce code:
---------------
<?
	preg_grep('/^T_/', $GLOBALS);
	var_dump($GLOBALS);
?>


Expected result:
----------------
array(16) {
  ["argv"]=>
  array(1) {
    [0]=>
    string(5) "a.php"
  }
  ["argc"]=>
  int(1)
  ["HTTP_POST_VARS"]=>
  array(0) {
  }
  ["_POST"]=>
  array(0) {
  }
  ["HTTP_GET_VARS"]=>
  array(0) {
  }
  ["_GET"]=>
  array(0) {
  }
  ["HTTP_COOKIE_VARS"]=>
  array(0) {
  }
  ["_COOKIE"]=>
  array(0) {
  }
  ["HTTP_SERVER_VARS"]=>


etceteras

Actual result:
--------------
string(5) "Array"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-03-04 18:22 UTC] tony2001@php.net
That's because $GLOBALS contains reference to itself.
So I'd call this kinda expected behaviour.
See this code:
<?php
$a = Array();
$a[] = &$a; 

preg_grep('/^T_/', $a);
var_dump($a);
?>
 [2005-03-04 18:36 UTC] bolk at hitv dot ru
Is it "expected behaviour" too? I think no.

<?
	$b = array(1,2,3);

	$a = array();
	$a[] = &$b;

	preg_grep('/^T_/', $a);

	var_dump($a);
	var_dump($b);
?>

array(1) {
  [0]=>
  &string(5) "Array"
}
string(5) "Array"
 [2005-03-04 18:37 UTC] plintus at smtp dot ru
Tony, if a bug occurs in some other situations, it's still a bug. However, Microsoft can use this trick: "Bugs are expected behaviour in Windows" :-)
 [2005-03-04 18:56 UTC] tony2001@php.net
bolk:
yup, this is the very same behaviour.
all elements of the target array get converted to strings, that's what you see.

plintus:
propose your own solution instead of useless comments?
recursive references should be carefully used and current situation looks like a user fault to me.
 [2005-03-04 19:01 UTC] bolk at hitv dot ru
I belive this situation must be detected. I think it is a bug. preg_grep() should not change its agruments.
 [2005-03-04 19:08 UTC] bolk at hitv dot ru
I see that the zend_print_zval_r_ex() detects recursive references.
 [2005-03-04 19:18 UTC] bolk at hitv dot ru
Why not introduce a function that converts refs to non-refs in array? Say, array_flatten ($array) or something?
 [2016-12-30 22:54 UTC] cmb@php.net
-Package: Feature/Change Request +Package: PCRE related
 [2019-03-18 16:15 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2019-03-18 16:15 UTC] nikic@php.net
Looks like this has been fixed a long time ago: https://3v4l.org/0f1ba
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 09:01:29 2024 UTC