Skip to content

fix: allow '~' in emitted anchor and alias names#1461

Merged
SGSSGene merged 1 commit into
jbeder:masterfrom
raphaelroshan:fix/emitter-anchor-tilde
Jul 13, 2026
Merged

fix: allow '~' in emitted anchor and alias names#1461
SGSSGene merged 1 commit into
jbeder:masterfrom
raphaelroshan:fix/emitter-anchor-tilde

Conversation

@raphaelroshan

Copy link
Copy Markdown
Contributor

IsAnchorChar (the emitter's ns-anchor-char check) rejects 0x7E (~) because of an off-by-one: it tests ch < 0x7E before falling through to the DEL/C1 exclusion. But ~ is a valid ns-char (YAML 1.2 printable range 0x21–0x7E) and is not a flow indicator, so it's a legal anchor-name character — and the scanner's anchor rule (Exp::Anchor()) already accepts it.

The result is an asymmetry: &anchor~ x parses fine but the emitter can't write it back out, failing with INVALID_ANCHOR — so yaml-cpp fails to round-trip its own valid input.

Fix: ch < 0x7Ech <= 0x7E. DEL (0x7F) and the C1 controls (0x80–0x9F) remain excluded. Added EmitterTest.AnchorWithTilde (fails before, passes after).

IsAnchorChar rejected 0x7E ('~') via an off-by-one (< 0x7E), even though
'~' is a valid ns-char and the parser's anchor rule already accepts it.
As a result the emitter could not round-trip its own valid anchor names,
failing with INVALID_ANCHOR. DEL (0x7F) and C1 controls stay excluded.
@SGSSGene

Copy link
Copy Markdown
Collaborator

Nice find! Could you provida a link to the yaml-specs which defines the printable range?

@raphaelroshan

raphaelroshan commented Jul 12, 2026

Copy link
Copy Markdown
Contributor Author

Sure — https://yaml.org/spec/1.2.2/#character-set

~ (U+007E) sits in the printable range [#x20-#x7E] of the c-printable production so it's a valid ns-char. Anchor names are ns-anchor-char, defined as ns-char minus the flow indicators ,[]{} (https://yaml.org/spec/1.2.2/#692-node-anchors), and ~ isn't a flow indicator, so it's valid in an anchor name.

@SGSSGene SGSSGene left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic! Thank you!

@SGSSGene
SGSSGene merged commit 156e6e2 into jbeder:master Jul 13, 2026
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants