Skip to content

Commit 1b7ce3b

Browse files
fix(scripts): address Copilot review on json_escape control char loop
- Set LC_ALL=C for the entire loop (not just printf) so that ${#s} and ${s:$i:1} operate on bytes deterministically across locales - Fix comment: U+0000 (NUL) cannot exist in bash strings, range is U+0001-U+001F; adjust code guard accordingly (code >= 1) - Emit directly to stdout instead of accumulating in a variable, avoiding quadratic string concatenation on longer inputs
1 parent 03377a4 commit 1b7ce3b

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

scripts/bash/common.sh

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,21 +171,21 @@ json_escape() {
171171
s="${s//$'\r'/\\r}"
172172
s="${s//$'\b'/\\b}"
173173
s="${s//$'\f'/\\f}"
174-
# Escape any remaining U+0000-U+001F control characters as \uXXXX.
175-
# Only single-byte characters can be JSON control chars; multi-byte UTF-8
176-
# sequences have first-byte values >= 0xC0 and are never control characters.
174+
# Escape any remaining U+0001-U+001F control characters as \uXXXX.
175+
# (U+0000/NUL cannot appear in bash strings and is excluded.)
176+
# LC_ALL=C ensures ${#s} counts bytes and ${s:$i:1} yields single bytes,
177+
# so multi-byte UTF-8 sequences (first byte >= 0xC0) pass through intact.
178+
local LC_ALL=C
177179
local i char code
178-
local out=""
179180
for (( i=0; i<${#s}; i++ )); do
180181
char="${s:$i:1}"
181-
code=$(LC_ALL=C printf '%d' "'$char" 2>/dev/null || echo 256)
182-
if (( code >= 0 && code <= 31 )); then
183-
out+=$(printf '\\u%04x' "$code")
182+
code=$(printf '%d' "'$char" 2>/dev/null || echo 256)
183+
if (( code >= 1 && code <= 31 )); then
184+
printf '\\u%04x' "$code"
184185
else
185-
out+="$char"
186+
printf '%s' "$char"
186187
fi
187188
done
188-
printf '%s' "$out"
189189
}
190190

191191
check_file() { [[ -f "$1" ]] && echo "$2" || echo "$2"; }

0 commit comments

Comments
 (0)