From d7bbe3de5cc58c1dca41149df89cfdc08019a722 Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Tue, 14 Mar 2017 07:32:08 -0400 Subject: [PATCH] swtpm_ioctl: Rearrange code for Unix client socket setup Rearrange the code of the setup of the Unix client socket. Signed-off-by: Stefan Berger --- src/swtpm_ioctl/tpm_ioctl.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/swtpm_ioctl/tpm_ioctl.c b/src/swtpm_ioctl/tpm_ioctl.c index b8d858f..0c7f5cc 100644 --- a/src/swtpm_ioctl/tpm_ioctl.c +++ b/src/swtpm_ioctl/tpm_ioctl.c @@ -571,17 +571,17 @@ static int open_connection(const char *devname, char *tcp_hostname, fprintf(stderr, "Could not connect using TCP socket.\n"); } } else if (unix_path) { + struct sockaddr_un addr; + + if (strlen(unix_path) + 1 > sizeof(addr.sun_path)) { + fprintf(stderr, "Socket path is too long.\n"); + return -1; + } + fd = socket(AF_UNIX, SOCK_STREAM, 0); if (fd > 0) { - struct sockaddr_un addr; - - if (strlen(unix_path) + 1 > sizeof(addr.sun_path)) { - fprintf(stderr, "Socket path is too long.\n"); - return -1; - } - addr.sun_family = AF_UNIX; - strcpy(addr.sun_path, unix_path); + strncpy(addr.sun_path, unix_path, sizeof(addr.sun_path)); if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { close(fd);