From 54b7ccd514f6a689347c8d1f876bec90613f28f8 Mon Sep 17 00:00:00 2001 From: "Erik St. Martin" Date: Sat, 22 Dec 2012 11:14:56 -0500 Subject: [PATCH] regexp: fix index panic in Replace 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 | 4 ++++ src/pkg/regexp/regexp.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/pkg/regexp/all_test.go b/src/pkg/regexp/all_test.go index 3596573b4f..9c4d64f582 100644 --- a/src/pkg/regexp/all_test.go +++ b/src/pkg/regexp/all_test.go @@ -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{ diff --git a/src/pkg/regexp/regexp.go b/src/pkg/regexp/regexp.go index bcf354b44d..c516a1566f 100644 --- a/src/pkg/regexp/regexp.go +++ b/src/pkg/regexp/regexp.go @@ -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 { -- 2.48.1