fix(encodings): invalid UTF-8 decode loses data silently #33

Open
opened 2026-10-28 21:52:10 +00:00 by flopana · 0 comments
Owner

Part of #30

Branch: feature/improve-encodings

Bugs

  1. TextDecoder is not fatal. Base64 decode of /w== gives � with no error. Encode it again and you get 77+9. The original bytes are lost.
  2. The error message goes into the Output field. Decode abc!, then click the toggle: the input becomes the error text.
  3. toast.error runs inside useMemo, during render. Each invalid keystroke fires a toast. StrictMode doubles it.
  4. Decode does not trim. SGVsbG8= + newline fails. Base16 decode of lowercase 48656c6c6f may fail.

Verification: from reading the code only. Item 4 base16 comes from memory of @scure/base. Check it first.

Red tests

  • Base64 decode of /w== shows an error, not �.
  • Decode abc!, then toggle: the input does not hold the error text.
  • No toast fires during render.
  • Decode of SGVsbG8= + newline gives Hello. Base16 decode of 48656c6c6f gives Hello, if it fails now.

Fix

  • convert(input, encoding, direction) in src/lib/Generators/Encodings.ts. It returns {success, value | error}, like parseTimestamp.
  • new TextDecoder("utf-8", { fatal: true }).
  • Trim the decode input.
  • The component shows "" on error and fires the toast in useEffect.
Part of #30 Branch: `feature/improve-encodings` ## Bugs 1. `TextDecoder` is not fatal. Base64 decode of `/w==` gives `�` with no error. Encode it again and you get `77+9`. The original bytes are lost. 2. The error message goes into the Output field. Decode `abc!`, then click the toggle: the input becomes the error text. 3. `toast.error` runs inside `useMemo`, during render. Each invalid keystroke fires a toast. StrictMode doubles it. 4. Decode does not trim. `SGVsbG8=` + newline fails. Base16 decode of lowercase `48656c6c6f` may fail. Verification: from reading the code only. Item 4 base16 comes from memory of `@scure/base`. Check it first. ## Red tests - Base64 decode of `/w==` shows an error, not `�`. - Decode `abc!`, then toggle: the input does not hold the error text. - No toast fires during render. - Decode of `SGVsbG8=` + newline gives `Hello`. Base16 decode of `48656c6c6f` gives `Hello`, if it fails now. ## Fix - `convert(input, encoding, direction)` in `src/lib/Generators/Encodings.ts`. It returns `{success, value | error}`, like `parseTimestamp`. - `new TextDecoder("utf-8", { fatal: true })`. - Trim the decode input. - The component shows `""` on error and fires the toast in `useEffect`.
Sign in to join this conversation.
No description provided.