Skip to content

Commit

Permalink
json.reader: fix segfault when parsing non-JSON documents.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Jul 29, 2015
1 parent 922cbeb commit 663fba1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
7 changes: 5 additions & 2 deletions basis/json/reader/reader-tests.factor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
USING: hashtables io.streams.string json json.reader kernel
literals math strings tools.test ;
USING: hashtables io.streams.string json json.reader
json.reader.private kernel literals math strings tools.test ;
IN: json.reader.tests

{ f } [ "false" json> ] unit-test
Expand Down Expand Up @@ -76,3 +76,6 @@ ${ { 0xabcd } >string } [ " \"\\uaBCd\" " json> ] unit-test
{ 1/0. } [ "Infinity" json> ] unit-test
{ -1/0. } [ "-Infinity" json> ] unit-test
{ t } [ "NaN" json> fp-nan? ] unit-test

[ "<!doctype html>\n<html>\n<head>\n " json> ]
[ not-a-json-number? ] must-fail-with
16 changes: 10 additions & 6 deletions basis/json/reader/reader.factor
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ IN: json.reader

<PRIVATE

ERROR: not-a-json-number string ;

: json-number ( char stream -- num char )
[ 1string ] [ "\s\t\r\n,:}]" swap stream-read-until ] bi*
[ append {
{ "Infinity" [ 1/0. ] }
{ "-Infinity" [ -1/0. ] }
{ "NaN" [ 0/0. ] }
[ string>number ]
} case ] dip ;
[
append {
{ "Infinity" [ 1/0. ] }
{ "-Infinity" [ -1/0. ] }
{ "NaN" [ 0/0. ] }
[ dup string>number [ nip ] [ not-a-json-number ] if* ]
} case
] dip ;

: json-expect ( token stream -- )
[ dup length ] [ stream-read ] bi* = [ json-error ] unless ; inline
Expand Down

0 comments on commit 663fba1

Please sign in to comment.