From 5cd81c9f1f061fbd01380e681aa183439659ee18 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Tue, 3 Feb 2026 19:52:00 -0800 Subject: [PATCH] Agent Update 1. Fix a couple unused variable warnings. 2. In wolfSSH_AGENT_DefaultActions(), fix comparison to the result of snprintf() treating normal result as an error. Reset the return code for the error state of the socket() command. Better cleanup of agent startup failures. --- examples/echoserver/echoserver.c | 26 ++++++++++++++++++++------ src/agent.c | 2 ++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/examples/echoserver/echoserver.c b/examples/echoserver/echoserver.c index cbf2662b..97b4a61a 100644 --- a/examples/echoserver/echoserver.c +++ b/examples/echoserver/echoserver.c @@ -383,6 +383,7 @@ static int wolfSSH_AGENT_DefaultActions(WS_AgentCbAction action, void* vCtx) if (action == WOLFSSH_AGENT_LOCAL_SETUP) { struct sockaddr_un* name = &ctx->name; size_t size; + int envSet = 0, nameBound = 0; WMEMSET(name, 0, sizeof(struct sockaddr_un)); ctx->pid = getpid(); @@ -390,15 +391,16 @@ static int wolfSSH_AGENT_DefaultActions(WS_AgentCbAction action, void* vCtx) ret = snprintf(name->sun_path, sizeof(name->sun_path), "/tmp/wolfserver.%d", ctx->pid); + if (ret >= 0) { + name->sun_path[sizeof(name->sun_path) - 1] = '\0'; + size = WSTRLEN(name->sun_path); + ret = (size < WSTRLEN("/tmp/wolfserver.")); + } if (ret == 0) { - name->sun_path[sizeof(name->sun_path) - 1] = '\0'; - size = WSTRLEN(name->sun_path) + - offsetof(struct sockaddr_un, sun_path); + size += offsetof(struct sockaddr_un, sun_path); ctx->listenFd = socket(AF_UNIX, SOCK_STREAM, 0); - if (ctx->listenFd == -1) { - ret = -1; - } + ret = (ctx->listenFd == -1) ? -1 : 0; } if (ret == 0) { @@ -407,10 +409,12 @@ static int wolfSSH_AGENT_DefaultActions(WS_AgentCbAction action, void* vCtx) } if (ret == 0) { + nameBound = 1; ret = setenv(EnvNameAuthPort, name->sun_path, 1); } if (ret == 0) { + envSet = 1; ret = listen(ctx->listenFd, 5); } @@ -418,6 +422,16 @@ static int wolfSSH_AGENT_DefaultActions(WS_AgentCbAction action, void* vCtx) ctx->state = AGENT_STATE_LISTEN; } else { + if (nameBound) { + unlink(ctx->name.sun_path); + } + if (envSet) { + unsetenv(EnvNameAuthPort); + } + if (ctx->listenFd >= 0) { + close(ctx->listenFd); + ctx->listenFd = -1; + } ret = WS_AGENT_SETUP_E; } } diff --git a/src/agent.c b/src/agent.c index 5cc160d6..dd5bb878 100644 --- a/src/agent.c +++ b/src/agent.c @@ -374,6 +374,7 @@ static int PostLock(WOLFSSH_AGENT_CTX* agent, word32 ppSz; WLOG(WS_LOG_AGENT, "Posting lock to agent %p", agent); + WOLFSSH_UNUSED(agent); ppSz = sizeof(pp) - 1; if (passphraseSz < ppSz) @@ -395,6 +396,7 @@ static int PostUnlock(WOLFSSH_AGENT_CTX* agent, word32 ppSz; WLOG(WS_LOG_AGENT, "Posting unlock to agent %p", agent); + WOLFSSH_UNUSED(agent); ppSz = sizeof(pp) - 1; if (passphraseSz < ppSz)