ソースを参照

Correctly handle -1 submatch indexes from stdlib regexp (#208)

* Correctly handle -1 submatch indexes from stdlib regexp

This was causing panics with ... such strange regex expressions

* Update regexp.go

Co-authored-by: Dmitry Panov <[email protected]>

Co-authored-by: Dmitry Panov <[email protected]>
Mihail Stoykov 5 年 前
コミット
feb2f0b876
2 ファイル変更5 行追加2 行削除
  1. 2 2
      regexp.go
  2. 3 0
      regexp_test.go

+ 2 - 2
regexp.go

@@ -30,8 +30,8 @@ type positionMapItem struct {
 type positionMap []positionMapItem
 type positionMap []positionMapItem
 
 
 func (m positionMap) get(src int) int {
 func (m positionMap) get(src int) int {
-	if src == 0 {
-		return 0
+	if src <= 0 {
+		return src
 	}
 	}
 	res := sort.Search(len(m), func(n int) bool { return m[n].src >= src })
 	res := sort.Search(len(m), func(n int) bool { return m[n].src >= src })
 	if res >= len(m) || m[res].src != src {
 	if res >= len(m) || m[res].src != src {

+ 3 - 0
regexp_test.go

@@ -204,6 +204,7 @@ func TestRegexpUTF16(t *testing.T) {
 	assert(compareArray(str.split(re), ["", "\uDC00"]), "#5");
 	assert(compareArray(str.split(re), ["", "\uDC00"]), "#5");
 	assert(compareArray("a\uD800\uDC00b".split(/\uD800/g), ["a", "\uDC00b"]), "#6");
 	assert(compareArray("a\uD800\uDC00b".split(/\uD800/g), ["a", "\uDC00b"]), "#6");
 	assert(compareArray("a\uD800\uDC00b".split(/(?:)/g), ["a", "\uD800", "\uDC00", "b"]), "#7");
 	assert(compareArray("a\uD800\uDC00b".split(/(?:)/g), ["a", "\uD800", "\uDC00", "b"]), "#7");
+	assert(compareArray("0\x80".split(/(0){0}/g), ["0", undefined, "\x80"]), "#7+");
 
 
 	re = /(?=)a/; // a hack to use regexp2
 	re = /(?=)a/; // a hack to use regexp2
 	assert.sameValue(re.exec('\ud83d\ude02a').index, 2, "#8");
 	assert.sameValue(re.exec('\ud83d\ude02a').index, 2, "#8");
@@ -236,6 +237,8 @@ func TestRegexpUnicode(t *testing.T) {
 
 
 	assert(compareArray("a\uD800\uDC00b".split(/(?:)/gu), ["a", "𐀀", "b"]), "#6");
 	assert(compareArray("a\uD800\uDC00b".split(/(?:)/gu), ["a", "𐀀", "b"]), "#6");
 
 
+	assert(compareArray("0\x80".split(/(0){0}/gu), ["0", undefined, "\x80"]), "#7");
+
 	var re = eval('/' + /\ud834\udf06/u.source + '/u');
 	var re = eval('/' + /\ud834\udf06/u.source + '/u');
 	assert(re.test('\ud834\udf06'), "#9");
 	assert(re.test('\ud834\udf06'), "#9");