fix(beam): escape quotes and backslashes in quoted atoms - #4866
Merged
Conversation
`quoteErlangAtom` wrapped a name in single quotes without escaping its
contents, but a quoted atom reads the same escape sequences a string does.
A `[<CompiledName>]` reaches the atom text verbatim, so both characters are
user-reachable:
[<CompiledName("it's")>] -> 'it's' syntax error
[<CompiledName("back\slash")>] -> 'back\slash' silently `back lash`
The second is the worse one: `\s` is Erlang's escape for a space, so the
module compiles and the atom is simply not the one that was asked for.
Escape `\` then `'`. Only `AtomLit` literals go through this function;
module, export and function-clause names print raw and always come from
`sanitizeErlangName`, which strips these characters already.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
quoteErlangAtomwraps a name in single quotes without escaping its contents. A quoted Erlang atom reads the same escape sequences a string does, so two characters break it — and[<CompiledName>]reaches the atom text verbatim (viaunionCaseTagName), which makes both user-reachable:The second is the one that matters: it compiles cleanly and yields the atom
back lash. Codegen and theerl_tagin reflection metadata both go through this function, so they agree on the wrong name and nothing surfaces the mismatch.Fix
Escape
\then'inside the quotes (backslash first, or the ones the second replace adds get escaped again).Scope is narrow:
quoteErlangAtomis only reached fromAtomLitprinting inErlangPrinter. Module, export and function-clause names print raw and always come fromsanitizeErlangName, which strips these characters already.Test
tests/Beam/ReflectionTests.fsgains a DU carrying both hostile[<CompiledName>]s, next to the existingCompiledName/erl_tagregression test. It covers pattern matching and aMakeUnion→ match round-trip, so the reflection tag has to land on the same atom codegen emits.Verification
'back\slash'was visible in the output.erlang:atom_to_binaryround-tripsit's,back\slashandboth'and\exactly.🤖 Generated with Claude Code