Skip to content

Commit 62b18bd

Browse files
committed
Share one ID counter for SFTP handles
- Merge fileIdCount and dirIdCount into a single handleIdCount. - File and directory handle IDs now share one namespace. - A close or other handle op cannot match the wrong resource type.
1 parent af4f84f commit 62b18bd

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

src/wolfsftp.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,9 +2357,9 @@ int wolfSSH_SFTP_RecvOpen(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
23572357

23582358
if (ret == WS_SUCCESS) {
23592359
/* Generate unique file handle ID and add to tracking list */
2360-
id[0] = ssh->fileIdCount[0];
2361-
id[1] = ssh->fileIdCount[1];
2362-
AddAssign64(ssh->fileIdCount, 1);
2360+
id[0] = ssh->handleIdCount[0];
2361+
id[1] = ssh->handleIdCount[1];
2362+
AddAssign64(ssh->handleIdCount, 1);
23632363

23642364
if ((ret = SFTP_AddFileHandle(ssh, fd, dir, id)) != WS_SUCCESS) {
23652365
WLOG(WS_LOG_SFTP, "Unable to store handle");
@@ -2541,9 +2541,9 @@ int wolfSSH_SFTP_RecvOpen(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
25412541

25422542
if (ret == WS_SUCCESS) {
25432543
/* Generate unique file handle ID and add to tracking list */
2544-
id[0] = ssh->fileIdCount[0];
2545-
id[1] = ssh->fileIdCount[1];
2546-
AddAssign64(ssh->fileIdCount, 1);
2544+
id[0] = ssh->handleIdCount[0];
2545+
id[1] = ssh->handleIdCount[1];
2546+
AddAssign64(ssh->handleIdCount, 1);
25472547

25482548
if (SFTP_AddFileHandle(ssh, fileHandle, dir, id) != WS_SUCCESS) {
25492549
WLOG(WS_LOG_SFTP, "Unable to store handle");
@@ -2713,11 +2713,11 @@ int wolfSSH_SFTP_RecvOpenDir(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
27132713
#else
27142714
cur->dir = ctx;
27152715
#endif
2716-
cur->id[0] = id[0] = ssh->dirIdCount[0];
2717-
cur->id[1] = id[1] = ssh->dirIdCount[1];
2716+
cur->id[0] = id[0] = ssh->handleIdCount[0];
2717+
cur->id[1] = id[1] = ssh->handleIdCount[1];
27182718
c32toa(id[0], idFlat);
27192719
c32toa(id[1], idFlat + UINT32_SZ);
2720-
AddAssign64(ssh->dirIdCount, 1);
2720+
AddAssign64(ssh->handleIdCount, 1);
27212721
cur->isEof = 0;
27222722
cur->next = ssh->dirList;
27232723
ssh->dirList = cur;
@@ -2872,11 +2872,11 @@ int wolfSSH_SFTP_RecvOpenDir(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
28722872
return WS_MEMORY_E;
28732873
}
28742874
cur->dir = INVALID_HANDLE_VALUE;
2875-
cur->id[0] = id[0] = ssh->dirIdCount[0];
2876-
cur->id[1] = id[1] = ssh->dirIdCount[1];
2875+
cur->id[0] = id[0] = ssh->handleIdCount[0];
2876+
cur->id[1] = id[1] = ssh->handleIdCount[1];
28772877
c32toa(id[0], idFlat);
28782878
c32toa(id[1], idFlat + UINT32_SZ);
2879-
AddAssign64(ssh->dirIdCount, 1);
2879+
AddAssign64(ssh->handleIdCount, 1);
28802880
cur->isEof = 0;
28812881
cur->dirName = dirName; /* take over ownership of buffer */
28822882
cur->next = ssh->dirList;

wolfssh/internal.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -920,9 +920,11 @@ struct WOLFSSH {
920920
char* sftpDefaultPath;
921921
#ifndef NO_WOLFSSH_DIR
922922
WS_DIR_LIST* dirList;
923-
word32 dirIdCount[2];
924923
#endif
925-
word32 fileIdCount[2];
924+
/* Shared counter for both file and directory handle IDs. A single
925+
* namespace guarantees IDs are unique across files and directories so a
926+
* close request cannot match the wrong resource type. */
927+
word32 handleIdCount[2];
926928
WS_FILE_LIST* fileList;
927929
struct WS_SFTP_RECV_INIT_STATE* recvInitState;
928930
struct WS_SFTP_RECV_STATE* recvState;

0 commit comments

Comments
 (0)