Skip to content

stdlib(encoding): to_base64 is wrong for non-ASCII input on Bun-ESM (UTF-16 units, not UTF-8 octets) — silent wrong output #849

Backlog

Description

@hyperpolymath

On Bun-ESM, encoding::to_base64 returns wrong output for any string containing non-ASCII characters. This is silent wrong output, the top tier (T0) on #563.

Reproduce

Re-verified on main-based build 161eaaf:

use encoding::{to_base64};
pub fn main() -> () {
  println(to_base64("é"));
  println(to_base64("🦜"));
  println(to_base64("abc"));
}
Input Bun-ESM Correct (RFC 4648; printf 'é' | base64)
é 6Q== w6k=
🦜 P5w= 8J+mnA==
abc YWJj YWJj

Cause

stdlib/encoding.affine reads the input through len / string_sub / string_char_code_at. These index by UTF-8 octet on the interpreter and core wasm, but by UTF-16 code unit on Bun-ESM (#831). On Bun the encoder therefore encodes UTF-16 code units, truncated to bytes, instead of the UTF-8 octets. No existing test applies these functions to a non-ASCII string, which is why it went unnoticed.

Other byte-oriented stdlib code has the same shape and is likely affected the same way: hex helpers in stdlib/SafeHex.affine and JSON escaping in stdlib/json.affine. Those were not run.

Not verified here

The #831 investigation reported that core wasm returns the correct w6k= and 8J+mnA==. I could not reproduce that path standalone: a stdlib module imported into a standalone wasm build is an unresolved import. The interpreter currently fails on this program's let mut loop, which PR #843 fixes.

Handling

Refs #831, #829, #844, #563.


Generated by Claude Code

Activity

  1. whatproblem-ai commented on Dec 1, 2026

    @whatproblem-ai

    Problem: On Bun-ESM, encoding::to_base64 silently returns wrong output for non-ASCII input (é gives 6Q==, 🦜 gives P5w=). len / string_sub / string_char_code_at index by UTF-16 code unit there (#831), so the encoder encodes truncated UTF-16 units instead of UTF-8 octets.
    Expected: RFC 4648 output of the UTF-8 octets on every backend (w6k=, 8J+mnA==), matching printf ... | base64.
    Missing: Bun version used, confirmation on core wasm and on the interpreter once #843 lands, whether SafeHex.affine and json.affine are actually affected


    Clarity: 93%. Clear enough to work on.

    ⌨️ Commands · 💬 Have feedback? Tell us · 🎯 New here? Add WhatProblem to your repo · 🔒 Privacy

  2. added
    bug
    priority:p1
    scope:repo
    on Dec 1, 2026
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

    bugpriority:p1scope:repo

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions