Home | History | Annotate | Line # | Download | only in lint1
ckctype.c revision 1.1
      1 /* $NetBSD: ckctype.c,v 1.1 2021/04/05 02:07:14 rillig Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2021 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 
     34 #include "nbtool_config.h"
     35 
     36 #endif
     37 
     38 #include <sys/cdefs.h>
     39 
     40 #if defined(__RCSID) && !defined(lint)
     41 __RCSID("$NetBSD: ckctype.c,v 1.1 2021/04/05 02:07:14 rillig Exp $");
     42 #endif
     43 
     44 #include <string.h>
     45 
     46 #include "lint1.h"
     47 
     48 /*
     49  * Check that the functions from <ctype.h> are used properly.  They are
     50  * difficult to use when their argument comes from an expression of type
     51  * 'char'.  In such a case, the argument must be converted to 'unsigned char',
     52  * not directly to 'int'.
     53  *
     54  * https://stackoverflow.com/a/60696378
     55  */
     56 
     57 #define NEED(cond) \
     58 	do {			\
     59 		if (!(cond))	\
     60 			return;	\
     61 	} while (false)
     62 
     63 static bool
     64 is_ctype_function(const char *name)
     65 {
     66 
     67 	if (name[0] != 'i' || name[1] != 's')
     68 		if (name[0] != 't' || name[1] != 'o')
     69 			return false;
     70 
     71 	return strcmp(name, "isalnum") == 0 ||
     72 	       strcmp(name, "isalpha") == 0 ||
     73 	       strcmp(name, "isblank") == 0 ||
     74 	       strcmp(name, "iscntrl") == 0 ||
     75 	       strcmp(name, "isdigit") == 0 ||
     76 	       strcmp(name, "isgraph") == 0 ||
     77 	       strcmp(name, "islower") == 0 ||
     78 	       strcmp(name, "isprint") == 0 ||
     79 	       strcmp(name, "ispunct") == 0 ||
     80 	       strcmp(name, "isspace") == 0 ||
     81 	       strcmp(name, "isupper") == 0 ||
     82 	       strcmp(name, "isxdigit") == 0 ||
     83 	       strcmp(name, "tolower") == 0 ||
     84 	       strcmp(name, "toupper") == 0;
     85 }
     86 
     87 static bool
     88 is_ctype_table(const char *name)
     89 {
     90 
     91 	/* NetBSD sys/ctype_bits.h 1.6 from 2016-01-22 */
     92 	if (strcmp(name, "_ctype_tab_") == 0 ||
     93 	    strcmp(name, "_tolower_tab_") == 0 ||
     94 	    strcmp(name, "_toupper_tab_") == 0)
     95 		return true;
     96 
     97 	/* NetBSD sys/ctype_bits.h 1.1 from 2010-06-01 */
     98 	return strcmp(name, "_ctype_") == 0;
     99 }
    100 
    101 static void
    102 check_ctype_arg(const char *func, const tnode_t *arg)
    103 {
    104 	const tnode_t *on, *cn;
    105 
    106 	for (on = arg; on->tn_op == CVT; on = on->tn_left)
    107 		if (on->tn_type->t_tspec == UCHAR)
    108 			return;
    109 	if (on->tn_type->t_tspec == UCHAR)
    110 		return;
    111 
    112 	if (arg->tn_op == CVT && arg->tn_cast) {
    113 		/* argument to '%s' must be cast to 'unsigned char', not to '%s' */
    114 		warning(342, func, type_name(arg->tn_type));
    115 		return;
    116 	}
    117 
    118 	cn = before_conversion(arg);
    119 	if (cn->tn_type->t_tspec == CHAR) {
    120 		/* argument to '%s' must be 'unsigned char' or EOF, not '%s' */
    121 		warning(341, func, type_name(cn->tn_type));
    122 		return;
    123 	}
    124 }
    125 
    126 void
    127 check_ctype_function_call(const tnode_t *func, const tnode_t *args)
    128 {
    129 
    130 	NEED(func->tn_op == NAME);
    131 	NEED(is_ctype_function(func->tn_sym->s_name));
    132 	NEED(args != NULL);
    133 	NEED(args->tn_left != NULL);
    134 	NEED(args->tn_right == NULL);
    135 
    136 	check_ctype_arg(func->tn_sym->s_name, args->tn_left);
    137 }
    138 
    139 void
    140 check_ctype_macro_invocation(const tnode_t *ln, const tnode_t *rn)
    141 {
    142 
    143 	NEED(ln->tn_op == PLUS);
    144 	NEED(ln->tn_left != NULL);
    145 	NEED(ln->tn_left->tn_op == LOAD);
    146 	NEED(ln->tn_left->tn_left != NULL);
    147 	NEED(ln->tn_left->tn_left->tn_op == NAME);
    148 	NEED(is_ctype_table(ln->tn_left->tn_left->tn_sym->s_name));
    149 
    150 	check_ctype_arg("function from <ctype.h>", rn);
    151 }
    152