PHP 4.0 Bug #6346 Updated: preg_replace with arrays truncates results

From: Date: Fri, 25 Aug 2000 09:16:29 +0000
Subject: PHP 4.0 Bug #6346 Updated: preg_replace with arrays truncates results
Groups: php.dev 
Request: Send a blank email to php-dev+get-30608@lists.php.net to get a copy of this message
ID: 6346 User Update by: rick@eastcore.net Status: Open Bug Type: Misbehaving function Description: preg_replace with arrays truncates results Here's an attempt at a patch to solve the problem. The following caveats should be seriously heeded: I don't have a system handy to compile and test the patch, but I should tomorrow. I have about 15 minutes worth of familiarity with the PHP4 source base. Given that, if the patch doesn't work it should at least point to what appears to be the right place in the code to apply a fix (the problem is that the length of the arrays never appears to be checked and the replacement functions don't seem to deal well with empty arrays). --- php-4.0.1.pl2.orig/ext/pcre/php_pcre.c Mon Jun 12 14:55:57 2000 +++ php-4.0.1pl2/ext/pcre/php_pcre.c Fri Aug 25 04:03:47 2000 @@ -779,11 +779,15 @@ /* Duplicate subject string for repeated replacement */ subject_value = estrndup((*subject)->value.str.val, (*subject)->value.str.len); subject_len = (*subject)->value.str.len; - + + // empty regex array should leave subject untouched + if (zend_hash_num_elements(regex->value.ht) == 0) return subject_value; zend_hash_internal_pointer_reset(regex->value.ht); if (replace->type == IS_ARRAY) { - zend_hash_internal_pointer_reset(replace->value.ht); + // empty replace array should leave subject untouched + if (zend_hash_num_elements(replace->value.ht) == 0) return subject_value; + zend_hash_internal_pointer_reset(replace->value.ht); } else { /* Set replacement value to the passed one */ Full Bug description available at: http://bugs.php.net/?id=6346

« previous php.dev (#30608) next »