From 37d61167ea80e3d7985e984ff9259b5dc7108831 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 2 Oct 2016 18:50:37 +0200 Subject: [PATCH] - hotfix for a RapidJSON bug: If the Writer tries to process an INF or NaN value, it aborts and leaves the writer in a broken state, unable to recover. Changed so that it writes a 0 value so that the resulting JSON at least parses correctly. --- src/rapidjson/writer.h | 42 +++++++++++++++++++++++++----------------- src/serializer.cpp | 4 ++-- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/rapidjson/writer.h b/src/rapidjson/writer.h index c5a3b98a92..369e7e6ff0 100644 --- a/src/rapidjson/writer.h +++ b/src/rapidjson/writer.h @@ -322,22 +322,30 @@ protected: bool WriteDouble(double d) { if (internal::Double(d).IsNanOrInf()) { - if (!(writeFlags & kWriteNanAndInfFlag)) - return false; - if (internal::Double(d).IsNan()) { - PutReserve(*os_, 3); - PutUnsafe(*os_, 'N'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'N'); - return true; - } - if (internal::Double(d).Sign()) { - PutReserve(*os_, 9); - PutUnsafe(*os_, '-'); - } - else - PutReserve(*os_, 8); - PutUnsafe(*os_, 'I'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'f'); - PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'y'); - return true; + bool ret = true; + if (!(writeFlags & kWriteNanAndInfFlag)) + { + // if we abort here, the writer is left in a broken state, unable to recover, so better write a 0 in addition to returning an error. + ret = false; + d = 0; + } + else + { + if (internal::Double(d).IsNan()) { + PutReserve(*os_, 3); + PutUnsafe(*os_, 'N'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'N'); + return true; + } + if (internal::Double(d).Sign()) { + PutReserve(*os_, 9); + PutUnsafe(*os_, '-'); + } + else + PutReserve(*os_, 8); + PutUnsafe(*os_, 'I'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'f'); + PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'y'); + return true; + } } char buffer[25]; @@ -345,7 +353,7 @@ protected: PutReserve(*os_, static_cast(end - buffer)); for (char* p = buffer; p != end; ++p) PutUnsafe(*os_, static_cast(*p)); - return true; + return ret; } bool WriteString(const Ch* str, SizeType length) { diff --git a/src/serializer.cpp b/src/serializer.cpp index a4714292a6..7861701c01 100644 --- a/src/serializer.cpp +++ b/src/serializer.cpp @@ -217,11 +217,11 @@ struct FWriter { if (mWriter1) { - if (!mWriter1->Double(k)) mWriter1->Double(0); + mWriter1->Double(k); } else if (mWriter2) { - if (!mWriter2->Double(k)) mWriter2->Double(0); + mWriter2->Double(k); } }