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