From 145c64e6c4e4151e869104e12da71786b8c31932 Mon Sep 17 00:00:00 2001 From: Timo Weingärtner Date: Thu, 22 Apr 2021 23:33:14 +0200 Subject: replace strcpy() with something clang-tidy doesn't complain about --- ssh-agent-filter.C | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/ssh-agent-filter.C b/ssh-agent-filter.C index cede337..175fba2 100644 --- a/ssh-agent-filter.C +++ b/ssh-agent-filter.C @@ -132,12 +132,11 @@ void arm(std::ios & stream) { int make_upstream_agent_conn () { char const * path; - int sock; - struct sockaddr_un addr; if (!(path = getenv("SSH_AUTH_SOCK"))) throw invalid_argument("no $SSH_AUTH_SOCK"); + int sock; { std::lock_guard lock{fd_fork_mutex}; if ((sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) == -1) @@ -145,13 +144,13 @@ int make_upstream_agent_conn () { cloexec(sock); } - addr.sun_family = AF_UNIX; + struct sockaddr_un addr{AF_UNIX, {}}; - if (strlen(path) >= sizeof(addr.sun_path)) + if (auto len = strlen(path); len < sizeof(addr.sun_path)) + std::copy(path, path + len, addr.sun_path); + else throw length_error("$SSH_AUTH_SOCK too long"); - strcpy(addr.sun_path, path); - if (connect(sock, reinterpret_cast(&addr), sizeof(addr))) throw system_error(errno, system_category(), "connect"); @@ -160,8 +159,6 @@ int make_upstream_agent_conn () { int make_listen_sock () { int sock; - struct sockaddr_un addr; - { std::lock_guard lock{fd_fork_mutex}; if ((sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) == -1) @@ -172,13 +169,13 @@ int make_listen_sock () { if (fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK)) throw system_error(errno, system_category(), "fcntl"); - addr.sun_family = AF_UNIX; + struct sockaddr_un addr{AF_UNIX, {}}; - if (path.native().length() >= sizeof(addr.sun_path)) + if (path.native().length() < sizeof(addr.sun_path)) + std::copy(path.native().begin(), path.native().end(), addr.sun_path); + else throw length_error("path for listen socket too long"); - strcpy(addr.sun_path, path.c_str()); - if (bind(sock, reinterpret_cast(&addr), sizeof(addr))) throw system_error(errno, system_category(), "bind"); -- cgit v1.2.3