Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 11 Dec 2008 20:04:36 +0100
From:      Ed Schouten <ed@80386.nl>
To:        FreeBSD Arch <freebsd-arch@freebsd.org>
Subject:   [Patch] strnlen(3)
Message-ID:  <20081211190436.GE1176@hoeg.nl>
In-Reply-To: <20081211175519.GD1176@hoeg.nl>
References:  <49381DD4.2000506@kasimir.com> <20081211175519.GD1176@hoeg.nl>

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

--WlEyl6ow+jlIgNUh
Content-Type: multipart/mixed; boundary="ytoMbUMiTKPMT3hY"
Content-Disposition: inline


--ytoMbUMiTKPMT3hY
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hello all,

I've attached a patch, that adds strnlen(3) to libc. It also moves
strndup(3) out of __BSD_VISIBLE. I'll see if it survives `make universe'
and commit it soonish. Any comments?

--=20
 Ed Schouten <ed@80386.nl>
 WWW: http://80386.nl/

--ytoMbUMiTKPMT3hY
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment; filename="strnlen.diff"
Content-Transfer-Encoding: quoted-printable

Index: include/string.h
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- include/string.h	(revision 185939)
+++ include/string.h	(working copy)
@@ -95,8 +95,9 @@
 char	*strncat(char * __restrict, const char * __restrict, size_t);
 int	 strncmp(const char *, const char *, size_t) __pure;
 char	*strncpy(char * __restrict, const char * __restrict, size_t);
+char	*strndup(const char *, size_t);
+size_t	 strnlen(const char *, size_t) __pure;
 #if __BSD_VISIBLE
-char	*strndup(const char *, size_t);
 char	*strnstr(const char *, const char *, size_t) __pure;
 #endif
 char	*strpbrk(const char *, const char *) __pure;
Index: lib/libc/string/Symbol.map
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- lib/libc/string/Symbol.map	(revision 185939)
+++ lib/libc/string/Symbol.map	(working copy)
@@ -82,6 +82,7 @@
 	flsll;
 	memrchr;
 	strndup;
+	strnlen;
 };
=20
 FBSDprivate_1.0 {
Index: lib/libc/string/Makefile.inc
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- lib/libc/string/Makefile.inc	(revision 185939)
+++ lib/libc/string/Makefile.inc	(working copy)
@@ -11,7 +11,7 @@
 	memcpy.c memmem.c memmove.c memset.c rindex.c stpcpy.c strcasecmp.c \
 	strcat.c strcasestr.c strchr.c strcmp.c strcoll.c strcpy.c strcspn.c \
 	strdup.c strerror.c strlcat.c strlcpy.c strlen.c strmode.c strncat.c \
-	strncmp.c strncpy.c strndup.c strnstr.c \
+	strncmp.c strncpy.c strndup.c strnlen.c strnstr.c \
 	strpbrk.c strrchr.c strsep.c strsignal.c strspn.c strstr.c strtok.c \
 	strxfrm.c swab.c wcscat.c wcschr.c wcscmp.c wcscoll.c wcscpy.c \
 	wcscspn.c wcsdup.c \
@@ -52,6 +52,7 @@
 MLINKS+=3Dstrerror.3 perror.3 strerror.3 sys_errlist.3 strerror.3 sys_nerr=
=2E3
 MLINKS+=3Dstrerror.3 strerror_r.3
 MLINKS+=3Dstrlcpy.3 strlcat.3
+MLINKS+=3Dstrlen.3 strnlen.3
 MLINKS+=3Dstrtok.3 strtok_r.3
 MLINKS+=3Dstrstr.3 strcasestr.3
 MLINKS+=3Dstrstr.3 strnstr.3
Index: lib/libc/string/strnlen.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- lib/libc/string/strnlen.c	(revision 0)
+++ lib/libc/string/strnlen.c	(revision 0)
@@ -0,0 +1,40 @@
+/*-
+ * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURP=
OSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENT=
IAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STR=
ICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY W=
AY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <string.h>
+
+size_t
+strnlen(const char *str, size_t l)
+{
+	const char *s;
+
+	for (s =3D str; l > 0 && *s !=3D '\0'; l--, s++);
+
+	return (s - str);
+}
Index: lib/libc/string/strlen.3
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
--- lib/libc/string/strlen.3	(revision 185939)
+++ lib/libc/string/strlen.3	(working copy)
@@ -36,7 +36,8 @@
 .Dt STRLEN 3
 .Os
 .Sh NAME
-.Nm strlen
+.Nm strlen ,
+.Nm strnlen ,
 .Nd find length of string
 .Sh LIBRARY
 .Lb libc
@@ -44,21 +45,33 @@
 .In string.h
 .Ft size_t
 .Fn strlen "const char *s"
+.Ft size_t
+.Fn strnlen "const char *s" "size_t maxlen"
 .Sh DESCRIPTION
 The
 .Fn strlen
-function
+and
+.Fn strnlen
+functions
 computes the length of the string
 .Fa s .
+The
+.Fn strnlen
+function shall never examine more than
+.Fa maxlen
+bytes.
 .Sh RETURN VALUES
 The
 .Fn strlen
-function
-returns
-the number of characters that precede the
-terminating
+and
+.Fn strnlen
+functions return the number of characters that precede the terminating
 .Dv NUL
 character.
+In addition, the
+.Fn strnlen
+function never returns a value greater than
+.Fa maxlen .
 .Sh SEE ALSO
 .Xr string 3
 .Sh STANDARDS

--ytoMbUMiTKPMT3hY--

--WlEyl6ow+jlIgNUh
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (FreeBSD)

iEYEARECAAYFAklBZEQACgkQ52SDGA2eCwV9WgCdF4mWfSaRiLuKgoQh8evltTvC
VvYAn0GcytWwYl4/u9cqKqhSVah+1me1
=6WmV
-----END PGP SIGNATURE-----

--WlEyl6ow+jlIgNUh--



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