]> Cypherpunks repositories - gostls13.git/commit
regexp: fix slice bounds out of range panics
authorDidier Spezia <didier.06@gmail.com>
Fri, 28 Aug 2015 17:36:35 +0000 (17:36 +0000)
committerRuss Cox <rsc@golang.org>
Fri, 23 Oct 2015 03:30:25 +0000 (03:30 +0000)
commitf75f2f3fcc0bf4becde65dc23297e081a8d35d1f
tree9df6fa7b355612e7d4add3560187b8deea137fa9
parentc7e2eaff95bb4b9813a656c727337dc330af2c44
regexp: fix slice bounds out of range panics

Regular expressions involving a (x){0} term are
simplified by removing this term from the
expression, just before the expression is compiled.

The number of subexpressions is evaluated before
the simplification. The number of capture instructions
in the compiled expressions is not necessarily in line
with the number of subexpressions.

When the ReplaceAll(String) methods are used, a number
of capture slots (nmatch) is evaluated as 2*(s+1)
(s being the number of subexpressions).

In some case, it can be higher than the number of capture
instructions evaluated at compile time, resulting in a
panic when the internal slices of regexp.machine
are resized to this value.

Fixed by capping the number of capture slots to the number
of capture instructions.

I must say I do not really see the benefits of setting
nmatch lower than re.prog.NumCap using this 2*(s+1) formula,
so perhaps this can be further simplified.

Fixes #11178
Fixes #11176

Change-Id: I21415e8ef2dd5f2721218e9a679f7f6bfb76ae9b
Reviewed-on: https://go-review.googlesource.com/14013
Reviewed-by: Russ Cox <rsc@golang.org>
src/regexp/all_test.go
src/regexp/regexp.go