From bdc91968a2123ea616262011aa157f93a77f2495 Mon Sep 17 00:00:00 2001 From: Ben Konyi Date: Fri, 3 May 2019 18:07:27 +0000 Subject: [PATCH] [ VM / Service ] Updated JSONWriter::PrintValue(double) to use double_conversion instead of Printf. Fixes #31737 Change-Id: If70b7b6bdd1e977bda310a2b0566dcd48c6b3c6a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/101101 Commit-Queue: Ben Konyi Reviewed-by: Ryan Macnak --- runtime/vm/json_test.cc | 2 +- runtime/vm/json_writer.cc | 8 +++++++- runtime/vm/metrics_test.cc | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/runtime/vm/json_test.cc b/runtime/vm/json_test.cc index 17463283afd..d25d01de716 100644 --- a/runtime/vm/json_test.cc +++ b/runtime/vm/json_test.cc @@ -61,7 +61,7 @@ TEST_CASE(JSON_JSONStream_Primitives) { JSONArray jsarr(&js); jsarr.AddValue(1.0); } - EXPECT_STREQ("[1.000000]", js.ToCString()); + EXPECT_STREQ("[1.0]", js.ToCString()); } { JSONStream js; diff --git a/runtime/vm/json_writer.cc b/runtime/vm/json_writer.cc index 77112ae99c6..587bf1586f3 100644 --- a/runtime/vm/json_writer.cc +++ b/runtime/vm/json_writer.cc @@ -5,6 +5,7 @@ #include "platform/assert.h" #include "platform/unicode.h" +#include "vm/double_conversion.h" #include "vm/json_writer.h" #include "vm/object.h" @@ -117,8 +118,13 @@ void JSONWriter::PrintValue64(int64_t i) { } void JSONWriter::PrintValue(double d) { + // Max length of a double in characters (including \0). + // See double_conversion.cc. + const size_t kBufferLen = 25; + char buffer[kBufferLen]; + DoubleToCString(d, buffer, kBufferLen); PrintCommaIfNeeded(); - buffer_.Printf("%f", d); + buffer_.Printf("%s", buffer); } static const char base64_digits[65] = diff --git a/runtime/vm/metrics_test.cc b/runtime/vm/metrics_test.cc index 6ca672632c0..4482a6d0faf 100644 --- a/runtime/vm/metrics_test.cc +++ b/runtime/vm/metrics_test.cc @@ -67,7 +67,7 @@ VM_UNIT_TEST_CASE(Metric_OnDemand) { "{\"type\":\"Counter\",\"name\":\"a.b.c\",\"description\":" "\"foobar\",\"unit\":\"byte\"," "\"fixedId\":true,\"id\":\"metrics\\/native\\/a.b.c\"" - ",\"value\":99.000000}", + ",\"value\":99.0}", json); } Dart_ShutdownIsolate();