Skip to content

Return a String from CGI.escapeHTML - #151

Open
adityawrk wants to merge 2 commits into
ruby:masterfrom
adityawrk:fix/escape-html-class
Open

adityawrk wants to merge 2 commits into
ruby:masterfrom
adityawrk:fix/escape-html-class

Conversation

@adityawrk

@adityawrk adityawrk commented Nov 29, 2026 •

Copy link
Copy Markdown

CGI.escapeHTML kept the receiver's class when nothing needed escaping and returned a plain String when it did:

class S < String; end
CGI.escapeHTML(S.new("a")).class  # was S
CGI.escapeHTML(S.new("&")).class  # was String

The C and JRuby fast paths duplicated the input in the no-escape case (rb_str_dup / strDup). They now build a new String either way. The result stays unfrozen and keeps the original encoding.

Before, against the installed extension: {"a" => S, "&" => String}. After rebuilding: {"a" => String, "&" => String}, and test/cgi/test_cgi_escape.rb passes for both the native extension and the pure-Ruby fallback (444 tests).

Fixes #19

A String subclass came back as itself when nothing needed escaping
and as a plain String when a character was escaped.
The fast path duplicated the receiver when no character needed
escaping, so a subclass survived only in that case. Both results
are now a new String.

Fixes ruby#19

This branch has not been deployed

No deployments
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.

The class of CGI.escapeHTML result

1 participant