Skip to content

Commit 0c1fe55

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 724276c commit 0c1fe55

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
@@ -2362,9 +2362,9 @@ int wolfSSH_SFTP_RecvOpen(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
23622362

23632363
if (ret == WS_SUCCESS) {
23642364
/* Generate unique file handle ID and add to tracking list */
2365-
id[0] = ssh->fileIdCount[0];
2366-
id[1] = ssh->fileIdCount[1];
2367-
AddAssign64(ssh->fileIdCount, 1);
2365+
id[0] = ssh->handleIdCount[0];
2366+
id[1] = ssh->handleIdCount[1];
2367+
AddAssign64(ssh->handleIdCount, 1);
23682368

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

25472547
if (ret == WS_SUCCESS) {
25482548
/* Generate unique file handle ID and add to tracking list */
2549-
id[0] = ssh->fileIdCount[0];
2550-
id[1] = ssh->fileIdCount[1];
2551-
AddAssign64(ssh->fileIdCount, 1);
2549+
id[0] = ssh->handleIdCount[0];
2550+
id[1] = ssh->handleIdCount[1];
2551+
AddAssign64(ssh->handleIdCount, 1);
25522552

25532553
if (SFTP_AddFileHandle(ssh, fileHandle, dir, id) != WS_SUCCESS) {
25542554
WLOG(WS_LOG_SFTP, "Unable to store handle");
@@ -2718,11 +2718,11 @@ int wolfSSH_SFTP_RecvOpenDir(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
27182718
#else
27192719
cur->dir = ctx;
27202720
#endif
2721-
cur->id[0] = id[0] = ssh->dirIdCount[0];
2722-
cur->id[1] = id[1] = ssh->dirIdCount[1];
2721+
cur->id[0] = id[0] = ssh->handleIdCount[0];
2722+
cur->id[1] = id[1] = ssh->handleIdCount[1];
27232723
c32toa(id[0], idFlat);
27242724
c32toa(id[1], idFlat + UINT32_SZ);
2725-
AddAssign64(ssh->dirIdCount, 1);
2725+
AddAssign64(ssh->handleIdCount, 1);
27262726
cur->isEof = 0;
27272727
cur->next = ssh->dirList;
27282728
ssh->dirList = cur;
@@ -2877,11 +2877,11 @@ int wolfSSH_SFTP_RecvOpenDir(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
28772877
return WS_MEMORY_E;
28782878
}
28792879
cur->dir = INVALID_HANDLE_VALUE;
2880-
cur->id[0] = id[0] = ssh->dirIdCount[0];
2881-
cur->id[1] = id[1] = ssh->dirIdCount[1];
2880+
cur->id[0] = id[0] = ssh->handleIdCount[0];
2881+
cur->id[1] = id[1] = ssh->handleIdCount[1];
28822882
c32toa(id[0], idFlat);
28832883
c32toa(id[1], idFlat + UINT32_SZ);
2884-
AddAssign64(ssh->dirIdCount, 1);
2884+
AddAssign64(ssh->handleIdCount, 1);
28852885
cur->isEof = 0;
28862886
cur->dirName = dirName; /* take over ownership of buffer */
28872887
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)