Introduce virStrncpy.

Add the virStrncpy function, which takes a dst string, source string,
the number of bytes to copy and the number of bytes available in the
dest string.  If the source string is too large to fit into the
destination string, including the \0 byte, then no data is copied and
the function returns NULL.  Otherwise, this function copies n bytes
from source into dst, including the \0, and returns a pointer to the
dst string.  This function is intended to replace all unsafe uses
of strncpy in the code base, since strncpy does *not* guarantee that
the buffer terminates with a \0.

Signed-off-by: Chris Lalancette <clalance@redhat.com>
This commit is contained in:
Chris Lalancette
2009-08-03 14:37:44 +02:00
parent b81a7ece97
commit 03d777f345
27 changed files with 416 additions and 233 deletions

View File

@@ -170,7 +170,11 @@ proxyListenUnixSocket(const char *path) {
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
addr.sun_path[0] = '\0';
strncpy(&addr.sun_path[1], path, (sizeof(addr) - 4) - 2);
if (virStrcpy(&addr.sun_path[1], path, sizeof(addr.sun_path) - 1) == NULL) {
fprintf(stderr, "Path %s too long to fit into destination\n", path);
close(fd);
return -1;
}
/*
* now bind the socket to that address and listen on it