check return value of WFTELL()

pull/1157/head
Ruby Martin 2026-08-06 15:14:56 -06:00 committed by John Safranek
parent 97bbe42263
commit ab8058d7cd
4 changed files with 52 additions and 6 deletions

View File

@ -1730,6 +1730,7 @@ static int load_file(const char* fileName, byte* buf, word32* bufSz)
WFILE* file;
word32 fileSz;
word32 readSz;
long tmpSz;
if (fileName == NULL) return 0;
@ -1740,7 +1741,12 @@ static int load_file(const char* fileName, byte* buf, word32* bufSz)
return 0;
}
fileSz = (word32)WFTELL(NULL, file);
tmpSz = WFTELL(NULL, file);
if (tmpSz < 0) {
WFCLOSE(NULL, file);
return 0;
}
fileSz = (word32)tmpSz;
WREWIND(NULL, file);
if (buf == NULL || fileSz > *bufSz) {
@ -2565,6 +2571,11 @@ static char* LoadTpmSshKey(const char* keyFile, const char* username)
return NULL;
}
length = WFTELL(NULL, file);
if (length < 0) {
fprintf(stderr, "TPM key file tell failed\n");
WFCLOSE(NULL, file);
return NULL;
}
WREWIND(NULL, file);
usernameLen = WSTRLEN(username);

View File

@ -1660,6 +1660,7 @@ static int load_file(const char* fileName, byte* buf, word32* bufSz)
WFILE* file;
word32 fileSz;
word32 readSz;
long tmpSz;
if (fileName == NULL) return 0;
@ -1669,7 +1670,13 @@ static int load_file(const char* fileName, byte* buf, word32* bufSz)
WFCLOSE(NULL, file);
return 0;
}
fileSz = (word32)WFTELL(NULL, file);
tmpSz = WFTELL(NULL, file);
if (tmpSz < 0) {
WFCLOSE(NULL, file);
return 0;
}
fileSz = (word32)tmpSz;
WREWIND(NULL, file);
if (buf == NULL || fileSz > *bufSz) {
@ -2182,6 +2189,11 @@ static char* LoadTpmSshKey(const char* keyFile, const char* username)
return NULL;
}
length = WFTELL(NULL, file);
if (length < 0) {
fprintf(stderr, "TPM key file tell failed\n");
WFCLOSE(NULL, file);
return NULL;
}
WREWIND(NULL, file);
usernameLen = WSTRLEN(username);

View File

@ -2685,6 +2685,8 @@ int wsScpRecvCallback(WOLFSSH* ssh, int state, const char* basePath,
static int _GetFileSize(void* fs, WFILE* fp, word32* fileSz)
{
long tmpSz;
WOLFSSH_UNUSED(fs);
if (fp == NULL || fileSz == NULL)
@ -2692,7 +2694,11 @@ static int _GetFileSize(void* fs, WFILE* fp, word32* fileSz)
/* get file size */
if (WFSEEK_SUCCESS(WFSEEK(fs, fp, 0, WSEEK_END))) {
*fileSz = (word32)WFTELL(fs, fp);
tmpSz = WFTELL(fs, fp);
if (tmpSz < 0) {
return WS_BAD_FILE_E;
}
*fileSz = (word32)tmpSz;
WREWIND(fs, fp);
return WS_SUCCESS;
@ -3183,8 +3189,14 @@ static int ScpProcessEntry(WOLFSSH* ssh, char* fileName, word64* mTime,
if (ret == WS_SUCCESS) {
ret = _GetFileSize(ssh->fs, sendCtx->fp, totalFileSz);
if (ret == WS_SUCCESS)
if (ret != WS_SUCCESS) {
WLOG(WS_LOG_ERROR, "scp: unable to get file size, abort");
wolfSSH_SetScpErrorMsg(ssh, "unable to get file size");
ret = WS_SCP_ABORT;
}
else {
ret = (word32)WFREAD(ssh->fs, buf, 1, bufSz, sendCtx->fp);
}
}
/* keep fp open if no errors and transfer will continue */
@ -3357,8 +3369,13 @@ int wsScpSendCallback(WOLFSSH* ssh, int state, const char* peerRequest,
#endif
}
if (ret == WS_SUCCESS)
if (ret == WS_SUCCESS) {
ret = _GetFileSize(ssh->fs, sendCtx->fp, totalFileSz);
if (ret != WS_SUCCESS) {
WLOG(WS_LOG_ERROR, "scp: unable to get file size, abort");
wolfSSH_SetScpErrorMsg(ssh, "unable to get file size");
}
}
if (ret == WS_SUCCESS)
ret = GetFileStats(ssh->fs, sendCtx, peerRequest, mTime, aTime, fileMode);

View File

@ -177,6 +177,7 @@ static int load_file(const char* fileName, byte* buf, word32* bufSz)
WFILE* file;
word32 fileSz;
word32 readSz;
long tmpSz;
if (fileName == NULL) return 0;
@ -186,7 +187,12 @@ static int load_file(const char* fileName, byte* buf, word32* bufSz)
WFCLOSE(NULL, file);
return 0;
}
fileSz = (word32)WFTELL(NULL, file);
tmpSz = WFTELL(NULL, file);
if (tmpSz < 0) {
WFCLOSE(NULL, file);
return 0;
}
fileSz = (word32)tmpSz;
WREWIND(NULL, file);
if (buf == NULL || fileSz > *bufSz) {