Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src mkdep: make argument of findcc const



details:   https://anonhg.NetBSD.org/src/rev/57b1c22b5236
branches:  trunk
changeset: 1023033:57b1c22b5236
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Aug 20 05:45:19 2021 +0000

description:
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.

diffstat:

 tests/usr.bin/mkdep/t_findcc.sh |  44 ++++++++++++++++++++++++++++++++++++++--
 usr.bin/mkdep/findcc.c          |  31 ++++++++++++++++++----------
 usr.bin/mkdep/findcc.h          |   4 ++-
 usr.bin/mkdep/mkdep.c           |   9 +++----
 usr.bin/xlint/xlint/xlint.c     |   9 +++----
 5 files changed, 72 insertions(+), 25 deletions(-)

diffs (230 lines):

diff -r e1e597e61847 -r 57b1c22b5236 tests/usr.bin/mkdep/t_findcc.sh
--- a/tests/usr.bin/mkdep/t_findcc.sh   Fri Aug 20 04:23:56 2021 +0000
+++ b/tests/usr.bin/mkdep/t_findcc.sh   Fri Aug 20 05:45:19 2021 +0000
@@ -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 @@
                "$(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 @@
 }
 
 
+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_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
 }
diff -r e1e597e61847 -r 57b1c22b5236 usr.bin/mkdep/findcc.c
--- a/usr.bin/mkdep/findcc.c    Fri Aug 20 04:23:56 2021 +0000
+++ b/usr.bin/mkdep/findcc.c    Fri Aug 20 05:45:19 2021 +0000
@@ -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 @@
 #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) {
+               if (access(progname, X_OK) == 0)
+                       return progname;
+               free(progname);
+               return NULL;
        }
 
-       if (strchr(progname, '/') != NULL)
-               return access(progname, X_OK) ? NULL : strdup(progname);
-
        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 @@
 
                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 @@
        }
 
        free(path);
+       free(progname);
        return NULL;
 }
-
diff -r e1e597e61847 -r 57b1c22b5236 usr.bin/mkdep/findcc.h
--- a/usr.bin/mkdep/findcc.h    Fri Aug 20 04:23:56 2021 +0000
+++ b/usr.bin/mkdep/findcc.h    Fri Aug 20 05:45:19 2021 +0000
@@ -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 *);
diff -r e1e597e61847 -r 57b1c22b5236 usr.bin/mkdep/mkdep.c
--- a/usr.bin/mkdep/mkdep.c     Fri Aug 20 04:23:56 2021 +0000
+++ b/usr.bin/mkdep/mkdep.c     Fri Aug 20 05:45:19 2021 +0000
@@ -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 @@
 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 @@
        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);
diff -r e1e597e61847 -r 57b1c22b5236 usr.bin/xlint/xlint/xlint.c
--- a/usr.bin/xlint/xlint/xlint.c       Fri Aug 20 04:23:56 2021 +0000
+++ b/usr.bin/xlint/xlint/xlint.c       Fri Aug 20 05:45:19 2021 +0000
@@ -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 @@
 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 @@
 
        /* 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);



Home | Main Index | Thread Index | Old Index