From owner-freebsd-bugs@FreeBSD.ORG Sat Jan 5 11:10:02 2013 Return-Path: Delivered-To: freebsd-bugs@smarthost.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id C9CF215B for ; Sat, 5 Jan 2013 11:10:02 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:1900:2254:206c::16:87]) by mx1.freebsd.org (Postfix) with ESMTP id B06AC96B for ; Sat, 5 Jan 2013 11:10:02 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.5/8.14.5) with ESMTP id r05BA2IA027314 for ; Sat, 5 Jan 2013 11:10:02 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.5/8.14.5/Submit) id r05BA2qU027313; Sat, 5 Jan 2013 11:10:02 GMT (envelope-from gnats) Resent-Date: Sat, 5 Jan 2013 11:10:02 GMT Resent-Message-Id: <201301051110.r05BA2qU027313@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Jukka Ukkonen Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id 65056151 for ; Sat, 5 Jan 2013 11:08:57 +0000 (UTC) (envelope-from nobody@FreeBSD.org) Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22]) by mx1.freebsd.org (Postfix) with ESMTP id 56BDA963 for ; Sat, 5 Jan 2013 11:08:57 +0000 (UTC) Received: from red.freebsd.org (localhost [127.0.0.1]) by red.freebsd.org (8.14.5/8.14.5) with ESMTP id r05B8uH6029335 for ; Sat, 5 Jan 2013 11:08:56 GMT (envelope-from nobody@red.freebsd.org) Received: (from nobody@localhost) by red.freebsd.org (8.14.5/8.14.5/Submit) id r05B8uMk029334; Sat, 5 Jan 2013 11:08:56 GMT (envelope-from nobody) Message-Id: <201301051108.r05B8uMk029334@red.freebsd.org> Date: Sat, 5 Jan 2013 11:08:56 GMT From: Jukka Ukkonen To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-3.1 Subject: kern/175001: The current strnstr() function should be named strlstr() X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 05 Jan 2013 11:10:02 -0000 >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: