FreeBSD/sys/compat/cloudabi/cloudabi_file.c
/*
 * Copying pathnames from userspace to kernelspace.
 *
 * Unlike most operating systems, CloudABI doesn't use null-terminated
 * pathname strings. Processes always pass pathnames to the kernel by
 * providing a base pointer and a length. This has a couple of reasons:
 *
 * - It makes it easier to use CloudABI in combination with programming
 *   languages other than C, that may use non-null terminated strings.
 * - It allows for calling system calls on individual components of the
 *   pathname without modifying the input string.
 *
 * The function below copies in pathname strings and null-terminates it.
 * It also ensure that the string itself does not contain any null
 * bytes.
 *
 * TODO(ed): Add an abstraction to vfs_lookup.c that allows us to pass
 *           in unterminated pathname strings, so we can do away with
 *           the copying.
 */
Copying pathnames from userspace to kernelspace.
Unlike most operating systems, CloudABI doesn't use null-terminated
pathname strings. Processes always pass pathnames to the kernel by
providing a base pointer and a length. This has a couple of reasons.

- It makes it easier to use CloudABI in combination with programming
languages other than C, that may use non-null terminated strings.
- It allows for calling system calls on individual components of the
pathname without modifying the input string.

The function below copies in pathname strings and null-terminates it.
It also ensure that the string itself does not contain any null
bytes.

Add an abstraction to vfs_lookup.c that allows us to pass
in unterminated pathname strings, so we can do away with
the copying.
Copying pathnames from userspace to kernelspace . Unlike most operating systems CloudABI doesn ' t use null - terminated pathname strings . Processes always pass pathnames to the kernel by providing a base pointer and a length . This has a couple of reasons . - It makes it easier to use CloudABI in combination with programming languages other than C that may use non - null terminated strings . - It allows for calling system calls on individual components of the pathname without modifying the input string . The function below copies in pathname strings and null - terminates it . It also ensure that the string itself does not contain any null bytes . Add an abstraction to vfs_lookup . c that allows us to pass in unterminated pathname strings so we can do away with the copying .
static int
copyin_path(const char *uaddr, size_t len, char **result)
{
	char *buf;
	int error;
	if (len >= PATH_MAX)
		return (ENAMETOOLONG);
	buf = malloc(len + 1, M_CLOUDABI_PATH, M_WAITOK);
	error = copyin(uaddr, buf, len);
	if (error != 0) {
		free(buf, M_CLOUDABI_PATH);
		return (error);
	}
	if (memchr(buf, '\0', len) != NULL) {
		free(buf, M_CLOUDABI_PATH);
		return (EINVAL);
	}
	buf[len] = '\0';
	*result = buf;
	return (0);
}
static int copyin_path ( const char * uaddr , size_t len , char * * result ) { char * buf ; int error ; if ( len >= PATH_MAX ) return ( ENAMETOOLONG ) ; buf = malloc ( len + 1 , M_CLOUDABI_PATH , M_WAITOK ) ; error = copyin ( uaddr , buf , len ) ; if ( error != 0 ) { free ( buf , M_CLOUDABI_PATH ) ; return ( error ) ; } if ( memchr ( buf , ' \0 ' , len ) != NULL ) { free ( buf , M_CLOUDABI_PATH ) ; return ( EINVAL ) ; } buf [ len ] = ' \0 ' ; * result = buf ; return ( 0 ) ; }
Copying pathnames from userspace to kernelspace.
Copying pathnames from userspace to kernelspace .