Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 5 Jan 2013 11:08:56 GMT
From:      Jukka Ukkonen <jau@iki.fi>
To:        freebsd-gnats-submit@FreeBSD.org
Subject:   kern/175001: The current strnstr() function should be named strlstr()
Message-ID:  <201301051108.r05B8uMk029334@red.freebsd.org>
Resent-Message-ID: <201301051110.r05BA2qU027313@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         175001
>Category:       kern
>Synopsis:       The current strnstr() function should be named strlstr()
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Jan 05 11:10:00 UTC 2013
>Closed-Date:
>Last-Modified:
>Originator:     Jukka Ukkonen
>Release:        9.1-STABLE
>Organization:
-----
>Environment:
FreeBSD sleipnir 9.1-STABLE FreeBSD 9.1-STABLE #0: Sat Jan  5 09:07:29 EET 2013     root@sleipnir:/usr/obj/usr/src/sys/Sleipnir  amd64
>Description:
The current function named strnstr() should be called strlstr().
In the current function the length limit is on the first string parameter.

There are other functions predating the current strnstr() which also place
the limit on the first string of the two string parameters, e.g. strlcpy() and
strlcat().
OTOH the old functions called strncpy() and strncat() place the limit on the
second string parameter.

To maintain the old tradition as well as a sort of least surprise behaviour
there really should be two similar functions:
(1) strlstr (s1, s2, n) and
(2) strnstr (s1, s2, n).
The first one should look for a substring s2 in a limited prefix of s1.
The second one should look for a limited size prefix of s2 in the whole s1.

The current implementation behaves as (1) but has the name of (2).

Currently the only users of the misnamed strnstr() are mount, whois,
wpa_supplicant, and portsnap/phttpget. So, if done sooner than later
the change would not have a big impact.

Neither strlstr() nor strnstr() are controlled by any standards.
Nor are they frequently available in other OS environments.
In e.g. SuSE there is a function named strnstr(), but apparently not in
Red Hat.

>How-To-Repeat:
See the whole philosophy in "full description" above.

>Fix:
Rename the current strnstr() to strlstr() and add a new function called
strnstr() which behaves as the model below...

char *
(strnstr) (s, t, n)
    register const char     *s, *t;
    register const size_t   n;
{
    if (! s || ! t) {
        errno = EFAULT;

        return (NULL);
    }

    if (! *t || ! n)
        return ((char *) s);

    for ( ; (s = strchr (s, *t)); s++) {
        if (! strncmp (s, t, n))
            return ((char *) s);
    }

    return (NULL);
}


>Release-Note:
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201301051108.r05B8uMk029334>