Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Sat, 14 May 2022 23:16:14 +0100
From: Oliver Ford <ojford@...il.com>
To: Rich Felker <dalias@...c.org>
Cc: musl@...ts.openwall.com
Subject: Re: [PATCH musl v2 3/3] mntent: fix parsing lines with
 optional fields

On Sat, May 14, 2022 at 5:24 AM Rich Felker <dalias@...c.org> wrote:
> This has UB unless ptr points to at least len bytes. You can only
> evaluate ptr+len or ptr[len] after confirming that. The strncmp is the
> most natural (and free) way of confirming it. You could assign end
> after the strncmp but I think just using ptr[len] instead of *end
> makes it easier.
>
> >         if (!strncmp(ptr, opt, len) &&
> >                  (*end == '\0' || *end == ',' || *end == '=')) return ptr;
> >         ptr = strchr(ptr, ',');
> >         if (ptr) ptr++;
> >     }
> >
> >     return NULL;
>
> Minor style nit: NULL is generally not used in musl anymore; just 0 is
> fine here.
>
> BTW you dropped the list from CC; I re-added it.
>
> Rich

Version below should be correct, now uses ptr[len] instead of the *end.

char *hasmntopt(const struct mntent *mnt, const char *opt)
{
    char *ptr = mnt->mnt_opts;
    size_t len = strlen(opt);

    while (ptr) {
        if (!strncmp(ptr, opt, len) &&
            (ptr[len] == '\0' || ptr[len] == ',' || ptr[len] == '='))
return ptr;
        ptr = strchr(ptr, ',');
        if (ptr) ptr++;
    }

    return 0;
}

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.