summaryrefslogtreecommitdiffhomepage
path: root/libs/luci-lib-jsonc/src
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2023-10-25 22:35:08 +0200
committerJo-Philipp Wich <jo@mein.io>2023-10-25 22:42:23 +0200
commitefcd4879781ee6c428b467052957534a7919d36d (patch)
tree69154c7631f0d9e16b597de7b48464d1a365db14 /libs/luci-lib-jsonc/src
parent425bcfcb5285a1e1d3bea2680afbc21030dfd23e (diff)
luci-lib-jsonc: improve handling of integral numeric values
Properly deal with integral numeric values exceeding the int32_t range by replacing the cast logic with more fine grained checks: - Lua numbers which are integers in the first place are directly converted to JSON integers - Finite double Lua numbers which are integral are converted to JSON integer values - All other numeric values are converted to JSON doubles This should bring the handling of large integral value in line with the documented behavior of turning non-fractional Lua numbers into JSON integers. Fixes: #6647 Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'libs/luci-lib-jsonc/src')
-rw-r--r--libs/luci-lib-jsonc/src/jsonc.c8
1 files changed, 5 insertions, 3 deletions
diff --git a/libs/luci-lib-jsonc/src/jsonc.c b/libs/luci-lib-jsonc/src/jsonc.c
index 74839dd4f5..5abb738f5f 100644
--- a/libs/luci-lib-jsonc/src/jsonc.c
+++ b/libs/luci-lib-jsonc/src/jsonc.c
@@ -294,7 +294,7 @@ static bool visited(struct seen **sp, const void *ptr) {
static struct json_object * _lua_to_json_rec(lua_State *L, int index,
struct seen **seen)
{
- lua_Number nd, ni;
+ lua_Number nd;
struct json_object *obj;
const char *key;
int i, max;
@@ -364,10 +364,12 @@ static struct json_object * _lua_to_json_rec(lua_State *L, int index,
return json_object_new_boolean(lua_toboolean(L, index));
case LUA_TNUMBER:
+ if (lua_isinteger(L, index))
+ return json_object_new_int64(lua_tointeger(L, index));
+
nd = lua_tonumber(L, index);
- ni = lua_tointeger(L, index);
- if (nd == ni)
+ if (isfinite(nd) && trunc(nd) == nd)
return json_object_new_int64(nd);
return json_object_new_double(nd);