From 46030642fa93a0f56ecf4b3785b8c7cb133c5f85 Mon Sep 17 00:00:00 2001 From: Edward Lee Date: Tue, 12 May 2015 12:46:23 -0400 Subject: [PATCH] bytes->jsexpr: toss exn:fail:contract? when given invalid UTF-8 --- pkgs/racket-test/tests/json/json.rkt | 3 +++ racket/collects/json/main.rkt | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pkgs/racket-test/tests/json/json.rkt b/pkgs/racket-test/tests/json/json.rkt index 28fc492673..4c6491cae3 100644 --- a/pkgs/racket-test/tests/json/json.rkt +++ b/pkgs/racket-test/tests/json/json.rkt @@ -119,6 +119,9 @@ (string->jsexpr "") => eof (string->jsexpr " \t\r\n") => eof + ;; Invalid UTF-8 input. + (bytes->jsexpr #"\"\377\377\377\"") =error> exn:fail:contract? + ;; More string escapes: (string->jsexpr @T{ "hel\"lo" }) => "hel\"lo" (string->jsexpr @T{ "\\//\\\\//" }) => "\\//\\\\//" diff --git a/racket/collects/json/main.rkt b/racket/collects/json/main.rkt index 10142acd06..e65cabdc38 100644 --- a/racket/collects/json/main.rkt +++ b/racket/collects/json/main.rkt @@ -116,7 +116,7 @@ ;; Reading a string *could* have been nearly trivial using the racket ;; reader, except that it won't handle a "\/"... (define (read-string) - (define result (open-output-string)) + (define result (open-output-bytes)) (let loop () (define esc (let loop () @@ -127,7 +127,7 @@ [(= c 92) (read-bytes 1 i)] ;; 92 = \ [else (write-byte c result) (loop)]))) (cond - [(not esc) (get-output-string result)] + [(not esc) (bytes->string/utf-8 (get-output-bytes result))] [(assoc esc '([#"b" . #"\b"] [#"n" . #"\n"] [#"r" . #"\r"] [#"f" . #"\f"] [#"t" . #"\t"] [#"\\" . #"\\"] [#"\"" . #"\""] [#"/" . #"/"]))