Skip to content

Commit 2b1c2e2

Browse files
ahorekjeremyevans
authored andcommitted
handle invalid encoding
1 parent 8f0b257 commit 2b1c2e2

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

lib/cgi/util.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ def escapeHTML(string)
4949
table = Hash[TABLE_FOR_ESCAPE_HTML__.map {|pair|pair.map {|s|s.encode(enc)}}]
5050
string = string.gsub(/#{"['&\"<>]".encode(enc)}/, table)
5151
string.encode!(origenc) if origenc
52-
return string
52+
string
53+
else
54+
string = string.b
55+
string.gsub!(/['&\"<>]/, TABLE_FOR_ESCAPE_HTML__)
56+
string.force_encoding(enc)
5357
end
54-
string.gsub(/['&\"<>]/, TABLE_FOR_ESCAPE_HTML__)
5558
end
5659

5760
begin
@@ -90,7 +93,8 @@ def unescapeHTML(string)
9093
when Encoding::ISO_8859_1; 256
9194
else 128
9295
end
93-
string.gsub(/&(apos|amp|quot|gt|lt|\#[0-9]+|\#[xX][0-9A-Fa-f]+);/) do
96+
string = string.b
97+
string.gsub!(/&(apos|amp|quot|gt|lt|\#[0-9]+|\#[xX][0-9A-Fa-f]+);/) do
9498
match = $1.dup
9599
case match
96100
when 'apos' then "'"
@@ -116,6 +120,7 @@ def unescapeHTML(string)
116120
"&#{match};"
117121
end
118122
end
123+
string.force_encoding enc
119124
end
120125

121126
# Synonym for CGI.escapeHTML(str)

0 commit comments

Comments
 (0)