summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorForLoveOfCats <floc@unpromptedtirade.com>2022-01-19 11:40:18 -0500
committerLinus Groh <mail@linusgroh.de>2022-01-19 21:51:09 +0000
commit1d95fd54433c0f561f6efaac007b12a9fcd22a0b (patch)
tree315bb6dd15d8bdbaf37fe0b4fdcc10b5590eb6e0
parent7bea0d501e41ebed294b71417ee8f732a8905463 (diff)
downloadserenity-1d95fd54433c0f561f6efaac007b12a9fcd22a0b.zip
AK: Identify negative zero when parsing Json and represent with a double
Regardless of the backing type that the number would otherwise parse to, if it is zero and the sign was supposed to be negative then it needs to be a floating point number to represent the correct value.
-rw-r--r--AK/JsonParser.cpp8
1 files changed, 8 insertions, 0 deletions
diff --git a/AK/JsonParser.cpp b/AK/JsonParser.cpp
index ce2caf1f27..bf27305938 100644
--- a/AK/JsonParser.cpp
+++ b/AK/JsonParser.cpp
@@ -191,6 +191,7 @@ ErrorOr<JsonValue> JsonParser::parse_number()
Vector<char, 128> fraction_buffer;
bool is_double = false;
+ bool all_zero = true;
for (;;) {
char ch = peek();
if (ch == '.') {
@@ -202,6 +203,9 @@ ErrorOr<JsonValue> JsonParser::parse_number()
continue;
}
if (ch == '-' || (ch >= '0' && ch <= '9')) {
+ if (ch != '-' && ch != '0')
+ all_zero = false;
+
if (is_double) {
if (ch == '-')
return Error::from_string_literal("JsonParser: Error while parsing number"sv);
@@ -229,6 +233,10 @@ ErrorOr<JsonValue> JsonParser::parse_number()
StringView number_string(number_buffer.data(), number_buffer.size());
#ifndef KERNEL
+ // Check for negative zero which needs to be forced to be represented with a double
+ if (number_string.starts_with('-') && all_zero)
+ return JsonValue(-0.0);
+
if (is_double) {
// FIXME: This logic looks shaky.
int whole = 0;