From 1fb667ec0cfd2085706e67b5ae03ecff26dd04cd Mon Sep 17 00:00:00 2001 From: AZero13 Date: Fri, 5 Dec 2025 16:39:25 -0500 Subject: [PATCH] Ensure there is a null byte at the end of the JSON In JSON5 mode, a top-level number (e.g. "123") is left without a guaranteed terminator. Do what JSONScanner does in this case. --- Sources/FoundationEssentials/JSON/JSON5Scanner.swift | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Sources/FoundationEssentials/JSON/JSON5Scanner.swift b/Sources/FoundationEssentials/JSON/JSON5Scanner.swift index d0f24d27b..0655d6395 100644 --- a/Sources/FoundationEssentials/JSON/JSON5Scanner.swift +++ b/Sources/FoundationEssentials/JSON/JSON5Scanner.swift @@ -123,7 +123,14 @@ internal struct JSON5Scanner { throw JSONError.unexpectedCharacter(context: "after top-level value", ascii: char, location: reader.sourceLocation) } - return JSONMap(mapBuffer: partialMap.mapData, dataBuffer: self.reader.bytes) + let map = JSONMap(mapBuffer: partialMap.mapData, dataBuffer: self.reader.bytes) + + // If the input contains only a number, ensure a trailing NUL byte is available for strtod/strtof parsing. + if case .number = map.loadValue(at: 0)! { + map.copyInBuffer() + } + + return map } // MARK: Generic Value Scanning