From 630bb3f16f27c72e3fc2daf3161c96f0fd9c1ba0 Mon Sep 17 00:00:00 2001 From: Mark Pulford Date: Tue, 29 Nov 2011 22:47:35 +1030 Subject: Add workaround to handle platforms that print -nan --- lua_cjson.c | 17 +++++++++++------ tests/test.lua | 4 ---- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lua_cjson.c b/lua_cjson.c index 1226e71..806a67c 100644 --- a/lua_cjson.c +++ b/lua_cjson.c @@ -623,12 +623,17 @@ static void json_append_number(lua_State *l, strbuf_t *json, int index, if (cfg->encode_refuse_badnum && (isinf(num) || isnan(num))) json_encode_exception(l, cfg, index, "must not be NaN or Inf"); - /* Lowest double printed with %.14g is 21 characters long: - * -1.7976931348623e+308 - * - * Use 32 to include the \0, and a few extra just in case.. - */ - strbuf_append_fmt(json, 32, cfg->number_fmt, num); + if (isnan(num)) { + /* Some platforms may print -nan, just hard code it */ + strbuf_append_mem(json, "nan", 3); + } else { + /* Lowest double printed with %.14g is 21 characters long: + * -1.7976931348623e+308 + * + * Use 32 to include the \0, and a few extra just in case.. + */ + strbuf_append_fmt(json, 32, cfg->number_fmt, num); + } } static void json_append_object(lua_State *l, json_config_t *cfg, diff --git a/tests/test.lua b/tests/test.lua index d80dcf0..02d3760 100755 --- a/tests/test.lua +++ b/tests/test.lua @@ -150,10 +150,6 @@ local encode_error_tests = { json.refuse_invalid_numbers(false) return 'Setting refuse_invalid_numbers(false).' end, - function () - print('NOTE: receiving "-nan" in the following test is ok..') - return - end, { json.encode, { NaN }, true, { "nan" } }, { json.encode, { Inf }, true, { "inf" } }, function () -- cgit v1.2.3-55-g6feb