Home | History | Annotate | Line # | Download | only in lint1
cksnprintb.c revision 1.7
      1 /*	$NetBSD: cksnprintb.c,v 1.7 2024/03/03 16:09:01 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.7 2024/03/03 16:09:01 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.end - it.start);
     94 }
     95 
     96 static int
     97 range(quoted_iterator start, quoted_iterator end)
     98 {
     99 	return (int)(end.end - 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.end; 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 bool
    185 parse_description(checker *ck)
    186 {
    187 	size_t descr_start = 0 /* dummy */;
    188 	bool seen_descr = false;
    189 	quoted_iterator it = ck->it;
    190 	uint64_t end_marker = ck->new_style ? 0 : 32;
    191 
    192 	while (quoted_next(ck->fmt, &it) && it.value > end_marker) {
    193 		ck->it = it;
    194 		if (!seen_descr)
    195 			descr_start = it.start;
    196 		seen_descr = true;
    197 		if (it.escaped && !isprint((unsigned char)it.value)) {
    198 			/* non-printing character '%.*s' in description ... */
    199 			warning(363,
    200 			    len(it), start(it, ck->fmt),
    201 			    (int)(it.end - descr_start),
    202 			    ck->fmt->data + descr_start);
    203 		}
    204 	}
    205 	return seen_descr;
    206 }
    207 
    208 static bool
    209 check_directive(checker *ck)
    210 {
    211 	bool new_style = ck->new_style;
    212 	const buffer *fmt = ck->fmt;
    213 	quoted_iterator *it = &ck->it;
    214 
    215 	if (!quoted_next(fmt, it))
    216 		return false;
    217 	quoted_iterator dir = *it;
    218 
    219 	bool has_bit = !new_style
    220 	    || dir.value == 'b' || dir.value == 'f' || dir.value == 'F';
    221 	if (has_bit && new_style && !quoted_next(fmt, it)) {
    222 		/* missing bit position after '%.*s' */
    223 		warning(364, range(dir, *it), start(dir, fmt));
    224 		return false;
    225 	}
    226 	/* LINTED 86 "automatic 'bit' hides external declaration" */
    227 	quoted_iterator bit = *it;
    228 
    229 	bool has_width = new_style
    230 	    && (dir.value == 'f' || dir.value == 'F');
    231 	if (has_width && !quoted_next(fmt, it)) {
    232 		/* missing field width after '%.*s' */
    233 		warning(365, range(dir, *it), start(dir, fmt));
    234 		return false;
    235 	}
    236 	quoted_iterator width = *it;
    237 
    238 	bool has_cmp = new_style
    239 	    && (dir.value == '=' || dir.value == ':');
    240 	if (has_cmp && !quoted_next(fmt, it)) {
    241 		/* missing comparison value after directive '%.*s' */
    242 		warning(368, range(dir, *it), start(dir, fmt));
    243 		return false;
    244 	}
    245 	quoted_iterator cmp = *it;
    246 
    247 	bool has_default = new_style && dir.value == '*';
    248 
    249 	if (dir.value == '\0') {
    250 		quoted_iterator end = *it;
    251 		if (!quoted_next(fmt, &end)) {
    252 			/* redundant '\0' at the end of the format */
    253 			warning(377);
    254 			return false;
    255 		}
    256 	}
    257 
    258 	if (!has_bit && !has_cmp && !has_default) {
    259 		/* unknown directive '%.*s', must be one of 'bfF=:*' */
    260 		warning(374, len(dir), start(dir, fmt));
    261 		return false;
    262 	}
    263 	if (new_style && dir.escaped)
    264 		/* directive '%.*s' should not be escaped */
    265 		warning(362, len(dir), start(dir, fmt));
    266 
    267 	bool needs_descr = !(new_style && dir.value == 'F');
    268 	bool seen_descr = parse_description(ck);
    269 	bool seen_null = new_style
    270 	    && quoted_next(ck->fmt, &ck->it) && ck->it.value == 0;
    271 
    272 	if (has_bit)
    273 		check_hex_escape(fmt, bit);
    274 	if (has_width)
    275 		check_hex_escape(fmt, width);
    276 	if (has_bit && bit.octal_digits == 0 && bit.hex_digits == 0) {
    277 		/* bit position '%.*s' in '%.*s' should be escaped as ... */
    278 		warning(369, len(bit), start(bit, fmt),
    279 		    range(dir, *it), start(dir, fmt));
    280 	}
    281 	if (has_width && width.octal_digits == 0 && width.hex_digits == 0) {
    282 		/* field width '%.*s' in '%.*s' should be escaped as ... */
    283 		warning(370, len(width), start(width, fmt),
    284 		    range(dir, *it), start(dir, fmt));
    285 	}
    286 	if (has_bit && (new_style ? bit.value > 63 : bit.value - 1 > 31)) {
    287 		/* bit position '%.*s' (%ju) in '%.*s' out of range %u..%u */
    288 		warning(371,
    289 		    len(bit), start(bit, fmt), val(bit),
    290 		    range(dir, *it), start(dir, fmt),
    291 		    new_style ? 0 : 1, new_style ? 63 : 32);
    292 	}
    293 	if (has_width && width.value > 64) {
    294 		/* field width '%.*s' (%ju) in '%.*s' out of range 0..64 */
    295 		warning(372,
    296 		    len(width), start(width, fmt), val(width),
    297 		    range(dir, *it), start(dir, fmt));
    298 	}
    299 	if (has_width && bit.value + width.value > 64) {
    300 		/* bit field end %ju in '%.*s' out of range 0..64 */
    301 		warning(373, val(bit) + val(width),
    302 		    range(dir, *it), start(dir, fmt));
    303 	}
    304 	if (has_cmp && ck->field_width > 0 && ck->field_width < 64
    305 	    && cmp.value & ~value_bits((unsigned)ck->field_width)) {
    306 		/* comparison value '%.*s' (%ju) exceeds maximum field ... */
    307 		warning(375, len(cmp), start(cmp, fmt), val(cmp),
    308 		    (uintmax_t)value_bits((unsigned)ck->field_width));
    309 	}
    310 	if (has_bit) {
    311 		uint64_t w = has_width ? width.value : 1;
    312 		check_overlap(ck, bit.value, w, dir.start, it->end);
    313 		check_reachable(ck, bit.value, w, dir.start, it->end);
    314 	}
    315 	if (needs_descr && !seen_descr)
    316 		/* empty description in '%.*s' */
    317 		warning(367, range(dir, *it), start(dir, fmt));
    318 	if (new_style && !seen_null)
    319 		/* missing '\0' at the end of '%.*s' */
    320 		warning(366, range(dir, *it), start(dir, fmt));
    321 
    322 	if (has_width)
    323 		ck->field_width = width.value;
    324 	return true;
    325 }
    326 
    327 void
    328 check_snprintb(const tnode_t *expr)
    329 {
    330 	const buffer *fmt;
    331 	const tnode_t *value;
    332 	if (!match_snprintb_call(expr->tn_call, &fmt, &value))
    333 		return;
    334 
    335 	checker ck = {
    336 		.fmt = fmt,
    337 		.value = value,
    338 		.field_width = 64,
    339 	};
    340 
    341 	if (!quoted_next(fmt, &ck.it)) {
    342 		/* missing new-style '\177' or old-style number base */
    343 		warning(359);
    344 		return;
    345 	}
    346 	ck.new_style = ck.it.value == '\177';
    347 	if (ck.new_style && !quoted_next(fmt, &ck.it)) {
    348 		/* missing new-style number base after '\177' */
    349 		warning(360);
    350 		return;
    351 	}
    352 	if (ck.it.value != 8 && ck.it.value != 10 && ck.it.value != 16) {
    353 		/* number base '%.*s' is %ju, must be 8, 10 or 16 */
    354 		warning(361, len(ck.it), start(ck.it, fmt), val(ck.it));
    355 		return;
    356 	}
    357 
    358 	while (check_directive(&ck))
    359 		continue;
    360 }
    361