1 From 8de9593bb9dc05cb1be593a237682e8707e41aa9 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Toke=20H=C3=B8iland-J=C3=B8rgensen?= <toke@toke.dk>
3 Date: Wed, 25 Apr 2018 16:19:35 +0200
4 Subject: [PATCH] json_print: Fix hidden 64-bit type promotion
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 print_uint() will silently promote its variable type to uint64_t, but there
10 is nothing that ensures that the format string specifier passed along with
11 it fits (and the function name suggest to pass "%u").
13 Fix this by changing print_uint() to use a native 'unsigned int' type, and
14 introduce a separate print_u64() function for printing 64-bit values. All
15 call sites that were actually printing 64-bit values using print_uint() are
16 converted to use print_u64() instead.
18 Since print_int() was already using native int types, just add a
19 print_s64() to match, but don't convert any call sites.
21 Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
22 Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
24 include/json_print.h | 4 +++-
25 include/json_writer.h | 12 ++++++----
26 ip/ipaddress.c | 62 +++++++++++++++++++++++++--------------------------
27 ip/ipmacsec.c | 8 +++----
28 ip/ipmroute.c | 6 ++---
29 lib/json_print.c | 4 +++-
30 lib/json_writer.c | 30 +++++++++++++++++++++----
31 7 files changed, 78 insertions(+), 48 deletions(-)
33 --- a/include/json_print.h
34 +++ b/include/json_print.h
35 @@ -56,10 +56,12 @@ void close_json_array(enum output_type t
36 print_color_##type_name(t, COLOR_NONE, key, fmt, value); \
38 _PRINT_FUNC(int, int);
39 +_PRINT_FUNC(s64, int64_t);
40 _PRINT_FUNC(bool, bool);
41 _PRINT_FUNC(null, const char*);
42 _PRINT_FUNC(string, const char*);
43 -_PRINT_FUNC(uint, uint64_t);
44 +_PRINT_FUNC(uint, unsigned int);
45 +_PRINT_FUNC(u64, uint64_t);
46 _PRINT_FUNC(hu, unsigned short);
47 _PRINT_FUNC(hex, unsigned int);
48 _PRINT_FUNC(0xhex, unsigned int);
49 --- a/include/json_writer.h
50 +++ b/include/json_writer.h
51 @@ -34,9 +34,11 @@ void jsonw_string(json_writer_t *self, c
52 void jsonw_bool(json_writer_t *self, bool value);
53 void jsonw_float(json_writer_t *self, double number);
54 void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num);
55 -void jsonw_uint(json_writer_t *self, uint64_t number);
56 +void jsonw_uint(json_writer_t *self, unsigned int number);
57 +void jsonw_u64(json_writer_t *self, uint64_t number);
58 void jsonw_hu(json_writer_t *self, unsigned short number);
59 -void jsonw_int(json_writer_t *self, int64_t number);
60 +void jsonw_int(json_writer_t *self, int number);
61 +void jsonw_s64(json_writer_t *self, int64_t number);
62 void jsonw_null(json_writer_t *self);
63 void jsonw_lluint(json_writer_t *self, unsigned long long int num);
65 @@ -44,9 +46,11 @@ void jsonw_lluint(json_writer_t *self, u
66 void jsonw_string_field(json_writer_t *self, const char *prop, const char *val);
67 void jsonw_bool_field(json_writer_t *self, const char *prop, bool value);
68 void jsonw_float_field(json_writer_t *self, const char *prop, double num);
69 -void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num);
70 +void jsonw_uint_field(json_writer_t *self, const char *prop, unsigned int num);
71 +void jsonw_u64_field(json_writer_t *self, const char *prop, uint64_t num);
72 void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num);
73 -void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num);
74 +void jsonw_int_field(json_writer_t *self, const char *prop, int num);
75 +void jsonw_s64_field(json_writer_t *self, const char *prop, int64_t num);
76 void jsonw_null_field(json_writer_t *self, const char *prop);
77 void jsonw_lluint_field(json_writer_t *self, const char *prop,
78 unsigned long long int num);
81 @@ -555,21 +555,21 @@ static void print_vf_stats64(FILE *fp, s
84 open_json_object("rx");
85 - print_uint(PRINT_JSON, "bytes", NULL,
86 + print_u64(PRINT_JSON, "bytes", NULL,
87 rta_getattr_u64(vf[IFLA_VF_STATS_RX_BYTES]));
88 - print_uint(PRINT_JSON, "packets", NULL,
89 + print_u64(PRINT_JSON, "packets", NULL,
90 rta_getattr_u64(vf[IFLA_VF_STATS_RX_PACKETS]));
91 - print_uint(PRINT_JSON, "multicast", NULL,
92 + print_u64(PRINT_JSON, "multicast", NULL,
93 rta_getattr_u64(vf[IFLA_VF_STATS_MULTICAST]));
94 - print_uint(PRINT_JSON, "broadcast", NULL,
95 + print_u64(PRINT_JSON, "broadcast", NULL,
96 rta_getattr_u64(vf[IFLA_VF_STATS_BROADCAST]));
100 open_json_object("tx");
101 - print_uint(PRINT_JSON, "tx_bytes", NULL,
102 + print_u64(PRINT_JSON, "tx_bytes", NULL,
103 rta_getattr_u64(vf[IFLA_VF_STATS_TX_BYTES]));
104 - print_uint(PRINT_JSON, "tx_packets", NULL,
105 + print_u64(PRINT_JSON, "tx_packets", NULL,
106 rta_getattr_u64(vf[IFLA_VF_STATS_TX_PACKETS]));
109 @@ -602,50 +602,50 @@ static void print_link_stats64(FILE *fp,
112 open_json_object("rx");
113 - print_uint(PRINT_JSON, "bytes", NULL, s->rx_bytes);
114 - print_uint(PRINT_JSON, "packets", NULL, s->rx_packets);
115 - print_uint(PRINT_JSON, "errors", NULL, s->rx_errors);
116 - print_uint(PRINT_JSON, "dropped", NULL, s->rx_dropped);
117 - print_uint(PRINT_JSON, "over_errors", NULL, s->rx_over_errors);
118 - print_uint(PRINT_JSON, "multicast", NULL, s->multicast);
119 + print_u64(PRINT_JSON, "bytes", NULL, s->rx_bytes);
120 + print_u64(PRINT_JSON, "packets", NULL, s->rx_packets);
121 + print_u64(PRINT_JSON, "errors", NULL, s->rx_errors);
122 + print_u64(PRINT_JSON, "dropped", NULL, s->rx_dropped);
123 + print_u64(PRINT_JSON, "over_errors", NULL, s->rx_over_errors);
124 + print_u64(PRINT_JSON, "multicast", NULL, s->multicast);
125 if (s->rx_compressed)
126 - print_uint(PRINT_JSON,
127 + print_u64(PRINT_JSON,
129 NULL, s->rx_compressed);
132 if (show_stats > 1) {
133 - print_uint(PRINT_JSON,
134 + print_u64(PRINT_JSON,
136 NULL, s->rx_length_errors);
137 - print_uint(PRINT_JSON,
138 + print_u64(PRINT_JSON,
140 NULL, s->rx_crc_errors);
141 - print_uint(PRINT_JSON,
142 + print_u64(PRINT_JSON,
144 NULL, s->rx_frame_errors);
145 - print_uint(PRINT_JSON,
146 + print_u64(PRINT_JSON,
148 NULL, s->rx_fifo_errors);
149 - print_uint(PRINT_JSON,
150 + print_u64(PRINT_JSON,
152 NULL, s->rx_missed_errors);
154 - print_uint(PRINT_JSON,
155 + print_u64(PRINT_JSON,
156 "nohandler", NULL, s->rx_nohandler);
161 open_json_object("tx");
162 - print_uint(PRINT_JSON, "bytes", NULL, s->tx_bytes);
163 - print_uint(PRINT_JSON, "packets", NULL, s->tx_packets);
164 - print_uint(PRINT_JSON, "errors", NULL, s->tx_errors);
165 - print_uint(PRINT_JSON, "dropped", NULL, s->tx_dropped);
166 - print_uint(PRINT_JSON,
167 + print_u64(PRINT_JSON, "bytes", NULL, s->tx_bytes);
168 + print_u64(PRINT_JSON, "packets", NULL, s->tx_packets);
169 + print_u64(PRINT_JSON, "errors", NULL, s->tx_errors);
170 + print_u64(PRINT_JSON, "dropped", NULL, s->tx_dropped);
171 + print_u64(PRINT_JSON,
173 NULL, s->tx_carrier_errors);
174 - print_uint(PRINT_JSON, "collisions", NULL, s->collisions);
175 + print_u64(PRINT_JSON, "collisions", NULL, s->collisions);
176 if (s->tx_compressed)
177 print_uint(PRINT_JSON,
179 @@ -653,20 +653,20 @@ static void print_link_stats64(FILE *fp,
182 if (show_stats > 1) {
183 - print_uint(PRINT_JSON,
184 + print_u64(PRINT_JSON,
186 NULL, s->tx_aborted_errors);
187 - print_uint(PRINT_JSON,
188 + print_u64(PRINT_JSON,
190 NULL, s->tx_fifo_errors);
191 - print_uint(PRINT_JSON,
192 + print_u64(PRINT_JSON,
194 NULL, s->tx_window_errors);
195 - print_uint(PRINT_JSON,
196 + print_u64(PRINT_JSON,
198 NULL, s->tx_heartbeat_errors);
200 - print_uint(PRINT_JSON, "carrier_changes", NULL,
201 + print_u64(PRINT_JSON, "carrier_changes", NULL,
202 rta_getattr_u32(carrier_changes));
205 --- a/lib/json_print.c
206 +++ b/lib/json_print.c
207 @@ -117,8 +117,10 @@ void close_json_array(enum output_type t
210 _PRINT_FUNC(int, int);
211 +_PRINT_FUNC(s64, int64_t);
212 _PRINT_FUNC(hu, unsigned short);
213 -_PRINT_FUNC(uint, uint64_t);
214 +_PRINT_FUNC(uint, unsigned int);
215 +_PRINT_FUNC(u64, uint64_t);
216 _PRINT_FUNC(lluint, unsigned long long int);
217 _PRINT_FUNC(float, double);
219 --- a/lib/json_writer.c
220 +++ b/lib/json_writer.c
221 @@ -215,7 +215,12 @@ void jsonw_hu(json_writer_t *self, unsig
222 jsonw_printf(self, "%hu", num);
225 -void jsonw_uint(json_writer_t *self, uint64_t num)
226 +void jsonw_uint(json_writer_t *self, unsigned int num)
228 + jsonw_printf(self, "%u", num);
231 +void jsonw_u64(json_writer_t *self, uint64_t num)
233 jsonw_printf(self, "%"PRIu64, num);
235 @@ -225,7 +230,12 @@ void jsonw_lluint(json_writer_t *self, u
236 jsonw_printf(self, "%llu", num);
239 -void jsonw_int(json_writer_t *self, int64_t num)
240 +void jsonw_int(json_writer_t *self, int num)
242 + jsonw_printf(self, "%d", num);
245 +void jsonw_s64(json_writer_t *self, int64_t num)
247 jsonw_printf(self, "%"PRId64, num);
249 @@ -258,12 +268,18 @@ void jsonw_float_field_fmt(json_writer_t
250 jsonw_float_fmt(self, fmt, val);
253 -void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num)
254 +void jsonw_uint_field(json_writer_t *self, const char *prop, unsigned int num)
256 jsonw_name(self, prop);
257 jsonw_uint(self, num);
260 +void jsonw_u64_field(json_writer_t *self, const char *prop, uint64_t num)
262 + jsonw_name(self, prop);
263 + jsonw_u64(self, num);
266 void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num)
268 jsonw_name(self, prop);
269 @@ -278,12 +294,18 @@ void jsonw_lluint_field(json_writer_t *s
270 jsonw_lluint(self, num);
273 -void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num)
274 +void jsonw_int_field(json_writer_t *self, const char *prop, int num)
276 jsonw_name(self, prop);
277 jsonw_int(self, num);
280 +void jsonw_s64_field(json_writer_t *self, const char *prop, int64_t num)
282 + jsonw_name(self, prop);
283 + jsonw_s64(self, num);
286 void jsonw_null_field(json_writer_t *self, const char *prop)
288 jsonw_name(self, prop);