Date:        Thu, 19 Aug 2021 21:21:04 +0000
    From:        "Roland Illig" <ril...@netbsd.org>
    Message-ID:  <20210819212104.7c965f...@cvs.netbsd.org>

  | mkdep: fix prototype of findcc

That broke the build.

  | A function that modifies a string argument must not declare that
  | argument as 'const char *', even if all callers (mkdep and lint) always
  | pass it a modifiable string.

Apparently they don't (but they probably don't pass unmodifiable
strings containing spaces).

The right thing to do is to handle the FIXME in the comment,
and fix it, instead of enshrining it by changing the prototype.

Something like

char *
findcc(const char *progname)
{
        char   *path, *dir, *next;
        char   buffer[MAXPATHLEN];
        size_t len;

        if ((next = strchr(progname, ' ')) != NULL) {
                if (next > progname)
                        len = (size_t)(next - progname);
                else
                        return NULL;
        } else
                len = strlen(progname);

        /* there could be a test that len <= MAXINT, but really... */

        if (memchr(progname, '/', len) != NULL) {
                path = strndup(progname, len);

                if (access(path, X_OK) == 0)
                        return path;
                free(path);
                return NULL;
        }

        if (((path = getenv("PATH")) == NULL) ||
            ((path = strdup(path)) == NULL))
                return NULL;

        dir = path;
        while (dir != NULL) {
                if ((next = strchr(dir, ':')) != NULL)
                        *next++ = '\0';

                if (snprintf(buffer, sizeof(buffer),
                    "%s/%.*s", dir, (int)len, progname) < (int)sizeof(buffer)) {
                        if (!access(buffer, X_OK)) {
                                free(path);
                                return strdup(buffer);
                        }
                }
                dir = next;
        }

        free(path);
        return NULL;
}

That's untested, not even compile tested, and it should be before being
committed (fully tested, in a release build, with ATF tests run), but
something like that should work, and work without modifying the input
string, which should immediately go back to being const while this is being
worked out, to unbreak the build.

kre

ps: I cut & pasted the code from an xterm, so tabs will have been lost
all over the place.   That needs fixing for sure.

Reply via email to