From patchwork Sun Jan 12 22:55:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Petr_=C5=A0tetiar?= X-Patchwork-Id: 1221901 X-Patchwork-Delegate: ynezz@true.cz Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=true.cz Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20170209 header.b=WMdfqLYX; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47wsWs40RMz9s29 for ; Mon, 13 Jan 2020 09:55:53 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Subject:MIME-Version:References: In-Reply-To:Message-Id:Date:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=0a3bKCo6ejd3EEKPNLSHe29ehq7sQXfC/CsUuDpsx+U=; b=WMdfqLYX4NaqbL 5oZXFzAagSD11PchzuNcRNN7oCxHqbRcJdBFHrYVDIO1UBc16Jqo0rs3S3/Tuim5GYpkgfIdWBwT+ kQ5KcCZtEReV4aqNEkM1SDN3xM/IvprQi3Twb5IbH0Q0t1lFwH2r53rbja22zNwYde8nDnfGnnr1a e4GqprQOF1drf0qh4wxHmLbycIxsM2um+SkCo6UYUsdNCQN9b+COebnCQgACtIrIsuNOIp9C8Q53j 0hjrcyvApaYOduyxrKo2Jjfe29sWAyopxaax7iEbMytuHtF0bt21IylTupQpzDgBO1FP0s1F72Aix c105Rw3ViMrH/MnkFsDA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1iqm98-0001d6-8G; Sun, 12 Jan 2020 22:55:50 +0000 Received: from smtp-out.xnet.cz ([178.217.244.18]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1iqm8m-0001Cl-LC for openwrt-devel@lists.openwrt.org; Sun, 12 Jan 2020 22:55:31 +0000 Received: from meh.true.cz (meh.true.cz [108.61.167.218]) (Authenticated sender: petr@true.cz) by smtp-out.xnet.cz (Postfix) with ESMTPSA id F2995378D; Sun, 12 Jan 2020 23:55:22 +0100 (CET) Received: by meh.true.cz (OpenSMTPD) with ESMTP id fa2145ee; Sun, 12 Jan 2020 23:55:12 +0100 (CET) From: =?utf-8?q?Petr_=C5=A0tetiar?= To: openwrt-devel@lists.openwrt.org Date: Sun, 12 Jan 2020 23:55:15 +0100 Message-Id: <20200112225515.11654-5-ynezz@true.cz> In-Reply-To: <20200112225515.11654-1-ynezz@true.cz> References: <20200112225515.11654-1-ynezz@true.cz> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200112_145529_006085_46FB7DF1 X-CRM114-Status: UNSURE ( 7.17 ) X-CRM114-Notice: Please train this message. X-Spam-Score: 0.0 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (0.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [178.217.244.18 listed in list.dnswl.org] 0.0 SPF_NONE SPF: sender does not publish an SPF Record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record Subject: [OpenWrt-Devel] [PATCH libubox 4/4] blobmsg: blobmsg_add_json_element() 64-bit values X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Dainis Jonitis , =?utf-8?q?Petr_=C5=A0tetiar?= Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org From: Dainis Jonitis libjson-c json_type_int values are stored as int64_t. Use json_object_get_int64() instead of json_object_get_int() to avoid clamping to INT32_MAX. Signed-off-by: Dainis Jonitis [fixed author to match SoB, added unit test results] Signed-off-by: Petr Štetiar --- blobmsg_json.c | 10 ++++++++-- tests/cram/test_blobmsg.t | 32 ++++++++++++++++---------------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/blobmsg_json.c b/blobmsg_json.c index aee7a64fbca7..aedc2da22ebc 100644 --- a/blobmsg_json.c +++ b/blobmsg_json.c @@ -66,9 +66,15 @@ bool blobmsg_add_json_element(struct blob_buf *b, const char *name, json_object case json_type_boolean: blobmsg_add_u8(b, name, json_object_get_boolean(obj)); break; - case json_type_int: - blobmsg_add_u32(b, name, json_object_get_int(obj)); + case json_type_int: { + int64_t i64 = json_object_get_int64(obj); + if (i64 >= INT32_MIN && i64 <= INT32_MAX) { + blobmsg_add_u32(b, name, (uint32_t)i64); + } else { + blobmsg_add_u64(b, name, (uint64_t)i64); + } break; + } case json_type_double: blobmsg_add_double(b, name, json_object_get_double(obj)); break; diff --git a/tests/cram/test_blobmsg.t b/tests/cram/test_blobmsg.t index 84ec143d6ff1..74ad326931fe 100644 --- a/tests/cram/test_blobmsg.t +++ b/tests/cram/test_blobmsg.t @@ -46,8 +46,8 @@ check that blobmsg is producing expected results: 32767 (i32) -2147483648 (i32) 2147483647 (i32) - -2147483648 (i32) - 2147483647 (i32) + -9223372036854775808 (i64) + 9223372036854775807 (i64) 133.700000 (dbl) } Testdata: { @@ -60,8 +60,8 @@ check that blobmsg is producing expected results: \tbar-max : 32767 (i32) (esc) \tbaz-min : -2147483648 (i32) (esc) \tbaz-max : 2147483647 (i32) (esc) - \ttaz-min : -2147483648 (i32) (esc) - \ttaz-max : 2147483647 (i32) (esc) + \ttaz-min : -9223372036854775808 (i64) (esc) + \ttaz-max : 9223372036854775807 (i64) (esc) \tworld : 2 (str) (esc) } @@ -109,8 +109,8 @@ check that blobmsg is producing expected results: 32767 (i32) -2147483648 (i32) 2147483647 (i32) - -2147483648 (i32) - 2147483647 (i32) + -9223372036854775808 (i64) + 9223372036854775807 (i64) 133.700000 (dbl) } Testdata: { @@ -123,8 +123,8 @@ check that blobmsg is producing expected results: \tbar-max : 32767 (i32) (esc) \tbaz-min : -2147483648 (i32) (esc) \tbaz-max : 2147483647 (i32) (esc) - \ttaz-min : -2147483648 (i32) (esc) - \ttaz-max : 2147483647 (i32) (esc) + \ttaz-min : -9223372036854775808 (i64) (esc) + \ttaz-max : 9223372036854775807 (i64) (esc) \tworld : 2 (str) (esc) } @@ -172,8 +172,8 @@ check that blobmsg is producing expected results: 32767 (i32) -2147483648 (i32) 2147483647 (i32) - -2147483648 (i32) - 2147483647 (i32) + -9223372036854775808 (i64) + 9223372036854775807 (i64) 133.700000 (dbl) } Testdata: { @@ -186,8 +186,8 @@ check that blobmsg is producing expected results: \tbar-max : 32767 (i32) (esc) \tbaz-min : -2147483648 (i32) (esc) \tbaz-max : 2147483647 (i32) (esc) - \ttaz-min : -2147483648 (i32) (esc) - \ttaz-max : 2147483647 (i32) (esc) + \ttaz-min : -9223372036854775808 (i64) (esc) + \ttaz-max : 9223372036854775807 (i64) (esc) \tworld : 2 (str) (esc) } @@ -235,8 +235,8 @@ check that blobmsg is producing expected results: 32767 (i32) -2147483648 (i32) 2147483647 (i32) - -2147483648 (i32) - 2147483647 (i32) + -9223372036854775808 (i64) + 9223372036854775807 (i64) 133.700000 (dbl) } Testdata: { @@ -249,7 +249,7 @@ check that blobmsg is producing expected results: \tbar-max : 32767 (i32) (esc) \tbaz-min : -2147483648 (i32) (esc) \tbaz-max : 2147483647 (i32) (esc) - \ttaz-min : -2147483648 (i32) (esc) - \ttaz-max : 2147483647 (i32) (esc) + \ttaz-min : -9223372036854775808 (i64) (esc) + \ttaz-max : 9223372036854775807 (i64) (esc) \tworld : 2 (str) (esc) }