Browse Source

Fixed getter for out-of-bounds integer properties of String objects

Dmitry Panov 4 years ago
parent
commit
1c1127b852
2 changed files with 17 additions and 6 deletions
  1. 3 6
      string.go
  2. 14 0
      string_test.go

+ 3 - 6
string.go

@@ -190,12 +190,9 @@ func (s *stringObject) getStr(name unistring.String, receiver Value) Value {
 }
 
 func (s *stringObject) getIdx(idx valueInt, receiver Value) Value {
-	i := int64(idx)
-	if i >= 0 {
-		if i < int64(s.length) {
-			return s._getIdx(int(i))
-		}
-		return nil
+	i := int(idx)
+	if i >= 0 && i < s.length {
+		return s._getIdx(i)
 	}
 	return s.baseObject.getStr(idx.string(), receiver)
 }

+ 14 - 0
string_test.go

@@ -0,0 +1,14 @@
+package goja
+
+import "testing"
+
+func TestStringOOBProperties(t *testing.T) {
+	const SCRIPT = `
+	var string = new String("str");
+	
+	string[4] = 1;
+	string[4];
+	`
+
+	testScript1(SCRIPT, valueInt(1), t)
+}