Exporter: Arabic mojibake in Excel (missing UTF-8 BOM), cursor leak, and potential NPE #3

Open
opened 2026-10-20 19:33:45 +00:00 by gemini · 0 comments
Collaborator

Description

Review of Exporter.kt identified several reliability and usability issues with CSV exporting:

  1. Arabic Text Corrupted in Microsoft Excel (Missing BOM):
    The CSV is written in standard UTF-8 without a Byte Order Mark (). When opening the exported CSV in Microsoft Excel, Excel defaults to legacy ANSI/system encodings, rendering Arabic questions and demographic text as unreadable Mojibake (العربية). Writing  at the start of the file resolves this across all spreadsheet software.

  2. Cursor Leak on Exception:
    In Exporter.export():

    val cursor = db.rawQuery(...)
    val fileName = ...
    val file = File(...)
    file.bufferedWriter().use { writer ->
        cursor.use { ... }
    }
    

    If an exception occurs during file creation or writer allocation, cursor is never closed.

  3. Potential NPE on getExternalFilesDir(null):
    If external storage is unavailable or unmounted, context.getExternalFilesDir(null) can return null, causing File(null, fileName) to throw an uncaught exception.

  4. Incomplete CSV Escaping:
    escapeCsv() checks for ,, ", and , but does not check for carriage return .

  5. Storage Leak / File Accumulation:
    Each export creates a new timestamped file (export_${System.currentTimeMillis()}.csv) in app storage without cleaning up previous exports.

Suggested Fix

  • Write  before writing the CSV header.
  • Wrap the database cursor directly in cursor.use outside the file writer.
  • Add null-checking and fallback to context.filesDir if external storage directory is null.
  • Handle in escapeCsv().
  • Prune old export files or reuse a dedicated export file name.
### Description Review of `Exporter.kt` identified several reliability and usability issues with CSV exporting: 1. **Arabic Text Corrupted in Microsoft Excel (Missing BOM)**: The CSV is written in standard UTF-8 without a Byte Order Mark (``). When opening the exported CSV in Microsoft Excel, Excel defaults to legacy ANSI/system encodings, rendering Arabic questions and demographic text as unreadable Mojibake (`العربية`). Writing `` at the start of the file resolves this across all spreadsheet software. 2. **Cursor Leak on Exception**: In `Exporter.export()`: ```kotlin val cursor = db.rawQuery(...) val fileName = ... val file = File(...) file.bufferedWriter().use { writer -> cursor.use { ... } } ``` If an exception occurs during file creation or writer allocation, `cursor` is never closed. 3. **Potential NPE on `getExternalFilesDir(null)`**: If external storage is unavailable or unmounted, `context.getExternalFilesDir(null)` can return `null`, causing `File(null, fileName)` to throw an uncaught exception. 4. **Incomplete CSV Escaping**: `escapeCsv()` checks for `,`, `"`, and ` `, but does not check for carriage return ` `. 5. **Storage Leak / File Accumulation**: Each export creates a new timestamped file (`export_${System.currentTimeMillis()}.csv`) in app storage without cleaning up previous exports. ### Suggested Fix - Write `` before writing the CSV header. - Wrap the database cursor directly in `cursor.use` outside the file writer. - Add null-checking and fallback to `context.filesDir` if external storage directory is null. - Handle ` ` in `escapeCsv()`. - Prune old export files or reuse a dedicated export file name.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
charlie/Tanit#3
No description provided.