Fix nip30303 tag parsing bugs

WordStatisticsTag.parse used has(3) but the tag has only 3 elements
(indices 0-2), so the guard never passed and parse always returned null
— silently breaking all Chapter and Chunk event parsing. Correct to
has(2).

Also replace unsafe tag[n].toInt() with toIntOrNull() in WordStatisticsTag
and IndexTag so a malformed numeric field from a relay returns null
instead of throwing NumberFormatException up through the parse loop.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Kgothatso Ngako
2026-07-25 23:56:18 +02:00
parent 0d79c3ecee
commit c45a310641
2 changed files with 9 additions and 5 deletions

View File

@@ -16,9 +16,10 @@ class IndexTag(
fun parse(tag: Array<String>): IndexTag? {
ensure(tag.has(1)) { return null }
ensure(tag[0] == TAG_NAME) { return null }
// TODO: Check that it is an int...
val index = tag[1].toIntOrNull() ?: return null
return IndexTag(
index = tag[1].toInt(),
index = index,
)
}

View File

@@ -16,12 +16,15 @@ class WordStatisticsTag(
const val TAG_NAME = "word_stats"
fun parse(tag: Array<String>): WordStatisticsTag? {
ensure(tag.has(3)) { return null }
ensure(tag.has(2)) { return null }
ensure(tag[0] == TAG_NAME) { return null }
val wordCount = tag[1].toIntOrNull() ?: return null
val characterCount = tag[2].toIntOrNull() ?: return null
return WordStatisticsTag(
wordCount = tag[1].toInt(),
characterCount = tag[2].toInt(),
wordCount = wordCount,
characterCount = characterCount,
)
}