Skip to content

sftpclient: command dispatch, path, and LS fixes - #1406

Open
ejohnstown wants to merge 4 commits into
wolfSSL:masterfrom
ejohnstown:sftpclient-dispatch
Open

ejohnstown wants to merge 4 commits into
wolfSSL:masterfrom
ejohnstown:sftpclient-dispatch

Conversation

@ejohnstown

Copy link
Copy Markdown
Contributor

Fixes to the example SFTP client's command handling and to the library's directory listing, each with tests.

  • doCmds() selects a command only on the first word of the line, so rm getlog removes getlog instead of fetching og (F-14766, F-16749)
  • doCmds() frees the remote path on every early return (F-16752)
  • clean_path() drops .. only as a whole path component, so cd ..dots stays put (F-11666)
  • wolfSSH_SFTP_LS() ends a listing only on SSH_FX_EOF; any other status frees the partial list and returns NULL with the error kept (F-18849)

Copilot AI balanced review requested due to automatic review settings December 3, 2026 23:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

READDIR can report a partial listing as successful without receiving EOF when STATUS(OK) or NAME decoding/allocation failures leave no error recorded.

1 open issue
What changed in this PR

Improves the example SFTP client’s command handling and the library’s directory-listing error handling.

Changes:

  • Matches whole command keywords and preserves directory names beginning with ...
  • Frees remote-path allocations on early returns.
  • Tracks listing status, rejects partial results on errors, and adds regression tests.
File Description
wolfssh/​wolfsftp.h Documents listing results and failure behavior.
wolfssh/​internal.h Adds NAME-response status tracking.
tests/​unit.c Tests listing EOF and failure statuses.
tests/​sftp.c Tests command dispatch, paths, and unsupported commands.
src/​wolfsftp.c Distinguishes listing EOF from errors and preserves failures.
examples/​sftpclient/​sftpclient.c Fixes dispatch, path cleanup, and allocation cleanup.

Review effort: Balanced

Comment thread src/wolfsftp.c Resolved
@ejohnstown

Copy link
Copy Markdown
Contributor Author

@claude review

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Findings marked 🟡 are optional suggestions and need no follow-up push.

Additional findings (outside the current diff — GitHub can't attach inline comments there):

  • 🟣 src/wolfsftp.c — pre-existing: Callers of wolfSSH_SFTP_LS see a directory they cannot open (permission denied, handle cap) as empty, which the new contract at wolfsftp.h:304 now promises. When the OPENDIR or CLOSE reply is a failure STATUS, wolfSSH_SFTP_GetHandle and wolfSSH_SFTP_Close return an error but leave ssh->error at WS_SUCCESS. LS then returns NULL with no error set. Fix: on every non-retry failure branch in LS (OPENDIR, GETHANDLE, CLOSE), set ssh->error to the step's return code when it is still WS_SUCCESS, so NULL with no error only ever means EOF on an empty listing.

    Why this was flagged

    The trigger is LS on a path that REALPATH resolves but OPENDIR rejects. The wolfSSH server answers with STATUS: PERMISSION from GetAndCleanPath, "Too Many Open Directory Handles", or NOFILE when WOPENDIR fails (src/wolfsftp.c:3246-3273). wolfSSH_SFTP_GetHandle maps that STATUS to ret = WS_SFTP_STATUS_NOT_OK (src/wolfsftp.c:8073) and never writes ssh->error. ssh->error is still WS_SUCCESS because wolfSSH_stream_read sets it at src/ssh.c:1534. The LS GETHANDLE branch (src/wolfsftp.c:8201-8209) then clears state and returns NULL with ssh->error == WS_SUCCESS. The base returned NULL here too, but its doc said NULL means failure. This PR replaces that doc with the wolfsftp.h:304-306 rule that NULL with no error is an empty directory. The new unit tests (tests/unit.c:28414-28480) stage only READDIR failures, so they do not catch this.

    Verification: pre-existing. GetHandle then sets ret = WS_SFTP_STATUS_NOT_OK; (src/wolfsftp.c:8073) and returns it from CLEANUP (8116) without touching ssh->error. The LS GETHANDLE branch (8201-8209) sees an error that is not a retry code, clears the state and returns NULL. The new contract added in wolfssh/wolfsftp.h:304-306 says "NULL with no error is an empty directory".

Comment thread src/wolfsftp.c Resolved
Comment thread examples/sftpclient/sftpclient.c Resolved
Comment thread src/wolfsftp.c Resolved

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #1406

Scan targets checked: wolfssh-src, wolfssh-bugs
Coverage: 4 of 6 in-scope changed file(s) opened by the reviewer; not opened: wolfssh/internal.h, wolfssh/wolfsftp.h

Findings: 2
2 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Review tier: Max

Comment thread src/wolfsftp.c Resolved
Comment thread src/wolfsftp.c Resolved
doCmds() picks a command only when its keyword is the first word of
the line, followed by a blank or the end of the line. A keyword inside
a file name or inside another command no longer selects that command,
so "rm getlog" removes getlog instead of fetching og.

- sftpCmdArgs() returns the argument text, skipping blanks around the
  keyword; every command uses it, creat included
- lcd and lls report "not supported" where they are compiled out
- tests: rm of getlog, cdrom and tmkdir must remove the files; pwd
  and ls ended by a CR

Issue: F-14766, F-16749
The mkdir, get, put, cd, rmdir, rm and rename branches of doCmds()
free the heap-built remote path (and rename's target) before every
early return, as creat and chmod already do. The leak mattered when
the client runs in-process under tests/sftp.c.

Issue: F-16752
clean_path() treats ".." as the parent only when it is a complete path
component, followed by '/' or the end of the path. A directory such as
"..dots" is kept, so cd into it no longer moves up a level.

- tests: cd into ..dots and check pwd

Issue: F-11666
wolfSSH_SFTP_LS() ends a listing only on SSH_FX_EOF or an empty NAME,
and every step that fails leaves an error set, so NULL with no error is
an empty directory. A READDIR failure still closes the handle, then
frees the entries read so far.

- wolfSSH_SFTP_DoName() records how a NAME request ended and sets an
  error on every failure, STATUS(OK) included: it carries no names
- SendPacketType() sets the error when its buffer allocation fails
- sftpclient ls prints an error when the listing fails
- tests: each LS outcome from OPENDIR to CLOSE, and each allocation in
  LS failing in turn; ls of a removed working directory

Issue: F-18849

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #1406

Scan targets checked: wolfssh-src, wolfssh-bugs
Coverage: 2 of 5 in-scope changed file(s) opened by the reviewer; not opened: tests/sftp.c, tests/unit.c, wolfssh/internal.h

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Lite

@wolfSSL-Fenrir-bot
wolfSSL-Fenrir-bot dismissed their stale review December 4, 2026 01:06

Fenrir's latest completed scan found no issues; clearing the prior automated change request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants