All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: alin.nastac@gmail.com
Cc: netdev@vger.kernel.org, Stephen Hemminger <stephen@networkplumber.org>
Subject: [RFC iproute 2/5] json: make json print_uint take unsigned int
Date: Fri, 20 Apr 2018 10:15:16 -0700	[thread overview]
Message-ID: <20180420171519.8028-3-stephen@networkplumber.org> (raw)
In-Reply-To: <20180420171519.8028-1-stephen@networkplumber.org>

It is a source of possible bugs that json print handler print_uint
was doing implict conversion to 64 bit than printing with the
format specifier which often had only a unsigned format value.

Instead introduce wider range of print stubs for unsigned
integer types. No color versions of these necessary.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 include/json_print.h | 16 +++++++++++++---
 lib/json_print.c     | 33 ++++++++++++++++++++++++++++++---
 2 files changed, 43 insertions(+), 6 deletions(-)

diff --git a/include/json_print.h b/include/json_print.h
index 2ca7830adbd6..3d400ecf8f50 100644
--- a/include/json_print.h
+++ b/include/json_print.h
@@ -59,12 +59,22 @@ _PRINT_FUNC(int, int);
 _PRINT_FUNC(bool, bool);
 _PRINT_FUNC(null, const char*);
 _PRINT_FUNC(string, const char*);
-_PRINT_FUNC(uint, uint64_t);
-_PRINT_FUNC(hu, unsigned short);
 _PRINT_FUNC(hex, unsigned int);
 _PRINT_FUNC(0xhex, unsigned int);
-_PRINT_FUNC(lluint, unsigned long long int);
 _PRINT_FUNC(float, double);
 #undef _PRINT_FUNC
 
+#define _PRINT_FUNC(type_name, type)			\
+	void print_##type_name(enum output_type t,	\
+			       const char *key,		\
+			       const char *fmt,		\
+			       type value);
+
+_PRINT_FUNC(hu, unsigned short);
+_PRINT_FUNC(uint, unsigned int);
+_PRINT_FUNC(luint, unsigned long int);
+_PRINT_FUNC(uint64, uint64_t);
+_PRINT_FUNC(lluint, unsigned long long int);
+#undef _PRINT_FUNC
+
 #endif /* _JSON_PRINT_H_ */
diff --git a/lib/json_print.c b/lib/json_print.c
index bda7293350c3..696d8c01d3e6 100644
--- a/lib/json_print.c
+++ b/lib/json_print.c
@@ -116,12 +116,39 @@ void close_json_array(enum output_type type, const char *str)
 		}							\
 	}
 _PRINT_FUNC(int, int);
-_PRINT_FUNC(hu, unsigned short);
-_PRINT_FUNC(uint, uint64_t);
-_PRINT_FUNC(lluint, unsigned long long int);
 _PRINT_FUNC(float, double);
 #undef _PRINT_FUNC
 
+static void json_print_uint64(const char *key, uint64_t val)
+{
+	if (key)
+		jsonw_uint_field(_jw, key, val);
+	else
+		jsonw_uint(_jw, val);
+}
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
+#define PRINT_FUNC(type_name, type)		\
+	void print_##type_name(enum output_type t, \
+				const char *key,	\
+				const char *fmt,	\
+				type value)		\
+	{						\
+		if (_IS_JSON_CONTEXT(t))		\
+			json_print_uint64(key, value);	\
+		else if (_IS_FP_CONTEXT(t))		\
+			printf(fmt, value);		\
+	}
+
+PRINT_FUNC(hu, unsigned short);
+PRINT_FUNC(uint, unsigned int);
+PRINT_FUNC(lluint, unsigned long long int);
+PRINT_FUNC(luint, unsigned long int);
+PRINT_FUNC(uint64, uint64_t);
+#undef PRINT_FUNC
+#pragma GCC diagnostic pop
+
 void print_color_string(enum output_type type,
 			enum color_attr color,
 			const char *key,
-- 
2.17.0

  parent reply	other threads:[~2018-04-20 17:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-20 17:15 [RFC iproute 0/5] print_uint issues Stephen Hemminger
2018-04-20 17:15 ` [RFC iproute 1/5] ipneigh: fix missing format specifier Stephen Hemminger
2018-04-20 17:15 ` Stephen Hemminger [this message]
2018-04-20 17:15 ` [RFC iproute 3/5] flower: use 16 bit format where possible Stephen Hemminger
2018-04-20 17:15 ` [RFC iproute 4/5] tcp_metrics: use print_luint Stephen Hemminger
2018-04-20 17:15 ` [RFC iproute 5/5] mroute: use print_uint64 Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180420171519.8028-3-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=alin.nastac@gmail.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.