iproute2: backport json_print-fix-hidden-64-bit-type-promotion
[openwrt/staging/chunkeey.git] / package / network / utils / iproute2 / patches / 002-json_print-fix-hidden-64-bit-type-promotion.patch
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
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
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").
12
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.
17
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.
20
21 Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk>
22 Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
23 ---
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(-)
32
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); \
37 }
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);
64
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);
79 --- a/ip/ipaddress.c
80 +++ b/ip/ipaddress.c
81 @@ -555,21 +555,21 @@ static void print_vf_stats64(FILE *fp, s
82
83 /* RX stats */
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]));
97 close_json_object();
98
99 /* TX stats */
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]));
107 close_json_object();
108 close_json_object();
109 @@ -602,50 +602,50 @@ static void print_link_stats64(FILE *fp,
110
111 /* RX stats */
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,
128 "compressed",
129 NULL, s->rx_compressed);
130
131 /* RX error stats */
132 if (show_stats > 1) {
133 - print_uint(PRINT_JSON,
134 + print_u64(PRINT_JSON,
135 "length_errors",
136 NULL, s->rx_length_errors);
137 - print_uint(PRINT_JSON,
138 + print_u64(PRINT_JSON,
139 "crc_errors",
140 NULL, s->rx_crc_errors);
141 - print_uint(PRINT_JSON,
142 + print_u64(PRINT_JSON,
143 "frame_errors",
144 NULL, s->rx_frame_errors);
145 - print_uint(PRINT_JSON,
146 + print_u64(PRINT_JSON,
147 "fifo_errors",
148 NULL, s->rx_fifo_errors);
149 - print_uint(PRINT_JSON,
150 + print_u64(PRINT_JSON,
151 "missed_errors",
152 NULL, s->rx_missed_errors);
153 if (s->rx_nohandler)
154 - print_uint(PRINT_JSON,
155 + print_u64(PRINT_JSON,
156 "nohandler", NULL, s->rx_nohandler);
157 }
158 close_json_object();
159
160 /* TX stats */
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,
172 "carrier_errors",
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,
178 "compressed",
179 @@ -653,20 +653,20 @@ static void print_link_stats64(FILE *fp,
180
181 /* TX error stats */
182 if (show_stats > 1) {
183 - print_uint(PRINT_JSON,
184 + print_u64(PRINT_JSON,
185 "aborted_errors",
186 NULL, s->tx_aborted_errors);
187 - print_uint(PRINT_JSON,
188 + print_u64(PRINT_JSON,
189 "fifo_errors",
190 NULL, s->tx_fifo_errors);
191 - print_uint(PRINT_JSON,
192 + print_u64(PRINT_JSON,
193 "window_errors",
194 NULL, s->tx_window_errors);
195 - print_uint(PRINT_JSON,
196 + print_u64(PRINT_JSON,
197 "heartbeat_errors",
198 NULL, s->tx_heartbeat_errors);
199 if (carrier_changes)
200 - print_uint(PRINT_JSON, "carrier_changes", NULL,
201 + print_u64(PRINT_JSON, "carrier_changes", NULL,
202 rta_getattr_u32(carrier_changes));
203 }
204 close_json_object();
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
208 } \
209 }
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);
218 #undef _PRINT_FUNC
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);
223 }
224
225 -void jsonw_uint(json_writer_t *self, uint64_t num)
226 +void jsonw_uint(json_writer_t *self, unsigned int num)
227 +{
228 + jsonw_printf(self, "%u", num);
229 +}
230 +
231 +void jsonw_u64(json_writer_t *self, uint64_t num)
232 {
233 jsonw_printf(self, "%"PRIu64, num);
234 }
235 @@ -225,7 +230,12 @@ void jsonw_lluint(json_writer_t *self, u
236 jsonw_printf(self, "%llu", num);
237 }
238
239 -void jsonw_int(json_writer_t *self, int64_t num)
240 +void jsonw_int(json_writer_t *self, int num)
241 +{
242 + jsonw_printf(self, "%d", num);
243 +}
244 +
245 +void jsonw_s64(json_writer_t *self, int64_t num)
246 {
247 jsonw_printf(self, "%"PRId64, num);
248 }
249 @@ -258,12 +268,18 @@ void jsonw_float_field_fmt(json_writer_t
250 jsonw_float_fmt(self, fmt, val);
251 }
252
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)
255 {
256 jsonw_name(self, prop);
257 jsonw_uint(self, num);
258 }
259
260 +void jsonw_u64_field(json_writer_t *self, const char *prop, uint64_t num)
261 +{
262 + jsonw_name(self, prop);
263 + jsonw_u64(self, num);
264 +}
265 +
266 void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num)
267 {
268 jsonw_name(self, prop);
269 @@ -278,12 +294,18 @@ void jsonw_lluint_field(json_writer_t *s
270 jsonw_lluint(self, num);
271 }
272
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)
275 {
276 jsonw_name(self, prop);
277 jsonw_int(self, num);
278 }
279
280 +void jsonw_s64_field(json_writer_t *self, const char *prop, int64_t num)
281 +{
282 + jsonw_name(self, prop);
283 + jsonw_s64(self, num);
284 +}
285 +
286 void jsonw_null_field(json_writer_t *self, const char *prop)
287 {
288 jsonw_name(self, prop);