Skip to content

[bug] echo -e drops \u/\U escapes for non-scalar code points under UTF-8 locales (GNU encodes them as-is) #3787

Closed

Description

@caomengxuan666

echo -e silently drops \u/\U escapes whose code point is not a valid Unicode scalar (surrogates U+D800..U+DFFF, or above U+10FFFF) when the locale is UTF-8. GNU encodes the numeric value as-is (CESU-8 for surrogates, 4-byte form above the max).

LC_ALL=C.utf8 bash -c "echo -e 'A\ud83d\ude00B'" | od -An -tx1
# GNU:   41 ed a0 bd ed b8 80 42 0a
# rubash: 41 42 0a

LC_ALL=C.utf8 bash -c "echo -e 'A\U00110000B'" | od -An -tx1
# GNU:   41 f4 90 80 80 42 0a
# rubash: 41 42 0a

Scope (morph, 16 probes x2, GNU 5.3.20 and system bash 5.2 agree on every probe): DIFF exactly for surrogates (lone high/low, pairs, boundary U+D800/U+DC00, max U+DBFF/U+DFFF, \U0000D800 form) and for U+110000. SAME for valid scalars including noncharacters (U+FFFE/U+FFFF/U+FDD0), U+10FFFF, and for the C locale (both print the escape literally). $'...' ANSI-C quoting and printf %b already match GNU on these inputs, so the defect is in echo -e's own \u/\U conversion, not the general escape decoder.

Not #1234 (closed): that was $'...' emitting U+E000 marker bytes, a different code path already fixed.

Evidence: hunt rounds r129 (prescreen) + r129 verdict on fresh master build 70e962b9c2e4 (60 cases x2, ec67 DIFF both passes). Regression seed re-verified on the verdict binary x2: GNU 12/12, rubash 6/12.

Regression seed:

#!/usr/bin/env bash
# Regression seed R129-A: echo -e drops \u/\U escapes whose code point is
# not a valid Unicode scalar (surrogates U+D800..U+DFFF, or > U+10FFFF)
# under a UTF-8 locale. GNU encodes the numeric value as-is (CESU-8 for
# surrogates, 4-byte form above the max); rubash emits nothing for the escape.
# Scope: echo -e ONLY -- $'...' ANSI-C quoting and printf %b already match
# GNU on these inputs (morph m15/m16). Valid scalars (incl. noncharacters
# and U+10FFFF) and the C locale are unaffected.
# Usage: bash seed_r129a.sh <shell>   (shell = path to bash/rubash binary)
S="${1:?usage: bash seed_r129a.sh <shell>}"
fail=0
check() { if [ "$2" != "$3" ]; then echo "FAIL $1: expected [$2] got [$3]"; fail=1; else echo "PASS $1"; fi; }
od1() { LC_ALL=C.utf8 "$S" -c "$1" | od -An -tx1 | tr -s ' '; }
check "pair-d83d-de00" " 41 ed a0 bd ed b8 80 42 0a" "$(od1 "echo -e 'A\\ud83d\\ude00B'")"
check "lone-high-d800" " 41 ed a0 80 42 0a" "$(od1 "echo -e 'A\\ud800B'")"
check "lone-low-dfff" " 41 ed bf bf 42 0a" "$(od1 "echo -e 'A\\udfffB'")"
check "U-form-d800" " 41 ed a0 80 42 0a" "$(od1 "echo -e 'A\\U0000D800B'")"
check "beyond-max-110000" " 41 f4 90 80 80 42 0a" "$(od1 "echo -e 'A\\U00110000B'")"
check "boundary-pair" " 41 ed a0 80 ed b0 80 42 0a" "$(od1 "echo -e 'A\\ud800\\udc00B'")"
# --- controls (pass on both shells) ---
check "valid-e9" " 41 c3 a9 42 0a" "$(od1 "echo -e 'A\\u00e9B'")"
check "valid-astral" " 41 f0 9f 98 80 42 0a" "$(od1 "echo -e 'A\\U0001F600B'")"
check "nonchar-fffe" " 41 ef bf be 42 0a" "$(od1 "echo -e 'A\\ufffeB'")"
check "max-10ffff" " 41 f4 8f bf bf 42 0a" "$(od1 "echo -e 'A\\U0010FFFFB'")"
got=$(LC_ALL=C "$S" -c "echo -e 'A\\ud83d\\ude00B'" | od -An -tx1 | tr -s ' ')
check "c-locale-literal" " 41 5c 75 44 38 33 44 5c 75 44 45 30 30 42 0a" "$got"
got=$(LC_ALL=C.utf8 "$S" -c "printf '%b' 'A\\ud83d\\ude00B'" | od -An -tx1 | tr -s ' ')
check "printf-%b-pair" " 41 ed a0 bd ed b8 80 42" "$got"
exit $fail

Master SHA: 70e962b9c2e4212a4fc663db6acc16984f27b2d4

Activity

  1. added
    bug
    gnu-baseline
    area:builtins
    on Nov 16, 2026
  2. caomengxuan666 commented on Nov 16, 2026

    @caomengxuan666
    MemberAuthor

    队长车道开工(自动续单派工):复现 echo -e 在 UTF-8 locale 下丢弃非标量码点 \u/\U 转义的问题,修法与回归钉随后以草稿 PR 提交。

  3. caomengxuan666 commented on Nov 16, 2026

    @caomengxuan666
    MemberAuthor

    续作车道接手:前任车道被执行超时中断,钉已推(f7f20e6)、草稿 PR #3793 已开,修法在本地树未提交、REPORT 未写。由本车道续跑:核对修法、补 fail-first 与全腿、提交并写 REPORT。

  4. caomengxuan666 commented on Nov 17, 2026

    @caomengxuan666
    MemberAuthor

    Fixed by #3793 (merge commit 4fe7868). Y3 audit: 建议 PASS.

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

Metadata

Metadata

Labels

area:builtinsbuggnu-baseline

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions