HarmonyOSCSQLiteVela Player

A 44-Item Audit: Bridge Loops, Ring-Cache Corruption, and 5 TMDB Genre Pairs

Highlights from Batches 2-5 of Vela Player 1.3.1's deep audit (docs/deep-review-2026-11-19.md): http_bridge accept() exiting permanently on ECONNABORTED, partial pwrite corrupting readahead's ring cache, venc.c teardown UAF, bitmap_compose negative-offset UB, ftp_io strtok(), Asset Store Kit token migration, and SQLite LIKE genre cross-matching.

Before shipping Vela Player 1.3.1, I ran a full codebase audit (docs/deep-review-2026-11-19.md), logging 44 issues (DR-01 through DR-44). After resolving them, all 515 host-side JS tests and the native C suites (including ASan + UBSan runs) passed.

These are the sharpest bugs from Batches 2–5 — the kind that breeze past happy-path testing and fail silently or corrupt state on real devices.

1. http_bridge.c: one ECONNABORTED kills the stream bridge (DR-07)

The local HTTP bridge’s accept_main() loop used to check only errno == EINTR; any other accept(2) error triggered an immediate break.

When a player seeks rapidly or a TV drops a half-open handshake, accept(2) routinely returns ECONNABORTED, EAGAIN, EPROTO, or EHOSTUNREACH, and descriptor exhaustion yields EMFILE/ENFILE. Breaking out of the loop stopped the accept thread permanently without clearing g_port. Upper layers kept requesting http://127.0.0.1:<port>/... while nobody was listening anymore — requiring a full app kill to recover.

The fix extracts is_transient_accept_err() (covering HarmonyOS musl and macOS host test macros) to continue immediately, sleeps 50ms with logging on EMFILE/ENFILE/ENOMEM, and only clears g_port = -1 and closes g_listen_fd on genuinely fatal errors.

2. readahead.c: partial pwrite corruption and truncated paths (DR-09)

To avoid hitting the NAS every time a user rewinds 10 seconds, readahead.c spills evicted RAM blocks into a 256 MiB circular disk file (ra-%p.bin). The audit caught three bugs in that ring file:

  1. Torn blocks on partial pwrite: disk_spill_locked() overwrote occupied slots in-place and returned early if pwrite wrote fewer bytes than requested, leaving diskSlotBlock[slot] pointing at the old block number while disk bytes held a mix of old and new data. Rewinding into that slot played audio glitches. Now disk_spill_locked() invalidates slot metadata before writing, uses a pwrite_full() retry loop for EINTR/short writes, and marks the slot empty on any failure.
  2. 512-byte path truncation: diskDir was 512 bytes and diskPath was also 512 bytes. Appending /ra-0x....bin truncated long sandbox paths, breaking unlink(ra->diskPath) and leaking temp files. diskPath is now 640 bytes, mq_ra_set_disk() rejects oversized paths, and open() adds O_NOFOLLOW | O_CLOEXEC.
  3. mq_ra_read() now rejects pos < 0, verified under ASan + UBSan via test_disk_negative_and_long_dir in tools/test-readahead.c.

3. Native engine UB and races: venc.c, bitmap_compose.c, ftp_io.c, iso_io.c

  • Mutex destroyed before encoder callbacks stopped (DR-11, entry/src/main/cpp/transcode/venc.c): On Prepare() or Start() failure, mq_venc_open() destroyed v->cond and v->lock before calling OH_VideoEncoder_Destroy(v->codec), creating a UAF whenever venc_on_error fired from the encoder thread. I reversed the teardown order, moved read_layout() before Start() to eliminate a race with venc_on_stream_changed, widened need to int64_t need64 with stride >= width && sliceH >= height guards, and switched v->cond from CLOCK_REALTIME to CLOCK_MONOTONIC.
  • Negative-offset pointer UB (DR-12, entry/src/main/cpp/subtitle/bitmap_compose.c): When rect->x < 0, the inner loop computed rgba + ((size_t)dy * w + ox) * 4 before checking 0 <= dx && dx < w, wrapping ox into a huge unsigned offset, and cast potentially negative linesize[0] values to size_t. Bounds checks now precede pointer arithmetic, stride uses ptrdiff_t, and bounding boxes use int64_t.
  • Non-threadsafe strtok() (DR-13, ftp_io.c) and off-by-one VOB skip (DR-14, iso_io.c): ftp_parse_mlsd_line() called global strtok() during concurrent directory scans (switched to strtok_r), and ftpio_pread() left a broken data socket open on read errors (ftpio_drop_data(s); s->pos = -1; fixes it). Meanwhile, iso_io.c’s merge_vobs() checked e->name[8] == '0' — index 8 in VTS_01_0.VOB is ., so DVD menus were never skipped until fixed to name[7] != '0'.

4. Credential leakage and a skipped SQLite migration (DR-05, DR-23)

Vela’s privacy policy promises all tokens live in HarmonyOS Asset Store Kit rather than plaintext files. Yet TraktClient stored trakt_token/trakt_refresh and OpenSubtitlesClient stored os_token in plaintext marquee_prefs. Both now migrate on first read into SecretVault.putNamed()/getNamed() with a CLEARED_KEY flag to prevent stale values returning if the vault is unavailable (DR-05).

Worse, Db.ets had placed the 1.3.0 progress.subtitlePath credential-scrubbing logic inside if (store.version < 2), meaning every user upgrading from database versions v2 through v9 skipped it silently (DR-23). Moving it into an independent migrateProgressSubtitles() under store.version < 10 fixed the gap.

5. SQLite LIKE wildcards across 5 TMDB genre pairs, plus SafeArea.reset() (DR-25, DR-35)

When testing 1.2.0’s genre filter, selecting “Action” (动作) pulled in “Action & Adventure” (动作冒险). Across five TMDB genre pairs — 动作/冒险/科幻/奇幻/战争 colliding with compound labels like 动作冒险, 科幻奇幻, and 战争政治 — genres LIKE '%xxx%' cross-matched and inflated facet counts, while typing % or _ in search matched the whole database.

LibrarySql.ets now provides escapeLike() (escaping !, %, _ with ESCAPE '!'), and Stores.ets wraps genres in commas (',' || genres || ',' LIKE '%,动作,%') for exact token matching in both filtered() and selectionFacets().

Finally, DR-35 caught a lifecycle lockup in SafeArea.ets: EntryAbility.onWindowStageDestroy() never cleared SafeArea.win or unregistered listeners. If HarmonyOS destroyed and recreated the WindowStage in the same process, SafeArea.ensure() saw win !== null and returned early, freezing status-bar and 28vp bottom-bar insets on stale numbers. Adding SafeArea.reset() in onWindowStageDestroy() cleared it cleanly.

✦

Comments

  • Loading…

Comments are reviewed before publishing; email is visible only to me.