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.
This commit is contained in:
rillig 2021-08-20 05:45:19 +00:00
parent bcffde7ea8
commit 9108d0c35f
5 changed files with 72 additions and 25 deletions

View File

@ -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
}

View File

@ -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/19 21:21:04 rillig Exp $");
#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 @@ 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;
}

View File

@ -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 *);

View File

@ -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 **fname)
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);

View File

@ -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);