Skip to content

Commit 775f077

Browse files
committed
Handle non-printable control characters when escaping JSON
1 parent 531f89c commit 775f077

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

analysis/vendor/json/Json.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ let escape text =
141141
| '\b' -> Buffer.add_string buf "\\b"
142142
| '\r' -> Buffer.add_string buf "\\r"
143143
| '\t' -> Buffer.add_string buf "\\t"
144-
| c -> Buffer.add_char buf c);
144+
| c ->
145+
let code = Char.code c in
146+
if code < 0x20 then Printf.bprintf buf "\\u%04x" code
147+
else Buffer.add_char buf c);
145148
loop (i + 1))
146149
in
147150
loop 0;

tests/ounit_tests/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
(<> %{profile} browser))
1212
(flags
1313
(:standard -w +a-4-9-30-40-41-42-48-70))
14-
(libraries bsb bsb_helper core ounit2))
14+
(libraries bsb bsb_helper core ounit2 analysis))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
let ( >:: ), ( >::: ) = OUnit.(( >:: ), ( >::: ))
2+
3+
let suites =
4+
__FILE__
5+
>::: [
6+
( "escape 'hello'" >:: fun _ ->
7+
let escaped = Json.escape "hello" in
8+
let expected = "hello" in
9+
OUnit.assert_equal escaped expected );
10+
( "escape \\x17" >:: fun _ ->
11+
let escaped = Json.escape "\x17" in
12+
let expected = "\\u0017" in
13+
OUnit.assert_equal escaped expected );
14+
]

tests/ounit_tests/ounit_tests_main.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ let suites =
22
OUnit.( >::: ) __FILE__
33
[
44
Ounit_vec_test.suites;
5+
Ounit_json_tests.suites;
56
Ounit_ext_json_tests.suites;
67
Ounit_path_tests.suites;
78
Ounit_array_tests.suites;

0 commit comments

Comments
 (0)