Skip to content

Commit 9c1f289

Browse files
use dynamic buffer for SCP base path
1 parent 86499a5 commit 9c1f289

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

src/internal.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1163,11 +1163,11 @@ void SshResourceFree(WOLFSSH* ssh, void* heap)
11631163
ssh->scpRecvMsg = NULL;
11641164
ssh->scpRecvMsgSz = 0;
11651165
}
1166-
#ifdef WOLFSSL_NUCLEUS
1167-
WFREE(ssh->scpBasePathDynamic, heap, DYNTYPE_BUFFER);
1168-
ssh->scpBasePathDynamic = NULL;
1169-
ssh->scpBasePathSz = 0;
1170-
#endif
1166+
if (ssh->scpBasePathDynamic) {
1167+
WFREE(ssh->scpBasePathDynamic, heap, DYNTYPE_BUFFER);
1168+
ssh->scpBasePathDynamic = NULL;
1169+
ssh->scpBasePathSz = 0;
1170+
}
11711171
#endif
11721172
#ifdef WOLFSSH_SFTP
11731173
if (ssh->sftpDefaultPath) {

src/wolfscp.c

+15-16
Original file line numberDiff line numberDiff line change
@@ -1260,47 +1260,46 @@ int ParseScpCommand(WOLFSSH* ssh)
12601260

12611261
case 't':
12621262
ssh->scpDirection = WOLFSSH_SCP_TO;
1263-
#ifdef WOLFSSL_NUCLEUS
12641263
ssh->scpBasePathSz = cmdSz + WOLFSSH_MAX_FILENAME;
12651264
ssh->scpBasePathDynamic = (char*)WMALLOC(
12661265
ssh->scpBasePathSz,
12671266
ssh->ctx->heap, DYNTYPE_BUFFER);
1268-
#endif
1267+
if (ssh->scpBasePathDynamic == NULL) {
1268+
return WS_MEMORY_E;
1269+
}
1270+
WMEMSET(ssh->scpBasePathDynamic, 0, ssh->scpBasePathSz);
12691271
if (idx + 2 < cmdSz) {
12701272
/* skip space */
12711273
idx += 2;
1272-
#ifdef WOLFSSL_NUCLEUS
12731274
ssh->scpBasePath = ssh->scpBasePathDynamic;
1274-
WMEMCPY(ssh->scpBasePathDynamic, cmd + idx, cmdSz);
1275-
#else
1276-
ssh->scpBasePath = cmd + idx;
1277-
#endif
1275+
WMEMCPY(ssh->scpBasePathDynamic, cmd + idx,
1276+
cmdSz - idx);
12781277
ret = ParseBasePathHelper(ssh, cmdSz);
12791278
if (ret == WS_SUCCESS &&
1280-
wolfSSH_CleanPath(ssh, (char*)ssh->scpBasePath) < 0)
1279+
wolfSSH_CleanPath(ssh,
1280+
ssh->scpBasePathDynamic) < 0)
12811281
ret = WS_FATAL_ERROR;
12821282
}
12831283
break;
12841284

12851285
case 'f':
12861286
ssh->scpDirection = WOLFSSH_SCP_FROM;
1287-
#ifdef WOLFSSL_NUCLEUS
12881287
ssh->scpBasePathSz = cmdSz + WOLFSSH_MAX_FILENAME;
12891288
ssh->scpBasePathDynamic = (char*)WMALLOC(
12901289
ssh->scpBasePathSz,
12911290
ssh->ctx->heap, DYNTYPE_BUFFER);
1292-
#endif
1291+
if (ssh->scpBasePathDynamic == NULL) {
1292+
return WS_MEMORY_E;
1293+
}
1294+
WMEMSET(ssh->scpBasePathDynamic, 0, ssh->scpBasePathSz);
12931295
if (idx + 2 < cmdSz) {
12941296
/* skip space */
12951297
idx += 2;
1296-
#ifdef WOLFSSL_NUCLEUS
12971298
ssh->scpBasePath = ssh->scpBasePathDynamic;
1298-
WMEMCPY(ssh->scpBasePathDynamic, cmd + idx, cmdSz);
1299-
#else
1300-
ssh->scpBasePath = cmd + idx;
1301-
#endif
1299+
WMEMCPY(ssh->scpBasePathDynamic, cmd + idx,
1300+
cmdSz - idx);
13021301
if (wolfSSH_CleanPath(ssh,
1303-
(char*)ssh->scpBasePath) < 0)
1302+
ssh->scpBasePathDynamic) < 0)
13041303
ret = WS_FATAL_ERROR;
13051304
}
13061305
break;

wolfssh/internal.h

-2
Original file line numberDiff line numberDiff line change
@@ -721,11 +721,9 @@ struct WOLFSSH {
721721
char* scpRecvMsg; /* reading up to newline delimiter */
722722
int scpRecvMsgSz; /* current size of scp recv message */
723723
const char* scpBasePath; /* base path, ptr into channelList->command */
724-
#ifdef WOLFSSL_NUCLEUS
725724
/* alter base path instead of using chdir */
726725
char* scpBasePathDynamic; /* dynamic base path */
727726
word32 scpBasePathSz;
728-
#endif
729727
byte scpIsRecursive; /* recursive transfer requested */
730728
byte scpRequestType; /* directory or single file */
731729
byte scpMsgType;

0 commit comments

Comments
 (0)