Skip to content

Commit

Permalink
disallow unrecognized escapes in unescape_string (part of #21284) (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
digital-carver authored and JeffBezanson committed May 28, 2018
1 parent 83382cf commit c545d77
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 3 additions & 1 deletion base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,9 @@ function unescape_string(io, s::AbstractString)
c == 'v' ? '\v' :
c == 'f' ? '\f' :
c == 'r' ? '\r' :
c == 'e' ? '\e' : c)
c == 'e' ? '\e' :
(c == '\\' || c == '"') ? c :
throw(ArgumentError("invalid escape sequence \\$c")))
end
else
print(io, c)
Expand Down
7 changes: 6 additions & 1 deletion test/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
0x0000001c '\x1c' "\\x1c"
0x0000001f '\x1f' "\\x1f"
0x00000020 ' ' " "
0x00000022 '"' "\\\""
0x0000002f '/' "/"
0x00000030 '0' "0"
0x00000039 '9' "9"
Expand All @@ -26,6 +27,7 @@
0x00000041 'A' "A"
0x0000005a 'Z' "Z"
0x0000005b '[' "["
0x0000005c '\\' "\\\\"
0x00000060 '`' "`"
0x00000061 'a' "a"
0x0000007a 'z' "z"
Expand Down Expand Up @@ -73,7 +75,7 @@
local str = string(ch, cx[j,2])
@test str == unescape_string(escape_string(str))
end
@test repr(ch) == "'$(isprint(ch) ? ch : st)'"
@test repr(ch) == "'$(isprint(ch) && ch != '\\' ? ch : st)'"
end

for i = 0:0x7f, p = ["","\0","x","xxx","\x7f","\uFF","\uFFF",
Expand Down Expand Up @@ -169,6 +171,9 @@ join(myio, "", "", 1)
@testset "unescape_string ArgumentErrors" begin
@test_throws ArgumentError unescape_string(IOBuffer(), string('\\',"xZ"))
@test_throws ArgumentError unescape_string(IOBuffer(), string('\\',"777"))
@test_throws ArgumentError unescape_string(IOBuffer(), string('\\',"U110000"))
@test_throws ArgumentError unescape_string(IOBuffer(), string('\\',"N"))
@test_throws ArgumentError unescape_string(IOBuffer(), string('\\',"m"))
end
@testset "#11659" begin
# The indentation code was not correctly counting tab stops
Expand Down

0 comments on commit c545d77

Please sign in to comment.