Modern Apps git

← Modern-Apps.git

music: survive corrupt length fields when reading embedded lyrics

Commit
e95109ba887bbe13ee186903a232d2316107f362
Author
Vayun Mathur <[email protected]>
When
2026-11-08 02:14 UTC (28 days ago)
Parents
bbce76c
EmbeddedLyrics.read caught IllegalArgumentException, IllegalStateException and SecurityException, but a corrupt length field near Int.MAX_VALUE wraps the scan offset negative: the bounds checks then pass a negative offset into ByteBuffer.wrap (vorbis vendor length), String decoding (vorbis entry length) or raw array indexing (MP4 box scan), which throw IndexOutOfBoundsException instead. That exception escaped read() and crashed the caller - the now-playing lyrics load and the cast metadata path both read tags from the playing file, so one malformed download could take down playback UI.

Catch IndexOutOfBoundsException in read() and treat the file as carrying no readable lyrics, matching the existing corrupt-input behavior. Adds crafted-input regression tests for all three containers (Ogg vendor/entry length overflow, FLAC vendor length overflow, MP4 box-size overflow); each crashed with IndexOutOfBoundsException before the fix. Full :music:testDevUnitTest is green (13/13 EmbeddedLyricsTest, no other failures).

Diff

music/src/main/java/com/vayunmathur/music/platform/EmbeddedLyrics.kt
diff --git a/music/src/main/java/com/vayunmathur/music/platform/EmbeddedLyrics.kt b/music/src/main/java/com/vayunmathur/music/platform/EmbeddedLyrics.ktindex 74b2d34986..2a3249d199 100644--- a/music/src/main/java/com/vayunmathur/music/platform/EmbeddedLyrics.kt+++ b/music/src/main/java/com/vayunmathur/music/platform/EmbeddedLyrics.kt@@ -67,6 +67,12 @@ object EmbeddedLyrics {         null     } catch (_: IllegalStateException) {         null+    } catch (_: IndexOutOfBoundsException) {+        // Corrupt length fields can wrap the scan offset negative (e.g. a+        // vendor/entry/box length near Int.MAX_VALUE), which escapes the+        // bounds checks and fails inside ByteBuffer/array/String as an+        // out-of-bounds access rather than an IllegalArgumentException.+        null     } catch (_: SecurityException) {         null     }
music/src/test/java/com/vayunmathur/music/platform/EmbeddedLyricsTest.kt
diff --git a/music/src/test/java/com/vayunmathur/music/platform/EmbeddedLyricsTest.kt b/music/src/test/java/com/vayunmathur/music/platform/EmbeddedLyricsTest.ktindex 17186ac658..b2b110eadd 100644--- a/music/src/test/java/com/vayunmathur/music/platform/EmbeddedLyricsTest.kt+++ b/music/src/test/java/com/vayunmathur/music/platform/EmbeddedLyricsTest.kt@@ -111,6 +111,21 @@ class EmbeddedLyricsTest {         assertNull(read(oggOpus(truncated)))     } +    @Test+    fun `ogg comment lengths that overflow the offset return null instead of crashing`() {+        // vendorLength = Int.MAX_VALUE wraps (start + 4 + vendorLength) negative;+        // the bounds check then passes a negative offset to ByteBuffer.wrap.+        val vendorOverflow = "OpusTags".toByteArray(Charsets.ISO_8859_1) ++            intLe(Int.MAX_VALUE) + ByteArray(16)+        assertNull(read(oggOpus(vendorOverflow)))+        // Entry length = Int.MAX_VALUE wraps (start + length) negative, so the+        // size guard passes a huge length to String(bytes, start, length).+        val entryOverflow = "OpusTags".toByteArray(Charsets.ISO_8859_1) ++            intLe(1) + "x".toByteArray(Charsets.UTF_8) + intLe(1) ++            intLe(Int.MAX_VALUE) + ByteArray(4)+        assertNull(read(oggOpus(entryOverflow)))+    }+     @Test     fun `ogg comment packet spanning pages is reassembled`() {         val lyrics = "word ".repeat(200)@@ -165,6 +180,13 @@ class EmbeddedLyricsTest {         assertNull(read(truncated))     } +    @Test+    fun `flac comment lengths that overflow the offset return null instead of crashing`() {+        val vendorOverflow = "fLaC".toByteArray(Charsets.ISO_8859_1) ++            flacBlock(4, intLe(Int.MAX_VALUE) + ByteArray(16), last = true)+        assertNull(read(vendorOverflow))+    }+     // ------------------------------------------------------------------     // MP4     // ------------------------------------------------------------------@@ -221,6 +243,19 @@ class EmbeddedLyricsTest {         assertEquals("large box words", read(ftyp + moovHeader + moovPayload))     } +    @Test+    fun `mp4 box size that overflows the scan offset returns null instead of crashing`() {+        // A box claiming size Int.MAX_VALUE wraps (offset + size) negative, so the+        // end guard passes and the following iteration reads at a negative offset.+        // (Read as a signed int, so the high bit must stay clear: 0x7FFFFFFF.)+        val hugeFree = byteArrayOf(0x7F.toByte(), 0xFF.toByte(), 0xFF.toByte(), 0xFF.toByte()) ++            "free".toByteArray(Charsets.ISO_8859_1) + ByteArray(8)+        val meta = box("meta", ByteArray(4) + hugeFree)+        val moov = box("moov", box("udta", meta))+        val ftyp = box("ftyp", "M4A ".toByteArray(Charsets.ISO_8859_1) + ByteArray(8))+        assertNull(read(ftyp + moov))+    }+     // ------------------------------------------------------------------     // Container sniffing     // ------------------------------------------------------------------