Skip to content

Commit

Permalink
@- Modified preg_replace() to ignore backreferences that refer to
Browse files Browse the repository at this point in the history
@  non-existing subpatterns. (Andrei)
  • Loading branch information
Andrei Zmievski committed Nov 13, 2000
1 parent 86b7cd0 commit 4680687
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions ext/pcre/php_pcre.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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++;
Expand Down

0 comments on commit 4680687

Please sign in to comment.