Skip to content

Commit

Permalink
fix(selector): not throw StringIndexOut in js
Browse files Browse the repository at this point in the history
gkd-kit/inspect#7
修复在 js 里 string[i] 不会抛 StringIndexOutOfBoundsException 导致无限循环申请内存最终内存溢出
  • Loading branch information
lisonge committed Sep 22, 2023
1 parent 30a6f59 commit aa706b9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
8 changes: 0 additions & 8 deletions selector/src/commonMain/kotlin/li/songe/selector/Version.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,12 @@ internal object ParserSet {
ExtSyntaxError.assert(source, i, prefix)
val startChar = source[i]
i++
if (i >= source.length) {
ExtSyntaxError.throwError(source, i, "any char")
}
var data = ""
while (source[i] != startChar) {
if (i == source.length - 1) {
if (i >= source.length - 1) {
ExtSyntaxError.assert(source, i, startChar.toString())
break
}
Expand Down Expand Up @@ -442,7 +445,10 @@ internal object ParserSet {
val selectorList = mutableListOf<Pair<ConnectSegment, PropertySegment>>()
while (i < source.length && whiteCharParser.prefix.contains(source[i])) {
i += whiteCharStrictParser(source, i).length
val combinator = if (combinatorParser.prefix.contains((source[i]))) {
if (i >= source.length) {
break
}
val combinator = if (combinatorParser.prefix.contains(source[i])) {
val combinatorResult = combinatorParser(source, i)
i += combinatorResult.length
i += whiteCharStrictParser(source, i).length
Expand Down
16 changes: 11 additions & 5 deletions selector/src/jvmTest/kotlin/li/songe/selector/ParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ class ParserTest {
}

@Test
fun check_query(){
fun check_query() {
val projectCwd = File("../").absolutePath
val text =
"@TextView[text^='跳过'] + LinearLayout TextView[text*=`跳转`]"
val text = "@TextView[text^='跳过'] + LinearLayout TextView[text*=`跳转`]"
val selector = Selector.parse(text)
println("selector: $selector")
println(selector.trackIndex)
Expand All @@ -94,8 +93,8 @@ class ParserTest {
}
}
val transform = Transform<TestNode>(getAttr = { node, name ->
if (name=="_id") return@Transform node.id
if (name=="_pid") return@Transform node.pid
if (name == "_id") return@Transform node.id
if (name == "_pid") return@Transform node.pid
val value = node.attr[name] ?: return@Transform null
if (value is JsonNull) return@Transform null
value.intOrNull ?: value.booleanOrNull ?: value.content
Expand All @@ -106,4 +105,11 @@ class ParserTest {
println("target_size: " + targets.size)
println(targets.firstOrNull())
}

@Test
fun check_quote() {
// https://github.com/gkd-kit/inspect/issues/7
val selector = Selector.parse("a[a=''] ")
println("check_quote:$selector")
}
}

0 comments on commit aa706b9

Please sign in to comment.