Skip to content

Commit

Permalink
Fix errors due to bad strncpy behavour.
Browse files Browse the repository at this point in the history
As of GCC 8.1 a compiler error (stringop-truncation) will be raised as
strncpy has potentially unsafe behavour, which is the case here.

If strncpy's source (path in our case) is longer than the destination
the destination will not be null terminated. Which is what sockaddr_un
requires of sockaddr_un.sun_path.

By reducing the copy size by one ensures as much as path as can be
copied is and pads the rest of destination (the remaining one
character) with null bytes, ensuring it's properly null terminated.
  • Loading branch information
Jessica Tallon committed Dec 13, 2018
1 parent 9bbbb09 commit ae0ca39
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/apps/vhost/vhost_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int vhost_user_connect(const char *path)
}

un.sun_family = AF_UNIX;
strncpy(un.sun_path, path, sizeof(un.sun_path));
strncpy(un.sun_path, path, sizeof(un.sun_path)-1);

if (connect(sock, (struct sockaddr *) &un, sizeof(un)) == -1) {
close(sock);
Expand All @@ -54,7 +54,7 @@ int vhost_user_listen(const char *path)
}

un.sun_family = AF_UNIX;
strncpy(un.sun_path, path, sizeof(un.sun_path));
strncpy(un.sun_path, path, sizeof(un.sun_path)-1);
unlink(un.sun_path);
if (bind(sock, (struct sockaddr *) &un, sizeof(un)) == -1) {
close(sock);
Expand Down

0 comments on commit ae0ca39

Please sign in to comment.