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