]> Cypherpunks repositories - gostls13.git/commitdiff
regexp: fix index panic in Replace
authorErik St. Martin <alakriti@gmail.com>
Sat, 22 Dec 2012 16:14:56 +0000 (11:14 -0500)
committerRuss Cox <rsc@golang.org>
Sat, 22 Dec 2012 16:14:56 +0000 (11:14 -0500)
When using subexpressions ($1) as replacements, when they either don't exist or values weren't found causes a panic.
This patch ensures that the match location isn't -1, to prevent out of bounds errors.
Fixes #3816.

R=franciscossouza, rsc
CC=golang-dev
https://golang.org/cl/6931049

src/pkg/regexp/all_test.go
src/pkg/regexp/regexp.go

index 3596573b4ff242852b35e29d5ca3c85003c16077..9c4d64f582b1d860518d04754165ec5ad5bda654 100644 (file)
@@ -196,6 +196,10 @@ var replaceTests = []ReplaceTest{
        {"a+", "${oops", "aaa", "${oops"},
        {"a+", "$$", "aaa", "$"},
        {"a+", "$", "aaa", "$"},
+
+       // Substitution when subexpression isn't found
+       {"(x)?", "$1", "123", "123"},
+       {"abc", "$1", "123", "123"},
 }
 
 var replaceLiteralTests = []ReplaceTest{
index bcf354b44d6b818288aab159eaa81e212a88bb44..c516a1566f7e04e3d226e9bd15bfefe12bc503b6 100644 (file)
@@ -767,7 +767,7 @@ func (re *Regexp) expand(dst []byte, template string, bsrc []byte, src string, m
                }
                template = rest
                if num >= 0 {
-                       if 2*num+1 < len(match) {
+                       if 2*num+1 < len(match) && match[2*num] >= 0 {
                                if bsrc != nil {
                                        dst = append(dst, bsrc[match[2*num]:match[2*num+1]]...)
                                } else {