Skip to content

Bluetooth accessory names with MacRoman apostrophes render as � #19

Description

@yuchenzhu-research

Description

Bluetooth accessory names containing a typographic apostrophe can be corrupted in the TUI.

For example:

  • Expected: yuchen’s Magic Keyboard
  • Actual: yuchen�s Magic Keyboard

The raw output from pmset -g accps can contain byte 0xD5, which represents U+2019 RIGHT SINGLE QUOTATION MARK in MacRoman. Decoding the output with String::from_utf8_lossy treats that byte as invalid UTF-8 and inserts U+FFFD.

Current code

Some(o) if o.status.success() => {
    String::from_utf8_lossy(&o.stdout).to_string()
}

Fix

Keep the command output as bytes, preserve valid UTF-8, and fall back to MacRoman decoding only when needed:

Some(o) if o.status.success() => o.stdout,

fn parse_bluetooth_devices(output: &[u8]) -> Vec<BluetoothDevice> {
    let output = match std::str::from_utf8(output) {
        Ok(text) => std::borrow::Cow::Borrowed(text),
        Err(_) => {
            let (text, _, _) = encoding_rs::MACINTOSH.decode(output);
            text
        }
    };

    // Existing Bluetooth device parsing continues here.
}

Regression tests should cover both MacRoman and already-valid UTF-8 device names.

Pull request

A fix has already been submitted in #18.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions