Skip to content

Reject \x without hex digits in cUnescape - #2746

Open
0z5a wants to merge 1 commit into
meta:mainfrom
0z5a:fix/cunescape-hex-no-digits
Open

0z5a wants to merge 1 commit into
meta:mainfrom
0z5a:fix/cunescape-hex-no-digits

Conversation

@0z5a

@0z5a 0z5a commented Nov 13, 2026

Copy link
Copy Markdown

Summary

folly::cUnescape turned \x followed by a character that is not a hex digit into a NUL byte. cUnescape("a\\xg") returned "a\0g" in both strict and non-strict mode. The documented form is \x[0-9a-fA-F]+. Strict mode is documented to throw on an unrecognized sequence, and non-strict mode to leave it unchanged. That is already what happens when \x is the last thing in the input ("incomplete hex escape sequence").

The \x branch now treats "next character is not a hex digit" like "end of input":

input strict, before strict, after non-strict, before non-strict, after
a\xg a \0 g throws incomplete hex escape sequence a \0 g a\xg
a\x throws throws (unchanged) a\x a\x (unchanged)
a\x41 aA aA (unchanged) aA aA (unchanged)

Strict callers that relied on the old NUL byte now get the documented exception.

Fixes #2744

Validation

  • Added cases to the existing TEST(Escape, cUnescape) in folly/test/StringTest.cpp. They fail on main @ 4a2f75a ("it throws nothing"; "hello\0g", "\0\n") and pass with this change.
  • string_test (all 77 tests) passes. It was built with the CMake build (-DBUILD_TESTS=ON, RelWithDebInfo) and GCC 13.3 on x86_64 Linux.
  • This was a build of the string_test target only, not a full ctest run. Clang and MSVC were not tried.
  • clang-format (folly's .clang-format) reports no changes.
  • The branch is rebased onto main @ 7014db8. Neither touched file changed upstream between the two commits, so the tests were not re-run after the rebase.

This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.

cUnescape treated "\x" followed by a non-hex character as an escape
with no digits and appended a NUL byte: "a\xg" became "a\0g" in both
strict and non-strict mode. The documented grammar is \x[0-9a-fA-F]+,
and "\x" at the end of the input already throws "incomplete hex escape
sequence" in strict mode and is kept as "\x" in non-strict mode.

Handle "\x" followed by any non-hex character the same way as "\x" at
the end of the input.
@meta-cla meta-cla Bot added the CLA Signed label Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cUnescape turns \x followed by a non-hex character into a NUL byte

1 participant