Skip to content

Commit 7fc4734

Browse files
fix(jvm): check for null result from parse callback
1 parent c687a0e commit 7fc4734

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

ktreesitter/src/androidInstrumentedTest/kotlin/io/github/treesitter/ktreesitter/ParserTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ class ParserTest : FunSpec({
6969
test("parse(callback)") {
7070
val source = "class Foo {}"
7171
val tree = parser.parse { byte, _ ->
72-
source.subSequence(byte.toInt(), minOf(byte.toInt() + 1, source.length))
72+
val end = minOf(byte.toInt() * 2, source.length)
73+
source.subSequence(byte.toInt(), end).ifEmpty { null }
7374
}
7475
tree.text().shouldBeNull()
7576
tree.rootNode.type shouldBe "program"

ktreesitter/src/commonTest/kotlin/io/github/treesitter/ktreesitter/ParserTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ class ParserTest : FunSpec({
6666
test("parse(callback)") {
6767
val source = "class Foo {}"
6868
val tree = parser.parse { byte, _ ->
69-
source.subSequence(byte.toInt(), minOf(byte.toInt() + 1, source.length))
69+
val end = minOf(byte.toInt() * 2, source.length)
70+
source.subSequence(byte.toInt(), end).ifEmpty { null }
7071
}
7172
tree.text().shouldBeNull()
7273
tree.rootNode.type shouldBe "program"

ktreesitter/src/jni/parser.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ static const char *parse_callback(void *payload, uint32_t byte_index, TSPoint po
5757
(*env)->DeleteLocalRef(env, point);
5858
if ((*env)->ExceptionCheck(env))
5959
return NULL;
60+
if (char_sequence == NULL)
61+
return NULL;
6062

6163
jstring string = (jstring)CALL_METHOD_NO_ARGS(Object, char_sequence, CharSequence_toString);
6264
(*env)->DeleteLocalRef(env, char_sequence);

0 commit comments

Comments
 (0)