[PATCH] Avoid inserting 'inf' and friends into the json output.

Carl Worth cworth at cworth.org
Mon Jul 30 15:01:15 PDT 2012


The JSON-generating code was previously carefult to avoid emitting NaN
into its output, but it would still write "inf" for an infinite value,
(which is invalid JSON).

Fix by calling writeNull for any value for which std::isfinite returns
false, (rather than the previous test for merely (n != n)).
---
 retrace/json.hpp |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/retrace/json.hpp b/retrace/json.hpp
index 6af1f4c..e8f319c 100644
--- a/retrace/json.hpp
+++ b/retrace/json.hpp
@@ -38,7 +38,7 @@
 #include <limits>
 #include <ostream>
 #include <string>
-
+#include <cmath>
 
 class JSONWriter
 {
@@ -328,7 +328,7 @@ public:
 
     template<class T>
     inline void writeNumber(T n) {
-        if (n != n) {
+        if (! std::isfinite(n)) {
             // NaN
             writeNull();
         } else {
-- 
1.7.10



More information about the apitrace mailing list