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