ckctype.c revision 1.2
1/* $NetBSD: ckctype.c,v 1.2 2021/04/05 02:17:52 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@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.2 2021/04/05 02:17:52 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 55static bool 56is_ctype_function(const char *name) 57{ 58 59 if (name[0] != 'i' || name[1] != 's') 60 if (name[0] != 't' || name[1] != 'o') 61 return false; 62 63 return strcmp(name, "isalnum") == 0 || 64 strcmp(name, "isalpha") == 0 || 65 strcmp(name, "isblank") == 0 || 66 strcmp(name, "iscntrl") == 0 || 67 strcmp(name, "isdigit") == 0 || 68 strcmp(name, "isgraph") == 0 || 69 strcmp(name, "islower") == 0 || 70 strcmp(name, "isprint") == 0 || 71 strcmp(name, "ispunct") == 0 || 72 strcmp(name, "isspace") == 0 || 73 strcmp(name, "isupper") == 0 || 74 strcmp(name, "isxdigit") == 0 || 75 strcmp(name, "tolower") == 0 || 76 strcmp(name, "toupper") == 0; 77} 78 79static bool 80is_ctype_table(const char *name) 81{ 82 83 /* NetBSD sys/ctype_bits.h 1.6 from 2016-01-22 */ 84 if (strcmp(name, "_ctype_tab_") == 0 || 85 strcmp(name, "_tolower_tab_") == 0 || 86 strcmp(name, "_toupper_tab_") == 0) 87 return true; 88 89 /* NetBSD sys/ctype_bits.h 1.1 from 2010-06-01 */ 90 return strcmp(name, "_ctype_") == 0; 91} 92 93static void 94check_ctype_arg(const char *func, const tnode_t *arg) 95{ 96 const tnode_t *on, *cn; 97 98 for (on = arg; on->tn_op == CVT; on = on->tn_left) 99 if (on->tn_type->t_tspec == UCHAR) 100 return; 101 if (on->tn_type->t_tspec == UCHAR) 102 return; 103 104 if (arg->tn_op == CVT && arg->tn_cast) { 105 /* argument to '%s' must be cast to 'unsigned char', not to '%s' */ 106 warning(342, func, type_name(arg->tn_type)); 107 return; 108 } 109 110 cn = before_conversion(arg); 111 if (cn->tn_type->t_tspec == CHAR) { 112 /* argument to '%s' must be 'unsigned char' or EOF, not '%s' */ 113 warning(341, func, type_name(cn->tn_type)); 114 return; 115 } 116} 117 118void 119check_ctype_function_call(const tnode_t *func, const tnode_t *args) 120{ 121 122 if (func->tn_op == NAME && 123 is_ctype_function(func->tn_sym->s_name) && 124 args != NULL && 125 args->tn_left != NULL && 126 args->tn_right == NULL) 127 check_ctype_arg(func->tn_sym->s_name, args->tn_left); 128} 129 130void 131check_ctype_macro_invocation(const tnode_t *ln, const tnode_t *rn) 132{ 133 134 if (ln->tn_op == PLUS && 135 ln->tn_left != NULL && 136 ln->tn_left->tn_op == LOAD && 137 ln->tn_left->tn_left != NULL && 138 ln->tn_left->tn_left->tn_op == NAME && 139 is_ctype_table(ln->tn_left->tn_left->tn_sym->s_name)) 140 check_ctype_arg("function from <ctype.h>", rn); 141} 142