js-on-sx: JSON.parse raises SyntaxError, rejects trailing content + control chars
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 43s

This commit is contained in:
2026-05-10 06:32:11 +00:00
parent 836b31a5b6
commit 769559bae7
2 changed files with 23 additions and 7 deletions

View File

@@ -5321,12 +5321,21 @@
(&rest args)
(if
(= (len args) 0)
js-undefined
(raise (js-new-call SyntaxError (js-args "Unexpected token undefined")))
(let
((st (dict)))
(dict-set! st "s" (js-to-string (nth args 0)))
(dict-set! st "i" 0)
(js-json-parse-value st)))))
(let
((result (js-json-parse-value st)))
(begin
(js-json-skip-ws! st)
(if
(< (get st "i") (len (get st "s")))
(raise
(js-new-call SyntaxError
(js-args (str "Unexpected token at position " (get st "i")))))
result)))))))
(define
js-json-skip-ws!
@@ -5348,7 +5357,7 @@
(let
((s (get st "s")) (i (get st "i")))
(cond
((>= i (len s)) (error "JSON: unexpected end"))
((>= i (len s)) (raise (js-new-call SyntaxError (js-args "JSON: unexpected end"))))
((= (char-at s i) "\"") (js-json-parse-string st))
((= (char-at s i) "[") (js-json-parse-array st))
((= (char-at s i) "{") (js-json-parse-object st))
@@ -5380,8 +5389,13 @@
(let
((i (get st "i")))
(cond
((>= i (len s)) nil)
((>= i (len s))
(raise (js-new-call SyntaxError (js-args "JSON: unterminated string"))))
((= (char-at s i) "\"") nil)
((< (char-code (char-at s i)) 32)
(raise
(js-new-call SyntaxError
(js-args "JSON: control character in string"))))
((= (char-at s i) "\\")
(begin
(when
@@ -5457,7 +5471,7 @@
(js-json-skip-ws! st)
(js-json-parse-array-loop st result)))
((= c "]") (dict-set! st "i" (+ (get st "i") 1)))
(else (error "JSON: expected , or ]"))))))
(else (raise (js-new-call SyntaxError (js-args "JSON: expected , or ]"))))))))
(define
js-json-parse-object
@@ -5482,7 +5496,7 @@
(js-json-skip-ws! st)
(when
(not (= (char-at (get st "s") (get st "i")) ":"))
(error "JSON: expected :"))
(raise (js-new-call SyntaxError (js-args "JSON: expected :"))))
(dict-set! st "i" (+ (get st "i") 1))
(let ((v (js-json-parse-value st))) (dict-set! result k v))
(js-json-skip-ws! st)
@@ -5494,7 +5508,7 @@
(dict-set! st "i" (+ (get st "i") 1))
(js-json-parse-object-loop st result)))
((= c "}") (dict-set! st "i" (+ (get st "i") 1)))
(else (error "JSON: expected , or }")))))))
(else (raise (js-new-call SyntaxError (js-args "JSON: expected , or }")))))))))
(define JSON {:stringify js-json-stringify :parse js-json-parse})