aboutsummaryrefslogtreecommitdiff
path: root/ssh-agent-filter.C
diff options
context:
space:
mode:
authorTimo Weingärtner <timo@tiwe.de>2021-04-22 23:33:14 +0200
committerTimo Weingärtner <timo@tiwe.de>2026-03-07 22:25:16 +0100
commit145c64e6c4e4151e869104e12da71786b8c31932 (patch)
treeded364c4ba5e19c074ae1e5c9d512d19c35a6726 /ssh-agent-filter.C
parentfc0e196e4fd302d681725c9e5549e34b93a91075 (diff)
downloadssh-agent-filter-145c64e6c4e4151e869104e12da71786b8c31932.tar.gz
replace strcpy() with something clang-tidy doesn't complain about
Diffstat (limited to 'ssh-agent-filter.C')
-rw-r--r--ssh-agent-filter.C21
1 files 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<struct sockaddr const *>(&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<struct sockaddr const *>(&addr), sizeof(addr)))
throw system_error(errno, system_category(), "bind");