Skip to content

Commit 900ce56

Browse files
authored
Merge pull request #210 from emqx/scan-invalid-utf8-error
chore: make scan invalid utf8 error more clear
2 parents bf12c08 + 8772fa6 commit 900ce56

4 files changed

Lines changed: 59 additions & 1 deletion

File tree

src/hocon_token.erl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ read(Filename) ->
5858

5959
-spec scan(binary() | string(), hocon:ctx()) -> list().
6060
scan(Input, Ctx) when is_binary(Input) ->
61-
scan(unicode_list(Input), Ctx);
61+
case unicode_list(Input) of
62+
{error, _Ok, Invalid} ->
63+
throw({scan_invalid_utf8, Invalid, Ctx});
64+
InputList ->
65+
scan(InputList, Ctx)
66+
end;
6267
scan(Input, Ctx) when is_list(Input) ->
6368
case hocon_scanner:string(Input) of
6469
{ok, Tokens, _EndLine} ->

test/data/invalid-utf8.conf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
bridges {
2+
webhook {
3+
test {
4+
body = "<!-- Edited by XMLSpy� --> \n<note> \n<to>Tove</to> \n<from>Jani</from> \n<heading>Reminder</heading> \n<body>Don't forget me this weekend!</body> \n</note>"
5+
connect_timeout = "11s"
6+
direction = "egress"
7+
enable = true
8+
enable_pipelining = 100
9+
headers {"content-type" = "application/json"}
10+
max_retries = 3
11+
method = "post"
12+
pool_size = 9
13+
pool_type = "random"
14+
request_timeout = "5s"
15+
ssl {
16+
ciphers = ""
17+
depth = 10
18+
enable = false
19+
reuse_sessions = true
20+
secure_renegotiate = true
21+
user_lookup_fun = "emqx_tls_psk:lookup"
22+
verify = "verify_peer"
23+
versions = ["tlsv1.3", "tlsv1.2", "tlsv1.1", "tlsv1"]
24+
}
25+
url = "http://127.0.0.1:18083"
26+
}
27+
}
28+
}

test/data/unicode-utf8.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# unicode:characters_to_list(<<"®">>, utf8) return {error, _, _}
2+
# but unicode:characters_to_list(<<"®"/utf8>>, utf8) is ok.
3+
test {
4+
body = "<!-- Edited by XML-XXX® --><note>\n</note>"
5+
text = "你我他"
6+
}

test/hocon_tests.erl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,3 +881,22 @@ unescape_test() ->
881881
},
882882
Conf
883883
).
884+
885+
unicode_utf8_test() ->
886+
{ok, Conf} = hocon:load("./test/data/unicode-utf8.conf"),
887+
?assertEqual(
888+
#{
889+
<<"test">> =>
890+
#{
891+
<<"body">> => <<"<!-- Edited by XML-XXX® --><note>\n</note>"/utf8>>,
892+
<<"text">> => <<"你我他"/utf8>>
893+
}
894+
},
895+
Conf
896+
).
897+
898+
invalid_utf8_test() ->
899+
?assertMatch(
900+
{error, {scan_invalid_utf8, _, _}},
901+
hocon:load("./test/data/invalid-utf8.conf")
902+
).

0 commit comments

Comments
 (0)