1 # $NetBSD: t_ctype_abuse.sh,v 1.1 2024/12/18 02:47:00 riastradh Exp $ 2 # 3 # Copyright (c) 2024 The NetBSD Foundation, Inc. 4 # All rights reserved. 5 # 6 # Redistribution and use in source and binary forms, with or without 7 # modification, are permitted provided that the following conditions 8 # are met: 9 # 1. Redistributions of source code must retain the above copyright 10 # notice, this list of conditions and the following disclaimer. 11 # 2. Redistributions in binary form must reproduce the above copyright 12 # notice, this list of conditions and the following disclaimer in the 13 # documentation and/or other materials provided with the distribution. 14 # 15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 # POSSIBILITY OF SUCH DAMAGE. 26 # 27 28 ctype_abuse_head() 29 { 30 local ctypefn reftype desc 31 32 ctypefn=$1 33 reftype=$2 34 35 case $reftype in 36 var) desc="variable";; 37 ptr) desc="pointer dereference";; 38 array) desc="array element";; 39 funcall) 40 desc="function call";; 41 esac 42 43 atf_set "descr" "Test that $ctypefn warns on $desc of type char" 44 atf_set "require.progs" "cc" 45 } 46 47 ctype_abuse_body() 48 { 49 local ctypefn reftype 50 51 ctypefn=$1 52 reftype=$2 53 54 case $reftype in 55 var) decl='x'; ref='x';; 56 ptr) decl='*x'; ref='*x';; 57 array) decl='x[]'; ref='x[0]';; 58 funcall) 59 decl='f(void)'; ref='f()';; 60 esac 61 62 cat >test.c <<EOF 63 #include <ctype.h> 64 65 extern char $decl; 66 67 int 68 g(void) 69 { 70 71 return $ctypefn($ref); 72 } 73 EOF 74 case $reftype in 75 var) atf_expect_fail 'PR lib/58912: ctype(3) abuse detection' \ 76 ' fails for variable references';; 77 esac 78 atf_check -s not-exit:0 \ 79 -e match:'array subscript has type.*char.*-W.*char-subscripts' \ 80 cc -c -Wall -Werror test.c 81 } 82 83 ctype_abuse_tests() 84 { 85 local ctypefn reftype tc 86 87 for ctypefn in \ 88 isalpha \ 89 isupper \ 90 islower \ 91 isdigit \ 92 isxdigit \ 93 isalnum \ 94 isspace \ 95 ispunct \ 96 isprint \ 97 isgraph \ 98 iscntrl \ 99 isblank \ 100 toupper \ 101 tolower \ 102 # end of ctypefn enumeration 103 do 104 for reftype in var ptr array funcall; do 105 tc=${ctypefn}_${reftype} 106 eval "atf_test_case $tc" 107 eval "${tc}_head() 108 { 109 ctype_abuse_head $ctypefn $reftype 110 }" 111 eval "${tc}_body() 112 { 113 ctype_abuse_body $ctypefn $reftype 114 }" 115 atf_add_test_case $tc 116 done 117 done 118 } 119 120 atf_init_test_cases() 121 { 122 123 ctype_abuse_tests 124 } 125