Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disallow unrecognized escapes in unescape_string (part of #21284) #27238

Merged
merged 2 commits into from
May 28, 2018
Merged
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
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