Skip to content

Commit

Permalink
Fixes #13 - Invalid encoding of control characters 0x01-0x1F.
Browse files Browse the repository at this point in the history
  • Loading branch information
aw committed Mar 20, 2018
1 parent 7fa4b20 commit 7cc6eac
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 3.7.0 (2018-03-21)

* Fix issue #13 - Invalid encoding of control characters 0x01-0x1F
* Add regression tests for this issue

## 3.6.0 (2018-03-21)

* Fix issue #12 - Invalid encoding of quote and solidus (\\ and \") characters
Expand Down
26 changes: 15 additions & 11 deletions json.l
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# The MIT License (MIT)
#
# Copyright (c) 2017-2018 Alexander Williams, Unscramble <license@unscramble.jp>
# Copyright (c) 2015-2018 Alexander Williams, Unscramble <license@unscramble.jp>

(and symbols (not (= "false" (sys "PIL_NAMESPACES")))
(symbols 'json 'pico)
Expand All @@ -14,6 +14,8 @@
(local iterate-object iterate-list make-null make-boolean make-json-number)
(local make-json-string make-json-array make-generic make-object make-array) )

(setq *Json_control_characters (extract '((N) (unless (member N '("^H" "^L" "^J" "^M" "^I")) N)) (mapcar char (range 1 31))))

# send error message to STDERR
[de err-throw (Error)
(msg Error)
Expand Down Expand Up @@ -143,16 +145,18 @@
(pack
"\""
(extract '((N)
(case N
("\"" "\\\"")
("\\" "\\\\")
("^H" "\\b")
("^L" "\\f")
("^J" "\\n")
("^M" "\\r")
("^I" "\\t")
("\\\^" "\^")
(T N) ) )
(if (member N *Json_control_characters)
(pack "\\u" (pad 4 (hex (char N))))
(case N
("\"" "\\\"")
("\\" "\\\\")
("^H" "\\b")
("^L" "\\f")
("^J" "\\n")
("^M" "\\r")
("^I" "\\t")
("\\\^" "\^")
(T N) ) ) )
(chop Value) )
"\"" ]

Expand Down
2 changes: 1 addition & 1 deletion module.l
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[de MODULE_INFO
("name" "json")
("version" "3.6.0")
("version" "3.7.0")
("summary" "JSON encoder/decoder for PicoLisp")
("source" "https://github.com/aw/picolisp-json.git")
("author" "Alexander Williams")
Expand Down
12 changes: 11 additions & 1 deletion test/test_regressions.l
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@
(decode "{\"test\":\"/\"}")
"Regression test GH issue #12 - Invalid encoding of quote and solidus (\\ and \") characters (6)" ) ]

# Invalid encoding of control characters 01-0x1F - https://github.com/aw/picolisp-json/issues/13
[de test-gh-issue-13 ()
(assert-equal "{\"test\":\"\\u0001\\u001E\\u001F \"}"
(encode (decode "../test7.json" T))
"Regression test GH issue #13 - Invalid encoding of control characters 0x01-0x1F (1)" )
(assert-equal "{\"test\":\"\\u0001\\u001E\\u001F \"}"
(encode (list (cons "test" (pack "^A^^^_" (char 32)))))
"Regression test GH issue #13 - Invalid encoding of control characters 0x01-0x1F (2)" ) ]

[execute
'(test-gh-issue-4)
'(test-gh-issue-5)
Expand All @@ -104,4 +113,5 @@
(when symbols '(test-gh-issue-9))
'(test-gh-issue-10)
'(test-gh-issue-11)
'(test-gh-issue-12) ]
'(test-gh-issue-12)
'(test-gh-issue-13) ]
1 change: 1 addition & 0 deletions test7.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"test":"\u0001\u001E\u001F "}

0 comments on commit 7cc6eac

Please sign in to comment.