[php-src] Issue #7917: preg_match and preg_match_all both do not set $matches on regex fail, leaving as it was set prior to call

From: Date: Sun, 09 Jan 2022 23:02:06 +0000
Subject: [php-src] Issue #7917: preg_match and preg_match_all both do not set $matches on regex fail, leaving as it was set prior to call
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-238922@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/7917 Comment Author: mschroebel Seems to me from programming for 35 years that when you call a function that is supposed to return matches and it fails, that there ought to be nothing there. I didn't discover it because I had a logic problem, but it seems that there is a pitfall there. I was just stepping through my code with Xdebug and phpStorm, seeing the fail and the value returned. Say you call preg_match_all to validate something, and then again to further verify without checking the result. The $matches still has the values from the first call that worked. My thinking when saw that was there are lots and lots of these cases hanging around out there, where everything looks "hunky dory" yet isn't because the 2nd call failed yet it's not obvious in the browser. Regex's are not the easiest things to read and work with so failing regex's are probably common. If you have 2 in the same script both using the same var for $matches, you may never know. It's not how I would expect things to be handled and it's really the only odd behavior I've found in php in 24 years. I'm only a programmer that uses php, it's up to you guys that define it. Close if you want. I know of the pitfall myself, now. Seems like shrapnel to me, though, Garbage hanging about. Been there forever I'd guess.

« previous php.bugs (#238922) next »