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.
This commit is contained in:
parent
4729fbd85f
commit
5578933d56
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=90819
@ -906,6 +906,7 @@ libkern/strtol.c standard
|
||||
libkern/strtoq.c standard
|
||||
libkern/strtoul.c standard
|
||||
libkern/strtouq.c standard
|
||||
libkern/strvalid.c standard
|
||||
net/bpf.c standard
|
||||
net/bpf_filter.c optional bpf
|
||||
bpf.h standard \
|
||||
|
55
sys/libkern/strvalid.c
Normal file
55
sys/libkern/strvalid.c
Normal file
@ -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);
|
||||
}
|
@ -89,6 +89,7 @@ char *strcpy __P((char *, const char *));
|
||||
size_t strlen __P((const char *));
|
||||
int strncmp __P((const char *, const char *, size_t));
|
||||
char *strncpy __P((char *, const char *, size_t));
|
||||
int strvalid __P((const char *, size_t));
|
||||
|
||||
|
||||
static __inline int
|
||||
|
Loading…
Reference in New Issue
Block a user