]> gitweb.factorcode.org Git - factor.git/commitdiff
json.reader: fix segfault when parsing non-JSON documents.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 29 Jul 2015 01:10:24 +0000 (18:10 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 29 Jul 2015 01:10:24 +0000 (18:10 -0700)
basis/json/reader/reader-tests.factor
basis/json/reader/reader.factor

index 798acbec8acf34ef43e4dcce2c55a396285e4dc8..3bef14e9d3cea648edbc590ba49cbb336edd6c48 100644 (file)
@@ -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
@@ -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
index 8028b507c8612de092a90fd76ebca1e800e2e616..5a8fe895a964593f072148bd8f5ca21d3533053a 100644 (file)
@@ -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