From 77408a81a696472338c2f90147e41fff3e81005d Mon Sep 17 00:00:00 2001 From: Mark Pulford Date: Sun, 1 Jan 2012 23:30:34 +1030 Subject: Add build option to disable invalid numbers Windows MinGW doesn't convert Infinity/NaN/hexadecimal numbers. Add DISABLE_INVALID_NUMBERS build option option to disable invalid numbers. --- Makefile | 8 +++++--- lua-cjson-1.0devel-1.rockspec | 11 +++++++---- lua_cjson.c | 15 +++++++++++++++ manual.txt | 21 ++++++++++++--------- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 085e78a..999b877 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,10 @@ LUA_VERSION = 5.1 ## Available defines for CJSON_CFLAGS # -# USE_INTERNAL_ISINF: Workaround for Solaris platforms missing isinf(). -# DISABLE_CJSON_GLOBAL: Do not store module is "cjson" global +# USE_INTERNAL_ISINF: Workaround for Solaris platforms missing isinf(). +# DISABLE_CJSON_GLOBAL: Do not store module is "cjson" global. +# DISABLE_INVALID_NUMBERS: Permanently disable invalid JSON numbers: +# - NaN, Infinity, hex. ## Build defaults TARGET = cjson.so @@ -38,7 +40,7 @@ INSTALL_CMD = install ## Windows (MinGW) #TARGET = cjson.dll #PREFIX = /home/user/opt -#CJSON_CFLAGS = +#CJSON_CFLAGS = -DDISABLE_INVALID_NUMBERS #CJSON_LDFLAGS = -shared -L$(PREFIX)/lib -llua51 ## End platform specific section diff --git a/lua-cjson-1.0devel-1.rockspec b/lua-cjson-1.0devel-1.rockspec index fa20c53..9ef9f2c 100644 --- a/lua-cjson-1.0devel-1.rockspec +++ b/lua-cjson-1.0devel-1.rockspec @@ -25,15 +25,18 @@ build = { cjson = { sources = { "lua_cjson.c", "strbuf.c", "fpconv.c" }, defines = { --- Optional workaround: --- USE_INTERNAL_ISINF: Provide internal isinf() implementation. Required --- on some Solaris platforms. -- LuaRocks does not support platform specific configuration for Solaris. --- Uncomment the line below on Solaris platforms. +-- Uncomment the line below on Solaris platforms if required. -- "USE_INTERNAL_ISINF" } } }, + -- Override default build options (per platform) + platforms = { + win32 = { modules = { cjson = { defines = { + "DISABLE_INVALID_NUMBERS" + } } } } + }, copy_directories = { "tests" } } diff --git a/lua_cjson.c b/lua_cjson.c index 175433a..ca85156 100644 --- a/lua_cjson.c +++ b/lua_cjson.c @@ -67,6 +67,11 @@ #define DEFAULT_ENCODE_KEEP_BUFFER 1 #define DEFAULT_ENCODE_NUMBER_PRECISION 14 +#ifdef DISABLE_INVALID_NUMBERS +#undef DEFAULT_DECODE_REFUSE_BADNUM +#define DEFAULT_DECODE_REFUSE_BADNUM 1 +#endif + typedef enum { T_OBJ_BEGIN, T_OBJ_END, @@ -341,6 +346,16 @@ static int json_cfg_refuse_invalid_numbers(lua_State *l) &cfg->encode_refuse_badnum, &cfg->decode_refuse_badnum); +#if DISABLE_INVALID_NUMBERS + /* Some non-POSIX platforms don't handle double <-> string translations + * for Infinity/NaN/hexadecimal properly. Throw an error if the + * user attempts to enable them. */ + if (!cfg->encode_refuse_badnum || !cfg->decode_refuse_badnum) { + cfg->encode_refuse_badnum = cfg->decode_refuse_badnum = 1; + luaL_error(l, "Infinity, NaN, and/or hexadecimal numbers are not supported."); + } +#endif + return 1; } diff --git a/manual.txt b/manual.txt index 3ccbce3..33fcc56 100644 --- a/manual.txt +++ b/manual.txt @@ -42,15 +42,6 @@ RPM:: Linux LuaRocks:: Unix, Windows -Build Options (#define) -~~~~~~~~~~~~~~~~~~~~~~~ - -[horizontal] -USE_INTERNAL_ISINF:: Workaround for Solaris platforms missing isinf(). -DISABLE_CJSON_GLOBAL:: Do not store module table in global "cjson" - variable. Redundant from Lua 5.2 onwards. - - Make ~~~~ @@ -117,6 +108,18 @@ Review the http://luarocks.org/en/Documentation[LuaRocks documentation] for further details. +Build Options (#define) +~~~~~~~~~~~~~~~~~~~~~~~ + +[horizontal] +USE_INTERNAL_ISINF:: Workaround for Solaris platforms missing ++isinf++(3). +DISABLE_CJSON_GLOBAL:: Do not store module table in global "cjson" + variable. Redundant from Lua 5.2 onwards. +DISABLE_INVALID_NUMBERS:: Recommended on platforms where ++strtod++(3) / + ++sprintf++(3) are not POSIX compliant (Eg, Windows MinGW). Restricts + the +cjson.refuse_invalid_numbers+ runtime configuration to +true+. + + API (Functions) --------------- -- cgit v1.2.3-55-g6feb