Home | History | Annotate | Line # | Download | only in lint1
cksnprintb.c revision 1.10
      1 /*	$NetBSD: cksnprintb.c,v 1.10 2024/03/10 16:27:16 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.10 2024/03/10 16:27:16 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 
     51 	quoted_iterator it;
     52 	uint64_t field_width;
     53 	uint64_t covered;
     54 	unsigned covered_start[64];
     55 	unsigned covered_end[64];
     56 } checker;
     57 
     58 static bool
     59 match_string_literal(const tnode_t *tn, const buffer **str)
     60 {
     61 	while (tn->tn_op == CVT)
     62 		tn = tn->u.ops.left;
     63 	return tn->tn_op == ADDR
     64 	    && tn->u.ops.left->tn_op == STRING
     65 	    && (*str = tn->u.ops.left->u.str_literals, (*str)->data != NULL);
     66 }
     67 
     68 static bool
     69 match_snprintb_call(const function_call *call,
     70     const buffer **fmt, const tnode_t **val)
     71 {
     72 	const char *func;
     73 
     74 	return call->func->tn_op == ADDR
     75 	    && call->func->u.ops.left->tn_op == NAME
     76 	    && (func = call->func->u.ops.left->u.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], fmt)
     80 	    && (*val = call->args[3], true);
     81 }
     82 
     83 static int
     84 len(quoted_iterator it)
     85 {
     86 	return (int)(it.end - it.start);
     87 }
     88 
     89 static int
     90 range(quoted_iterator start, quoted_iterator end)
     91 {
     92 	return (int)(end.end - start.start);
     93 }
     94 
     95 static const char *
     96 start(quoted_iterator it, const buffer *buf)
     97 {
     98 	return buf->data + it.start;
     99 }
    100 
    101 static uintmax_t
    102 val(quoted_iterator it)
    103 {
    104 	return it.value;
    105 }
    106 
    107 static void
    108 check_hex_escape(const buffer *buf, quoted_iterator it)
    109 {
    110 	if (it.hex_digits > 1) {
    111 		bool upper = false;
    112 		bool lower = false;
    113 		for (size_t i = it.start + 2; i < it.end; i++) {
    114 			if (isupper((unsigned char)buf->data[i]))
    115 				upper = true;
    116 			if (islower((unsigned char)buf->data[i]))
    117 				lower = true;
    118 		}
    119 		if (upper && lower)
    120 			/* hex escape '%.*s' mixes uppercase and lower... */
    121 			warning(357, len(it), start(it, buf));
    122 	}
    123 	if (it.hex_digits > 2)
    124 		/* hex escape '%.*s' has more than 2 digits */
    125 		warning(358, len(it), start(it, buf));
    126 }
    127 
    128 static void
    129 check_overlap(checker *ck, uint64_t dir_lsb, uint64_t width,
    130 	      size_t start, size_t end)
    131 {
    132 	unsigned lsb = (unsigned)(ck->new_style ? dir_lsb : dir_lsb - 1);
    133 	if (lsb >= 64 || width == 0 || width > 64)
    134 		return;
    135 
    136 	uint64_t field_mask = value_bits((unsigned)width) << lsb;
    137 	uint64_t overlap = ck->covered & field_mask;
    138 	if (overlap == 0)
    139 		goto update_covered;
    140 
    141 	for (unsigned i = lsb; i < 64; i++) {
    142 		if (!(overlap & bit(i)))
    143 			continue;
    144 		/* '%.*s' overlaps earlier '%.*s' on bit %u */
    145 		warning(376,
    146 		    (int)(end - start), ck->fmt->data + start,
    147 		    (int)(ck->covered_end[i] - ck->covered_start[i]),
    148 		    ck->fmt->data + ck->covered_start[i],
    149 		    ck->new_style ? i : i + 1);
    150 		break;
    151 	}
    152 
    153 update_covered:
    154 	ck->covered |= field_mask;
    155 	for (unsigned i = lsb; i < 64; i++) {
    156 		if (field_mask & bit(i)) {
    157 			ck->covered_start[i] = (unsigned)start;
    158 			ck->covered_end[i] = (unsigned)end;
    159 		}
    160 	}
    161 }
    162 
    163 static void
    164 check_reachable(checker *ck, uint64_t dir_lsb, uint64_t width,
    165 		size_t start, size_t end)
    166 {
    167 	unsigned lsb = (unsigned)(ck->new_style ? dir_lsb : dir_lsb - 1);
    168 	if (lsb >= 64 || width == 0 || width > 64)
    169 		return;
    170 
    171 	uint64_t field_mask = value_bits((unsigned)width) << lsb;
    172 	if (!(possible_bits(ck->value) & field_mask))
    173 		/* directive '%.*s' is unreachable by input value */
    174 		warning(378, (int)(end - start), ck->fmt->data + start);
    175 }
    176 
    177 static bool
    178 parse_description(checker *ck)
    179 {
    180 	size_t descr_start = 0 /* dummy */;
    181 	bool seen_descr = false;
    182 	quoted_iterator it = ck->it;
    183 	uint64_t end_marker = ck->new_style ? 0 : 32;
    184 
    185 	while (quoted_next(ck->fmt, &it) && it.value > end_marker) {
    186 		ck->it = it;
    187 		if (!seen_descr)
    188 			descr_start = it.start;
    189 		seen_descr = true;
    190 		if (it.escaped && !isprint((unsigned char)it.value)) {
    191 			/* non-printing character '%.*s' in description ... */
    192 			warning(363,
    193 			    len(it), start(it, ck->fmt),
    194 			    (int)(it.end - descr_start),
    195 			    ck->fmt->data + descr_start);
    196 		}
    197 	}
    198 	return seen_descr;
    199 }
    200 
    201 static bool
    202 check_directive(checker *ck)
    203 {
    204 	bool new_style = ck->new_style;
    205 	const buffer *fmt = ck->fmt;
    206 	quoted_iterator *it = &ck->it;
    207 
    208 	if (!quoted_next(fmt, it))
    209 		return false;
    210 	quoted_iterator dir = *it;
    211 
    212 	bool has_bit = !new_style
    213 	    || dir.value == 'b' || dir.value == 'f' || dir.value == 'F';
    214 	if (has_bit && new_style && !quoted_next(fmt, it)) {
    215 		/* missing bit position after '%.*s' */
    216 		warning(364, range(dir, *it), start(dir, fmt));
    217 		return false;
    218 	}
    219 	/* LINTED 86 "automatic 'bit' hides external declaration" */
    220 	quoted_iterator bit = *it;
    221 
    222 	bool has_width = new_style
    223 	    && (dir.value == 'f' || dir.value == 'F');
    224 	if (has_width && !quoted_next(fmt, it)) {
    225 		/* missing field width after '%.*s' */
    226 		warning(365, range(dir, *it), start(dir, fmt));
    227 		return false;
    228 	}
    229 	quoted_iterator width = *it;
    230 
    231 	bool has_cmp = new_style
    232 	    && (dir.value == '=' || dir.value == ':');
    233 	if (has_cmp && !quoted_next(fmt, it)) {
    234 		/* missing comparison value after directive '%.*s' */
    235 		warning(368, range(dir, *it), start(dir, fmt));
    236 		return false;
    237 	}
    238 	quoted_iterator cmp = *it;
    239 
    240 	bool has_default = new_style && dir.value == '*';
    241 
    242 	if (dir.value == '\0') {
    243 		quoted_iterator end = *it;
    244 		if (!quoted_next(fmt, &end)) {
    245 			/* redundant '\0' at the end of the format */
    246 			warning(377);
    247 			return false;
    248 		}
    249 	}
    250 
    251 	if (!has_bit && !has_cmp && !has_default) {
    252 		/* unknown directive '%.*s', must be one of 'bfF=:*' */
    253 		warning(374, len(dir), start(dir, fmt));
    254 		return false;
    255 	}
    256 	if (new_style && dir.escaped)
    257 		/* directive '%.*s' should not be escaped */
    258 		warning(362, len(dir), start(dir, fmt));
    259 
    260 	bool needs_descr = !(new_style && dir.value == 'F');
    261 	bool seen_descr = parse_description(ck);
    262 	bool seen_null = new_style
    263 	    && quoted_next(ck->fmt, &ck->it) && ck->it.value == 0;
    264 
    265 	if (has_bit)
    266 		check_hex_escape(fmt, bit);
    267 	if (has_width)
    268 		check_hex_escape(fmt, width);
    269 	if (has_bit && bit.octal_digits == 0 && bit.hex_digits == 0) {
    270 		/* bit position '%.*s' in '%.*s' should be escaped as ... */
    271 		warning(369, len(bit), start(bit, fmt),
    272 		    range(dir, *it), start(dir, fmt));
    273 	}
    274 	if (has_width && width.octal_digits == 0 && width.hex_digits == 0) {
    275 		/* field width '%.*s' in '%.*s' should be escaped as ... */
    276 		warning(370, len(width), start(width, fmt),
    277 		    range(dir, *it), start(dir, fmt));
    278 	}
    279 	if (has_bit && (new_style ? bit.value > 63 : bit.value - 1 > 31)) {
    280 		/* bit position '%.*s' (%ju) in '%.*s' out of range %u..%u */
    281 		warning(371,
    282 		    len(bit), start(bit, fmt), val(bit),
    283 		    range(dir, *it), start(dir, fmt),
    284 		    new_style ? 0 : 1, new_style ? 63 : 32);
    285 	}
    286 	if (has_width && width.value > 64) {
    287 		/* field width '%.*s' (%ju) in '%.*s' out of range 0..64 */
    288 		warning(372,
    289 		    len(width), start(width, fmt), val(width),
    290 		    range(dir, *it), start(dir, fmt));
    291 	}
    292 	if (has_width && bit.value + width.value > 64) {
    293 		/* bit field end %ju in '%.*s' out of range 0..64 */
    294 		warning(373, val(bit) + val(width),
    295 		    range(dir, *it), start(dir, fmt));
    296 	}
    297 	if (has_cmp && ck->field_width > 0 && ck->field_width < 64
    298 	    && cmp.value & ~value_bits((unsigned)ck->field_width)) {
    299 		/* comparison value '%.*s' (%ju) exceeds maximum field ... */
    300 		warning(375, len(cmp), start(cmp, fmt), val(cmp),
    301 		    (uintmax_t)value_bits((unsigned)ck->field_width));
    302 	}
    303 	if (has_bit) {
    304 		uint64_t w = has_width ? width.value : 1;
    305 		check_overlap(ck, bit.value, w, dir.start, it->end);
    306 		check_reachable(ck, bit.value, w, dir.start, it->end);
    307 	}
    308 	if (needs_descr && !seen_descr)
    309 		/* empty description in '%.*s' */
    310 		warning(367, range(dir, *it), start(dir, fmt));
    311 	if (new_style && !seen_null)
    312 		/* missing '\0' at the end of '%.*s' */
    313 		warning(366, range(dir, *it), start(dir, fmt));
    314 
    315 	if (has_width)
    316 		ck->field_width = width.value;
    317 	return true;
    318 }
    319 
    320 void
    321 check_snprintb(const tnode_t *expr)
    322 {
    323 	const buffer *fmt;
    324 	const tnode_t *value;
    325 	if (!match_snprintb_call(expr->u.call, &fmt, &value))
    326 		return;
    327 
    328 	checker ck = {
    329 		.fmt = fmt,
    330 		.value = value,
    331 		.field_width = 64,
    332 	};
    333 
    334 	if (!quoted_next(fmt, &ck.it)) {
    335 		/* missing new-style '\177' or old-style number base */
    336 		warning(359);
    337 		return;
    338 	}
    339 	ck.new_style = ck.it.value == '\177';
    340 	if (ck.new_style && !quoted_next(fmt, &ck.it)) {
    341 		/* missing new-style number base after '\177' */
    342 		warning(360);
    343 		return;
    344 	}
    345 	if (ck.it.value != 8 && ck.it.value != 10 && ck.it.value != 16) {
    346 		/* number base '%.*s' is %ju, must be 8, 10 or 16 */
    347 		warning(361, len(ck.it), start(ck.it, fmt), val(ck.it));
    348 		return;
    349 	}
    350 
    351 	while (check_directive(&ck))
    352 		continue;
    353 }
    354