summaryrefslogtreecommitdiff
path: root/ext/pcre/php_pcre.c
diff options
context:
space:
mode:
authorAndrei Zmievski <andrei@php.net>2000-11-13 19:49:41 +0000
committerAndrei Zmievski <andrei@php.net>2000-11-13 19:49:41 +0000
commit468068705fe090bbafc050a14da18b17452b55fb (patch)
treed10ae900262f6244c591d8de044c2ab94113b5f3 /ext/pcre/php_pcre.c
parent86b7cd0572cb61a2009c4118f643fe23cbd4f092 (diff)
downloadphp-git-468068705fe090bbafc050a14da18b17452b55fb.tar.gz
@- Modified preg_replace() to ignore backreferences that refer to
@ non-existing subpatterns. (Andrei)
Diffstat (limited to 'ext/pcre/php_pcre.c')
-rw-r--r--ext/pcre/php_pcre.c39
1 files changed, 24 insertions, 15 deletions
diff --git a/ext/pcre/php_pcre.c b/ext/pcre/php_pcre.c
index 877eecb152..096c36eec1 100644
--- a/ext/pcre/php_pcre.c
+++ b/ext/pcre/php_pcre.c
@@ -555,16 +555,22 @@ static int preg_do_eval(char *eval_str, int eval_str_len, char *subject,
while (*walk) {
/* If found a backreference.. */
- if ('\\' == *walk && preg_get_backref(walk+1, &backref) && backref < count) {
- /* Find the corresponding string match and substitute it
- in instead of the backref */
- match = subject + offsets[backref<<1];
- match_len = offsets[(backref<<1)+1] - offsets[backref<<1];
- if (match_len)
- esc_match = php_addslashes(match, match_len, &esc_match_len, 0);
- else {
- esc_match = match;
+ if ('\\' == *walk && preg_get_backref(walk+1, &backref)) {
+ if (backref < count) {
+ /* Find the corresponding string match and substitute it
+ in instead of the backref */
+ match = subject + offsets[backref<<1];
+ match_len = offsets[(backref<<1)+1] - offsets[backref<<1];
+ if (match_len)
+ esc_match = php_addslashes(match, match_len, &esc_match_len, 0);
+ else {
+ esc_match = match;
+ esc_match_len = 0;
+ }
+ } else {
+ esc_match = empty_string;
esc_match_len = 0;
+ match_len = 0;
}
sprintf(backref_buf, "\\%d", backref);
new_code = php_str_to_str(code, code_len,
@@ -688,8 +694,9 @@ char *php_pcre_replace(char *regex, int regex_len,
} else { /* do regular substitution */
walk = replace;
while (walk < replace_end)
- if ('\\' == *walk && preg_get_backref(walk+1, &backref) && backref < count) {
- new_len += offsets[(backref<<1)+1] - offsets[backref<<1];
+ if ('\\' == *walk && preg_get_backref(walk+1, &backref)) {
+ if (backref < count)
+ new_len += offsets[(backref<<1)+1] - offsets[backref<<1];
walk += (backref > 9) ? 3 : 2;
} else {
new_len++;
@@ -719,10 +726,12 @@ char *php_pcre_replace(char *regex, int regex_len,
} else { /* do regular backreference copying */
walk = replace;
while (walk < replace_end)
- if ('\\' == *walk && preg_get_backref(walk+1, &backref) && backref < count) {
- match_len = offsets[(backref<<1)+1] - offsets[backref<<1];
- memcpy(walkbuf, subject + offsets[backref<<1], match_len);
- walkbuf += match_len;
+ if ('\\' == *walk && preg_get_backref(walk+1, &backref)) {
+ if (backref < count) {
+ match_len = offsets[(backref<<1)+1] - offsets[backref<<1];
+ memcpy(walkbuf, subject + offsets[backref<<1], match_len);
+ walkbuf += match_len;
+ }
walk += (backref > 9) ? 3 : 2;
} else {
*walkbuf++ = *walk++;