Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions ext/cgi/escape/escape.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,10 @@ optimized_escape_html(VALUE str)
}
}

if (RSTRING_LEN(str) < (dest - buf)) {
escaped = rb_str_new(buf, dest - buf);
preserve_original_state(str, escaped);
}
else {
escaped = rb_str_dup(str);
}
/* Always a String. rb_str_dup kept a subclass when nothing was
* escaped, so the class depended on the input (cgi#19). */
escaped = rb_str_new(buf, dest - buf);
preserve_original_state(str, escaped);
ALLOCV_END(vbuf);
return escaped;
}
Expand Down
14 changes: 8 additions & 6 deletions ext/java/org/jruby/ext/cgi/escape/CGIEscape.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,15 @@ static void preserve_original_state(RubyString orig, RubyString dest) {
}
}

if (dest != null) {
dest.cat(cstrBytes, cstr + beg, len - beg);
preserve_original_state(str, dest);
return dest;
} else {
return str.strDup(runtime);
if (dest == null) {
// Always a String. strDup kept a subclass when nothing was
// escaped, so the class depended on the input (cgi#19).
dest = RubyString.newStringLight(runtime, len);
beg = 0;
}
dest.cat(cstrBytes, cstr + beg, len - beg);
preserve_original_state(str, dest);
return dest;
}

// Set of i has to happen outside this; see MATCH macro in MRI ext/cgi/escape/escape.c
Expand Down
20 changes: 20 additions & 0 deletions test/cgi/test_cgi_escape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ def test_cgi_unescapeURIComponent_nil
assert_raise(TypeError) { CGI.unescapeURIComponent(nil) }
end

def test_cgi_escape_html_returns_a_string
subclass = Class.new(String)
plain = CGI.escapeHTML(subclass.new("a"))
escaped = CGI.escapeHTML(subclass.new("&"))
assert_instance_of(String, plain)
assert_instance_of(String, escaped)
assert_equal("a", plain)
assert_equal("&amp;", escaped)
end

def test_cgi_escapeHTML
assert_equal("&#39;&amp;&quot;&gt;&lt;", CGI.escapeHTML("'&\"><"))
end
Expand Down Expand Up @@ -407,6 +417,16 @@ def teardown

include CGIEscapeTest::UnescapeHTMLTests

def test_cgi_escape_html_returns_a_string
subclass = Class.new(String)
plain = CGI.escapeHTML(subclass.new("a"))
escaped = CGI.escapeHTML(subclass.new("&"))
assert_instance_of(String, plain)
assert_instance_of(String, escaped)
assert_equal("a", plain)
assert_equal("&amp;", escaped)
end

def test_cgi_escapeHTML_with_invalid_byte_sequence
assert_equal("&lt;\xA4??&gt;", CGI.escapeHTML(%[<\xA4??>]))
end
Expand Down