summaryrefslogtreecommitdiffstats
path: root/sys/libkern
diff options
context:
space:
mode:
authorrwatson <rwatson@FreeBSD.org>2002-02-18 00:37:03 +0000
committerrwatson <rwatson@FreeBSD.org>2002-02-18 00:37:03 +0000
commit49c65a6b10085c64f7ff4dfb8ccc792f1e9dbc75 (patch)
treeca54ff259a2aa28bbc146e5a0c0d691cd98ede9f /sys/libkern
parent43f97fe4ae07b305a42ebbd3f668b9fdeecbaa5f (diff)
downloadFreeBSD-src-49c65a6b10085c64f7ff4dfb8ccc792f1e9dbc75.zip
FreeBSD-src-49c65a6b10085c64f7ff4dfb8ccc792f1e9dbc75.tar.gz
Add a 'strvalid()' call to libkern. Given a character pointer, and
buffer length, determine if the pointer is to a valid string. Currently, the only check is whether a '\0' appears in the buffer. This is useful when pulling in a structure from userland that may contain one or more strings, and validity testing must be performed on elements of the structure. When copying normal string arguments, copyinstr() is expected to be used.
Diffstat (limited to 'sys/libkern')
-rw-r--r--sys/libkern/strvalid.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/sys/libkern/strvalid.c b/sys/libkern/strvalid.c
new file mode 100644
index 0000000..df1b071
--- /dev/null
+++ b/sys/libkern/strvalid.c
@@ -0,0 +1,55 @@
+/*-
+ * Copyright (c) 2002 Networks Associates Technologies, Inc.
+ * All rights reserved.
+ *
+ * This software was developed for the FreeBSD Project by NAI Labs,
+ * the Security Research Division of Network Associates, Inc. under
+ * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
+ * CHATS research program.
+ *
+ * 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.
+ * 3. The names of the authors may not be used to endorse or promote
+ * products derived from this software without specific prior written
+ * permission.
+ *
+ * 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 PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * 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, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/types.h>
+#include <sys/libkern.h>
+
+/*
+ * Return (1) if the buffer pointed to by kernel pointer 'buffer' and
+ * of length 'bufferlen' contains a valid NUL-terminated string
+ */
+int
+strvalid(const char *buffer, size_t bufferlen)
+{
+ int i;
+
+ /* Must be NUL-terminated. */
+ for (i = 0; i < bufferlen; i++)
+ if (buffer[i] == '\0')
+ return (1);
+
+ return (0);
+}
OpenPOWER on IntegriCloud