Module Name: src Committed By: riastradh Date: Wed Dec 18 02:47:00 UTC 2024
Modified Files: src/distrib/sets/lists/tests: mi src/tests/usr.bin/cc: Makefile Added Files: src/tests/usr.bin/cc: t_ctype_abuse.sh Log Message: tests/usr.bin/cc: Add test for ctype(3) detection. PR lib/58912: ctype(3) abuse detection fails for variable references To generate a diff of this commit: cvs rdiff -u -r1.1350 -r1.1351 src/distrib/sets/lists/tests/mi cvs rdiff -u -r1.8 -r1.9 src/tests/usr.bin/cc/Makefile cvs rdiff -u -r0 -r1.1 src/tests/usr.bin/cc/t_ctype_abuse.sh Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/distrib/sets/lists/tests/mi diff -u src/distrib/sets/lists/tests/mi:1.1350 src/distrib/sets/lists/tests/mi:1.1351 --- src/distrib/sets/lists/tests/mi:1.1350 Tue Dec 17 17:56:45 2024 +++ src/distrib/sets/lists/tests/mi Wed Dec 18 02:47:00 2024 @@ -1,4 +1,4 @@ -# $NetBSD: mi,v 1.1350 2024/12/17 17:56:45 christos Exp $ +# $NetBSD: mi,v 1.1351 2024/12/18 02:47:00 riastradh Exp $ # # Note: don't delete entries from here - mark them as "obsolete" instead. # @@ -4799,6 +4799,7 @@ ./usr/tests/usr.bin/cc/t_asan_off_by_one tests-usr.bin-tests compattestfile,atf ./usr/tests/usr.bin/cc/t_asan_poison tests-usr.bin-tests compattestfile,atf ./usr/tests/usr.bin/cc/t_asan_uaf tests-usr.bin-tests compattestfile,atf +./usr/tests/usr.bin/cc/t_ctype_abuse tests-usr.bin-tests compattestfile,atf ./usr/tests/usr.bin/cc/t_fuzzer_oom tests-usr.bin-tests compattestfile,atf ./usr/tests/usr.bin/cc/t_fuzzer_simple tests-usr.bin-tests compattestfile,atf ./usr/tests/usr.bin/cc/t_fuzzer_timeout tests-usr.bin-tests compattestfile,atf Index: src/tests/usr.bin/cc/Makefile diff -u src/tests/usr.bin/cc/Makefile:1.8 src/tests/usr.bin/cc/Makefile:1.9 --- src/tests/usr.bin/cc/Makefile:1.8 Sun Aug 18 20:15:58 2019 +++ src/tests/usr.bin/cc/Makefile Wed Dec 18 02:47:00 2024 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.8 2019/08/18 20:15:58 kamil Exp $ +# $NetBSD: Makefile,v 1.9 2024/12/18 02:47:00 riastradh Exp $ .include <bsd.own.mk> @@ -22,6 +22,8 @@ UBSAN_TESTS+= t_ubsan_int_divzero TESTS_SH= # TESTS_SH+= $(ASAN_TESTS) TESTS_SH+= $(UBSAN_TESTS) + +TESTS_SH+= t_ctype_abuse TESTS_SH+= t_hello TESTS_SH+= t_libgomp Added files: Index: src/tests/usr.bin/cc/t_ctype_abuse.sh diff -u /dev/null src/tests/usr.bin/cc/t_ctype_abuse.sh:1.1 --- /dev/null Wed Dec 18 02:47:00 2024 +++ src/tests/usr.bin/cc/t_ctype_abuse.sh Wed Dec 18 02:47:00 2024 @@ -0,0 +1,124 @@ +# $NetBSD: t_ctype_abuse.sh,v 1.1 2024/12/18 02:47:00 riastradh Exp $ +# +# Copyright (c) 2024 The NetBSD Foundation, Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS +# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS +# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# + +ctype_abuse_head() +{ + local ctypefn reftype desc + + ctypefn=$1 + reftype=$2 + + case $reftype in + var) desc="variable";; + ptr) desc="pointer dereference";; + array) desc="array element";; + funcall) + desc="function call";; + esac + + atf_set "descr" "Test that $ctypefn warns on $desc of type char" + atf_set "require.progs" "cc" +} + +ctype_abuse_body() +{ + local ctypefn reftype + + ctypefn=$1 + reftype=$2 + + case $reftype in + var) decl='x'; ref='x';; + ptr) decl='*x'; ref='*x';; + array) decl='x[]'; ref='x[0]';; + funcall) + decl='f(void)'; ref='f()';; + esac + + cat >test.c <<EOF +#include <ctype.h> + +extern char $decl; + +int +g(void) +{ + + return $ctypefn($ref); +} +EOF + case $reftype in + var) atf_expect_fail 'PR lib/58912: ctype(3) abuse detection' \ + ' fails for variable references';; + esac + atf_check -s not-exit:0 \ + -e match:'array subscript has type.*char.*-W.*char-subscripts' \ + cc -c -Wall -Werror test.c +} + +ctype_abuse_tests() +{ + local ctypefn reftype tc + + for ctypefn in \ + isalpha \ + isupper \ + islower \ + isdigit \ + isxdigit \ + isalnum \ + isspace \ + ispunct \ + isprint \ + isgraph \ + iscntrl \ + isblank \ + toupper \ + tolower \ + # end of ctypefn enumeration + do + for reftype in var ptr array funcall; do + tc=${ctypefn}_${reftype} + eval "atf_test_case $tc" + eval "${tc}_head() + { + ctype_abuse_head $ctypefn $reftype + }" + eval "${tc}_body() + { + ctype_abuse_body $ctypefn $reftype + }" + atf_add_test_case $tc + done + done +} + +atf_init_test_cases() +{ + + ctype_abuse_tests +}