encode: Fail to dump NaN and infinity

Thanks to vim/vim#654
This commit is contained in:
ZyX
2016-02-21 21:33:58 +03:00
parent 7cdd01983a
commit 406562ac6d
2 changed files with 27 additions and 3 deletions

View File

@@ -7,6 +7,7 @@
#include <msgpack.h> #include <msgpack.h>
#include <inttypes.h> #include <inttypes.h>
#include <assert.h> #include <assert.h>
#include <math.h>
#include "nvim/eval/encode.h" #include "nvim/eval/encode.h"
#include "nvim/buffer_defs.h" // vimconv_T #include "nvim/buffer_defs.h" // vimconv_T
@@ -827,9 +828,23 @@ DEFINE_VIML_CONV_FUNCTIONS(, echo, garray_T *const, gap)
#undef CONV_FLOAT #undef CONV_FLOAT
#define CONV_FLOAT(flt) \ #define CONV_FLOAT(flt) \
do { \ do { \
const float_T flt_ = (flt); \
switch (fpclassify(flt_)) { \
case FP_NAN: { \
EMSG(_("E474: Unable to represent NaN value in JSON")); \
return FAIL; \
} \
case FP_INFINITE: { \
EMSG(_("E474: Unable to represent infinity in JSON")); \
return FAIL; \
} \
default: { \
char numbuf[NUMBUFLEN]; \ char numbuf[NUMBUFLEN]; \
vim_snprintf(numbuf, NUMBUFLEN - 1, "%g", (flt)); \ vim_snprintf(numbuf, NUMBUFLEN - 1, "%g", flt_); \
ga_concat(gap, numbuf); \ ga_concat(gap, (char_u *) numbuf); \
break; \
} \
} \
} while (0) } while (0)
/// Last used p_enc value /// Last used p_enc value

View File

@@ -472,6 +472,15 @@ describe('json_encode() function', function()
eq('1.0e50', eval('json_encode(1.0e50)')) eq('1.0e50', eval('json_encode(1.0e50)'))
end) end)
it('fails to dump NaN and infinite values', function()
eq('Vim(call):E474: Unable to represent NaN value in JSON',
exc_exec('call json_encode(str2float("nan"))'))
eq('Vim(call):E474: Unable to represent infinity in JSON',
exc_exec('call json_encode(str2float("inf"))'))
eq('Vim(call):E474: Unable to represent infinity in JSON',
exc_exec('call json_encode(-str2float("inf"))'))
end)
it('dumps lists', function() it('dumps lists', function()
eq('[]', funcs.json_encode({})) eq('[]', funcs.json_encode({}))
eq('[[]]', funcs.json_encode({{}})) eq('[[]]', funcs.json_encode({{}}))