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