cksnprintb.c revision 1.4 1 /* $NetBSD: cksnprintb.c,v 1.4 2024/03/03 00:50:41 rillig Exp $ */
2
3 /*-
4 * Copyright (c) 2024 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Roland Illig <rillig (at) NetBSD.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35
36 #include <sys/cdefs.h>
37 #if defined(__RCSID)
38 __RCSID("$NetBSD: cksnprintb.c,v 1.4 2024/03/03 00:50:41 rillig Exp $");
39 #endif
40
41 #include <stdbool.h>
42 #include <string.h>
43
44 #include "lint1.h"
45
46 typedef struct {
47 bool new_style;
48 const buffer *fmt;
49 const tnode_t *value;
50 uint64_t field_width;
51 uint64_t covered;
52 unsigned covered_start[64];
53 unsigned covered_end[64];
54 } checker;
55
56 static bool
57 match_string_literal(const tnode_t *tn, const buffer **str)
58 {
59 while (tn->tn_op == CVT)
60 tn = tn_ck_left(tn);
61 return tn->tn_op == ADDR
62 && tn->tn_left->tn_op == STRING
63 && (*str = tn->tn_left->tn_string, (*str)->data != NULL);
64 }
65
66 static bool
67 match_snprintb_call(const function_call *call,
68 const buffer **out_fmt, const tnode_t **out_val)
69 {
70 const char *func;
71 const tnode_t *val;
72 const buffer *str;
73
74 if (call->func->tn_op == ADDR
75 && call->func->tn_left->tn_op == NAME
76 && (func = call->func->tn_left->tn_sym->s_name, true)
77 && ((strcmp(func, "snprintb") == 0 && call->args_len == 4)
78 || (strcmp(func, "snprintb_m") == 0 && call->args_len == 5))
79 && match_string_literal(call->args[2], &str)
80 && (val = call->args[3], true)) {
81 *out_fmt = str;
82 *out_val = val;
83 return true;
84 }
85 return false;
86 }
87
88 static int
89 len(quoted_iterator it)
90 {
91 return (int)(it.i - it.start);
92 }
93
94 static int
95 range(quoted_iterator start, quoted_iterator end)
96 {
97 return (int)(end.i - start.start);
98 }
99
100 static const char *
101 start(quoted_iterator it, const buffer *buf)
102 {
103 return buf->data + it.start;
104 }
105
106 static uintmax_t
107 val(quoted_iterator it)
108 {
109 return it.value;
110 }
111
112 static void
113 check_hex_escape(const buffer *buf, quoted_iterator it)
114 {
115 if (it.hex_digits > 1) {
116 bool upper = false;
117 bool lower = false;
118 for (size_t i = it.start + 2; i < it.i; i++) {
119 if (isupper((unsigned char)buf->data[i]))
120 upper = true;
121 if (islower((unsigned char)buf->data[i]))
122 lower = true;
123 }
124 if (upper && lower)
125 /* hex escape '%.*s' mixes uppercase and lower... */
126 warning(357, len(it), start(it, buf));
127 }
128 if (it.hex_digits > 2)
129 /* hex escape '%.*s' has more than 2 digits */
130 warning(358, len(it), start(it, buf));
131 }
132
133 static void
134 check_overlap(checker *ck, uint64_t dir_lsb, uint64_t width,
135 size_t start, size_t end)
136 {
137 unsigned lsb = (unsigned)(ck->new_style ? dir_lsb : dir_lsb - 1);
138 if (lsb >= 64 || width == 0 || width > 64)
139 return;
140
141 uint64_t field_mask = value_bits((unsigned)width) << lsb;
142 uint64_t overlap = ck->covered & field_mask;
143 if (overlap == 0)
144 goto done;
145
146 for (unsigned i = lsb; i < 64; i++) {
147 if (!(overlap & bit(i)))
148 continue;
149 /* '%.*s' overlaps earlier '%.*s' on bit %u */
150 warning(376,
151 (int)(end - start), ck->fmt->data + start,
152 (int)(ck->covered_end[i] - ck->covered_start[i]),
153 ck->fmt->data + ck->covered_start[i],
154 ck->new_style ? i : i + 1);
155 break;
156 }
157
158 done:
159 ck->covered |= field_mask;
160 for (unsigned i = lsb; i < 64; i++) {
161 if (field_mask & bit(i)) {
162 ck->covered_start[i] = (unsigned)start;
163 ck->covered_end[i] = (unsigned)end;
164 }
165 }
166 }
167
168 static void
169 check_reachable(checker *ck, uint64_t dir_lsb, uint64_t width,
170 size_t start, size_t end)
171 {
172 unsigned lsb = (unsigned)(ck->new_style ? dir_lsb : dir_lsb - 1);
173 if (lsb >= 64 || width == 0 || width > 64)
174 return;
175
176 uint64_t field_mask = value_bits((unsigned)width) << lsb;
177 if (!(possible_bits(ck->value) & field_mask)) {
178 /* directive '%.*s' is unreachable by input value */
179 warning(378, (int)(end - start), ck->fmt->data + start);
180 }
181 }
182
183 static bool
184 check_directive(const buffer *fmt, quoted_iterator *it, bool new_style,
185 checker *ck)
186 {
187
188 if (!quoted_next(fmt, it))
189 return false;
190 quoted_iterator dir = *it;
191
192 bool has_bit = !new_style
193 || dir.value == 'b' || dir.value == 'f' || dir.value == 'F';
194 if (has_bit && new_style && !quoted_next(fmt, it)) {
195 /* missing bit position after '%.*s' */
196 warning(364, range(dir, *it), start(dir, fmt));
197 return false;
198 }
199 /* LINTED 86 "automatic 'bit' hides external declaration" */
200 quoted_iterator bit = *it;
201
202 bool has_width = new_style
203 && (dir.value == 'f' || dir.value == 'F');
204 if (has_width && !quoted_next(fmt, it)) {
205 /* missing field width after '%.*s' */
206 warning(365, range(dir, *it), start(dir, fmt));
207 return false;
208 }
209 quoted_iterator width = *it;
210
211 bool has_cmp = new_style
212 && (dir.value == '=' || dir.value == ':');
213 if (has_cmp && !quoted_next(fmt, it)) {
214 /* missing comparison value after directive '%.*s' */
215 warning(368, range(dir, *it), start(dir, fmt));
216 return false;
217 }
218 quoted_iterator cmp = *it;
219
220 bool has_default = new_style && dir.value == '*';
221 if (has_default && !quoted_next(fmt, it)) {
222 /* missing '\0' at the end of '%.*s' */
223 warning(366, range(dir, *it), start(dir, fmt));
224 return false;
225 }
226
227 if (new_style && dir.value == '\0') {
228 quoted_iterator end = *it;
229 if (!quoted_next(fmt, &end)) {
230 /* redundant '\0' at the end of new-style format */
231 warning(377);
232 return false;
233 }
234 }
235
236 if (!has_bit && !has_cmp && !has_default) {
237 /* unknown directive '%.*s' */
238 warning(374, len(dir), start(dir, fmt));
239 return false;
240 }
241
242 if (!quoted_next(fmt, it)) {
243 if (new_style && dir.value != '*')
244 /* missing '\0' at the end of '%.*s' */
245 warning(366, range(dir, *it), start(dir, fmt));
246 else
247 /* empty description in '%.*s' */
248 warning(367, range(dir, *it), start(dir, fmt));
249 return false;
250 }
251 quoted_iterator descr = *it;
252
253 quoted_iterator prev = *it;
254 for (;;) {
255 if (new_style && it->value == 0)
256 break;
257 if (!new_style && it->value == 0)
258 /* old-style format contains '\0' */
259 warning(362);
260 if (!new_style && it->value <= 32) {
261 *it = prev;
262 break;
263 }
264 if (it->escaped && !isprint((unsigned char)it->value)) {
265 /* non-printing character '%.*s' in description ... */
266 warning(363,
267 len(*it), start(*it, fmt),
268 range(descr, *it), start(descr, fmt));
269 }
270 prev = *it;
271 if (!quoted_next(fmt, it)) {
272 if (new_style) {
273 /* missing '\0' at the end of '%.*s' */
274 warning(366, range(dir, prev),
275 start(dir, fmt));
276 }
277 break;
278 }
279 }
280
281 if (has_bit)
282 check_hex_escape(fmt, bit);
283 if (has_width)
284 check_hex_escape(fmt, width);
285 if (has_bit && bit.octal_digits == 0 && bit.hex_digits == 0) {
286 /* bit position '%.*s' in '%.*s' should be escaped as ... */
287 warning(369, len(bit), start(bit, fmt),
288 range(dir, *it), start(dir, fmt));
289 }
290 if (has_width && width.octal_digits == 0 && width.hex_digits == 0) {
291 /* field width '%.*s' in '%.*s' should be escaped as ... */
292 warning(370, len(width), start(width, fmt),
293 range(dir, *it), start(dir, fmt));
294 }
295 if (has_bit && (new_style ? bit.value > 63 : bit.value - 1 > 31)) {
296 /* bit position '%.*s' (%ju) in '%.*s' out of range %u..%u */
297 warning(371,
298 len(bit), start(bit, fmt), val(bit),
299 range(dir, *it), start(dir, fmt),
300 new_style ? 0 : 1, new_style ? 63 : 32);
301 }
302 if (has_width && width.value > (new_style ? 64 : 32)) {
303 /* field width '%.*s' (%ju) in '%.*s' out of range 0..%u */
304 warning(372,
305 len(width), start(width, fmt), val(width),
306 range(dir, *it), start(dir, fmt),
307 new_style ? 64 : 32);
308 }
309 if (has_width && bit.value + width.value > 64) {
310 /* bit field end %ju in '%.*s' out of range 0..64 */
311 warning(373, val(bit) + val(width),
312 range(dir, *it), start(dir, fmt));
313 }
314 if (has_cmp && ck->field_width < 64
315 && cmp.value & ~(uint64_t)0 << ck->field_width) {
316 /* comparison value '%.*s' (%ju) exceeds maximum field ... */
317 warning(375, len(cmp), start(cmp, fmt), val(cmp),
318 (uintmax_t)value_bits((unsigned)ck->field_width));
319 }
320 if (has_bit) {
321 uint64_t w = has_width ? width.value : 1;
322 check_overlap(ck, bit.value, w, dir.start, it->i);
323 }
324 if (has_bit) {
325 uint64_t w = has_width ? width.value : 1;
326 check_reachable(ck, bit.value, w, dir.start, it->i);
327 }
328 if (descr.i == prev.i && dir.value != 'F') {
329 /* empty description in '%.*s' */
330 warning(367, range(dir, *it), start(dir, fmt));
331 }
332
333 if (has_width)
334 ck->field_width = width.value;
335 return true;
336 }
337
338 void
339 check_snprintb(const tnode_t *expr)
340 {
341 const buffer *fmt;
342 const tnode_t *value;
343 if (!match_snprintb_call(expr->tn_call, &fmt, &value))
344 return;
345
346 quoted_iterator it = { .i = 0 };
347 if (!quoted_next(fmt, &it)) {
348 /* missing new-style '\177' or old-style number base */
349 warning(359);
350 return;
351 }
352 bool new_style = it.value == '\177';
353 if (new_style && !quoted_next(fmt, &it)) {
354 /* missing new-style number base after '\177' */
355 warning(360);
356 return;
357 }
358 if (it.value != 8 && it.value != 10 && it.value != 16) {
359 /* number base '%.*s' is %ju, must be 8, 10 or 16 */
360 warning(361, len(it), start(it, fmt), val(it));
361 return;
362 }
363
364 checker ck = {
365 .new_style = new_style,
366 .fmt = fmt,
367 .value = value,
368 .field_width = 64,
369 };
370 while (check_directive(fmt, &it, new_style, &ck))
371 continue;
372 }
373