Module Name: src Committed By: rillig Date: Fri Aug 20 05:45:20 UTC 2021
Modified Files: src/tests/usr.bin/mkdep: t_findcc.sh src/usr.bin/mkdep: findcc.c findcc.h mkdep.c src/usr.bin/xlint/xlint: xlint.c Log Message: mkdep: make argument of findcc const Previously, findcc modified its argument string, even though it had been declared as 'const char *'. This triggered a lint warning that "strchr effectively discards 'const char *' from argument", in fact, this code caused the lint check to be implemented in the first place. The first attempt at fixing it by removing the 'const' from the parameter type was a bad idea since it made the API of that function more complicated. Revert back to making the parameter a 'const char *' and duplicate that string internally as necessary. Add a few more tests for absolute pathnames since these had been missing before. There are no tests yet for snprintf with too long strings, but the current change does not modify that part of the code. To generate a diff of this commit: cvs rdiff -u -r1.1 -r1.2 src/tests/usr.bin/mkdep/t_findcc.sh cvs rdiff -u -r1.8 -r1.9 src/usr.bin/mkdep/findcc.c cvs rdiff -u -r1.2 -r1.3 src/usr.bin/mkdep/findcc.h cvs rdiff -u -r1.46 -r1.47 src/usr.bin/mkdep/mkdep.c cvs rdiff -u -r1.78 -r1.79 src/usr.bin/xlint/xlint/xlint.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/usr.bin/mkdep/t_findcc.sh diff -u src/tests/usr.bin/mkdep/t_findcc.sh:1.1 src/tests/usr.bin/mkdep/t_findcc.sh:1.2 --- src/tests/usr.bin/mkdep/t_findcc.sh:1.1 Wed Aug 11 20:42:26 2021 +++ src/tests/usr.bin/mkdep/t_findcc.sh Fri Aug 20 05:45:19 2021 @@ -1,4 +1,4 @@ -# $NetBSD: t_findcc.sh,v 1.1 2021/08/11 20:42:26 rillig Exp $ +# $NetBSD: t_findcc.sh,v 1.2 2021/08/20 05:45:19 rillig Exp $ # # Copyright (c) 2021 The NetBSD Foundation, Inc. # All rights reserved. @@ -75,8 +75,8 @@ rel_not_found_body() { "$(atf_get_srcdir)"/h_findcc 'bin/echo' } -# If the program name contains a slash in the middle, it is interpreted -# relative to the current directory. +# If the program name contains a slash, no matter where, the program is not +# searched in the PATH. This is the same behavior as in /bin/sh. # atf_test_case rel_found rel_found_body() { @@ -106,6 +106,40 @@ rel_arg_found_body() { } +atf_test_case abs_not_found +abs_not_found_body() { + atf_check -o "inline:(not found)$n" \ + env -i \ + "$(atf_get_srcdir)"/h_findcc "$PWD/nonexistent/echo" +} + +atf_test_case abs_found +abs_found_body() { + mkdir bin + echo '#! /bin/sh' > bin/echo + chmod +x bin/echo + + atf_check -o "inline:$PWD/bin/echo$n" \ + env -i \ + "$(atf_get_srcdir)"/h_findcc "$PWD/bin/echo" +} + +# If the program name is an absolute pathname, the arguments are discarded. +# +# XXX: Discarding the arguments feels unintended. +# +atf_test_case abs_arg_found +abs_arg_found_body() { + mkdir bin + echo '#! /bin/sh' > bin/echo + chmod +x bin/echo + + atf_check -o "inline:$PWD/bin/echo$n" \ + env -i \ + "$(atf_get_srcdir)"/h_findcc "$PWD/bin/echo arg" +} + + atf_init_test_cases() { atf_add_test_case base_not_found atf_add_test_case base_found @@ -114,4 +148,8 @@ atf_init_test_cases() { atf_add_test_case rel_not_found atf_add_test_case rel_found atf_add_test_case rel_arg_found + + atf_add_test_case abs_not_found + atf_add_test_case abs_found + atf_add_test_case abs_arg_found } Index: src/usr.bin/mkdep/findcc.c diff -u src/usr.bin/mkdep/findcc.c:1.8 src/usr.bin/mkdep/findcc.c:1.9 --- src/usr.bin/mkdep/findcc.c:1.8 Thu Aug 19 21:21:04 2021 +++ src/usr.bin/mkdep/findcc.c Fri Aug 20 05:45:19 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: findcc.c,v 1.8 2021/08/19 21:21:04 rillig Exp $ */ +/* $NetBSD: findcc.c,v 1.9 2021/08/20 05:45:19 rillig Exp $ */ /*- * Copyright (c) 1999 The NetBSD Foundation, Inc. @@ -37,7 +37,7 @@ #if !defined(lint) __COPYRIGHT("@(#) Copyright (c) 1999 The NetBSD Foundation, Inc.\ All rights reserved."); -__RCSID("$NetBSD: findcc.c,v 1.8 2021/08/19 21:21:04 rillig Exp $"); +__RCSID("$NetBSD: findcc.c,v 1.9 2021/08/20 05:45:19 rillig Exp $"); #endif /* not lint */ #include <sys/param.h> @@ -49,21 +49,29 @@ __RCSID("$NetBSD: findcc.c,v 1.8 2021/08 #include "findcc.h" char * -findcc(char *progname) +findcc(const char *cc_command) { - char *path, *dir, *next; + char *progname, *path, *dir, *next; char buffer[MAXPATHLEN]; - if ((next = strchr(progname, ' ')) != NULL) { + if ((progname = strdup(cc_command)) == NULL) + return NULL; + + if ((next = strchr(progname, ' ')) != NULL) *next = '\0'; - } - if (strchr(progname, '/') != NULL) - return access(progname, X_OK) ? NULL : strdup(progname); + if (strchr(progname, '/') != NULL) { + if (access(progname, X_OK) == 0) + return progname; + free(progname); + return NULL; + } if (((path = getenv("PATH")) == NULL) || - ((path = strdup(path)) == NULL)) + ((path = strdup(path)) == NULL)) { + free(progname); return NULL; + } dir = path; while (dir != NULL) { @@ -72,8 +80,9 @@ findcc(char *progname) if (snprintf(buffer, sizeof(buffer), "%s/%s", dir, progname) < (int)sizeof(buffer)) { - if (!access(buffer, X_OK)) { + if (access(buffer, X_OK) == 0) { free(path); + free(progname); return strdup(buffer); } } @@ -81,6 +90,6 @@ findcc(char *progname) } free(path); + free(progname); return NULL; } - Index: src/usr.bin/mkdep/findcc.h diff -u src/usr.bin/mkdep/findcc.h:1.2 src/usr.bin/mkdep/findcc.h:1.3 --- src/usr.bin/mkdep/findcc.h:1.2 Thu Aug 19 21:21:04 2021 +++ src/usr.bin/mkdep/findcc.h Fri Aug 20 05:45:19 2021 @@ -1,3 +1,5 @@ +/* $NetBSD: findcc.h,v 1.3 2021/08/20 05:45:19 rillig Exp $ */ + #define DEFAULT_CC "cc" -char *findcc(char *); +char *findcc(const char *); Index: src/usr.bin/mkdep/mkdep.c diff -u src/usr.bin/mkdep/mkdep.c:1.46 src/usr.bin/mkdep/mkdep.c:1.47 --- src/usr.bin/mkdep/mkdep.c:1.46 Fri Aug 20 04:23:56 2021 +++ src/usr.bin/mkdep/mkdep.c Fri Aug 20 05:45:19 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: mkdep.c,v 1.46 2021/08/20 04:23:56 rillig Exp $ */ +/* $NetBSD: mkdep.c,v 1.47 2021/08/20 05:45:19 rillig Exp $ */ /*- * Copyright (c) 1999 The NetBSD Foundation, Inc. @@ -37,7 +37,7 @@ #if !defined(lint) __COPYRIGHT("@(#) Copyright (c) 1999 The NetBSD Foundation, Inc.\ All rights reserved."); -__RCSID("$NetBSD: mkdep.c,v 1.46 2021/08/20 04:23:56 rillig Exp $"); +__RCSID("$NetBSD: mkdep.c,v 1.47 2021/08/20 05:45:19 rillig Exp $"); #endif /* not lint */ #include <sys/mman.h> @@ -95,8 +95,7 @@ usage(void) static int run_cc(int argc, char **argv, const char **fname) { - static char default_cc[] = DEFAULT_CC; - char *CC; + const char *CC; const char *tmpdir; char * volatile pathname; static char tmpfilename[MAXPATHLEN]; @@ -106,7 +105,7 @@ run_cc(int argc, char **argv, const char int status; if ((CC = getenv("CC")) == NULL) - CC = default_cc; + CC = DEFAULT_CC; if ((pathname = findcc(CC)) == NULL) if (!setenv("PATH", DEFAULT_PATH, 1)) pathname = findcc(CC); Index: src/usr.bin/xlint/xlint/xlint.c diff -u src/usr.bin/xlint/xlint/xlint.c:1.78 src/usr.bin/xlint/xlint/xlint.c:1.79 --- src/usr.bin/xlint/xlint/xlint.c:1.78 Thu Aug 19 21:21:04 2021 +++ src/usr.bin/xlint/xlint/xlint.c Fri Aug 20 05:45:19 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: xlint.c,v 1.78 2021/08/19 21:21:04 rillig Exp $ */ +/* $NetBSD: xlint.c,v 1.79 2021/08/20 05:45:19 rillig Exp $ */ /* * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. @@ -38,7 +38,7 @@ #include <sys/cdefs.h> #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: xlint.c,v 1.78 2021/08/19 21:21:04 rillig Exp $"); +__RCSID("$NetBSD: xlint.c,v 1.79 2021/08/20 05:45:19 rillig Exp $"); #endif #include <sys/param.h> @@ -612,10 +612,9 @@ main(int argc, char *argv[]) static void fname(const char *name) { - static char default_cc[] = DEFAULT_CC; const char *bn, *suff; char **args, *ofn, *pathname; - char *CC; + const char *CC; size_t len; int fd; @@ -662,7 +661,7 @@ fname(const char *name) /* run cc */ if ((CC = getenv("CC")) == NULL) - CC = default_cc; + CC = DEFAULT_CC; if ((pathname = findcc(CC)) == NULL) if (setenv("PATH", DEFAULT_PATH, 1) == 0) pathname = findcc(CC);