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