json.reader: fix segfault when parsing non-JSON documents.

db4
John Benediktsson 2015-07-28 18:10:24 -07:00
parent 922cbebf8a
commit 663fba1d7d
2 changed files with 15 additions and 8 deletions

View File

@ -1,5 +1,5 @@
USING: hashtables io.streams.string json json.reader kernel USING: hashtables io.streams.string json json.reader
literals math strings tools.test ; json.reader.private kernel literals math strings tools.test ;
IN: json.reader.tests IN: json.reader.tests
{ f } [ "false" json> ] unit-test { f } [ "false" json> ] unit-test
@ -76,3 +76,6 @@ ${ { 0xabcd } >string } [ " \"\\uaBCd\" " json> ] unit-test
{ 1/0. } [ "Infinity" json> ] unit-test { 1/0. } [ "Infinity" json> ] unit-test
{ -1/0. } [ "-Infinity" json> ] unit-test { -1/0. } [ "-Infinity" json> ] unit-test
{ t } [ "NaN" json> fp-nan? ] unit-test { t } [ "NaN" json> fp-nan? ] unit-test
[ "<!doctype html>\n<html>\n<head>\n " json> ]
[ not-a-json-number? ] must-fail-with

View File

@ -9,14 +9,18 @@ IN: json.reader
<PRIVATE <PRIVATE
ERROR: not-a-json-number string ;
: json-number ( char stream -- num char ) : json-number ( char stream -- num char )
[ 1string ] [ "\s\t\r\n,:}]" swap stream-read-until ] bi* [ 1string ] [ "\s\t\r\n,:}]" swap stream-read-until ] bi*
[ append { [
{ "Infinity" [ 1/0. ] } append {
{ "-Infinity" [ -1/0. ] } { "Infinity" [ 1/0. ] }
{ "NaN" [ 0/0. ] } { "-Infinity" [ -1/0. ] }
[ string>number ] { "NaN" [ 0/0. ] }
} case ] dip ; [ dup string>number [ nip ] [ not-a-json-number ] if* ]
} case
] dip ;
: json-expect ( token stream -- ) : json-expect ( token stream -- )
[ dup length ] [ stream-read ] bi* = [ json-error ] unless ; inline [ dup length ] [ stream-read ] bi* = [ json-error ] unless ; inline