From ef7b08ce7f53aeebc1313c214a2ac1e860dbb309 Mon Sep 17 00:00:00 2001 From: Kurt Schwehr Date: Tue, 8 Aug 2017 07:54:38 -0700 Subject: [PATCH] Clamp double to int32 when narrowing in json_object_get_int. Avoids undefined behavior. Found by autofuzz. --- json_object.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/json_object.c b/json_object.c index 8c80426fa9..7148731bce 100644 --- a/json_object.c +++ b/json_object.c @@ -635,6 +635,10 @@ int32_t json_object_get_int(const struct json_object *jso) return INT32_MAX; return (int32_t) cint64; case json_type_double: + if (jso->o.c_double <= INT32_MIN) + return INT32_MIN; + if (jso->o.c_double >= INT32_MAX) + return INT32_MAX; return (int32_t)jso->o.c_double; case json_type_boolean: return jso->o.c_boolean;