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