Prevent irregular real numbers from being treated as valid JSON values.

Fixes PR 14628.
This commit is contained in:
Eli Barzilay 2014-10-13 10:59:59 -04:00 committed by Matthew Flatt
parent bc5d86f494
commit e52f273491
2 changed files with 8 additions and 2 deletions

View File

@ -35,6 +35,9 @@
(not (jsexpr? '#hasheq([1 . 1])))
(not (jsexpr? '#hasheq(["x" . 1])))
(not (jsexpr? '#hasheq(['() . 1])))
(not (jsexpr? (/ 1.0 0.0)))
(not (jsexpr? (/ -1.0 0.0)))
(not (jsexpr? (/ 0.0 0.0)))
)
;; other `null' values
(parameterize ([json-null #\null])

View File

@ -18,7 +18,7 @@
(define (jsexpr? x #:null [jsnull (json-null)])
(let loop ([x x])
(or (exact-integer? x)
(inexact-real? x)
(real-real? x)
(boolean? x)
(string? x)
(eq? x jsnull)
@ -26,6 +26,9 @@
(and (hash? x) (for/and ([(k v) (in-hash x)])
(and (symbol? k) (loop v)))))))
(define (real-real? x) ; not nan or inf
(and (inexact-real? x) (not (member x '(+nan.0 +inf.0 -inf.0)))))
;; ----------------------------------------------------------------------------
;; Generation: Racket -> JSON
@ -64,7 +67,7 @@
(write-string (regexp-replace* rx-to-encode str escape) o)
(write-bytes #"\"" o))
(let loop ([x x])
(cond [(or (exact-integer? x) (inexact-real? x)) (write x o)]
(cond [(or (exact-integer? x) (real-real? x)) (write x o)]
[(eq? x #f) (write-bytes #"false" o)]
[(eq? x #t) (write-bytes #"true" o)]
[(eq? x jsnull) (write-bytes #"null" o)]