libssh2: put the readdir buffers into struct

... instead of separate malloc() calls:

- removes two mallocs (and associated error handling paths)
- makes cleanup easier

Also reduce maximum SFTP file path lengths to 1024 bytes universally
everywhere. Using the system's own MAX_PATH did not make sense since
this is mostly about getting a remote file name.

Closes #15285
This commit is contained in:
Daniel Stenberg 2024-10-13 23:50:11 +02:00
parent 1cf187a4f6
commit 083b4ab6e4
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
4 changed files with 14 additions and 48 deletions

View File

@ -26,9 +26,9 @@
#if defined(USE_SSH)
#include "curl_path.h"
#include <curl/curl.h>
#include "curl_memory.h"
#include "curl_path.h"
#include "escape.h"
#include "memdebug.h"

View File

@ -28,19 +28,6 @@
#include <curl/curl.h>
#include "urldata.h"
#ifdef _WIN32
# undef PATH_MAX
# define PATH_MAX MAX_PATH
# ifndef R_OK
# define R_OK 4
# endif
#endif
#ifndef PATH_MAX
#define PATH_MAX 1024 /* just an extra precaution since there are systems that
have their definition hidden well */
#endif
CURLcode Curl_getworkingpath(struct Curl_easy *data,
char *homedir,
char **path);

View File

@ -1628,10 +1628,8 @@ static CURLcode sftp_readdir(struct Curl_easy *data,
{
CURLcode result = CURLE_OK;
int rc = libssh2_sftp_readdir_ex(sshc->sftp_handle,
sshp->readdir_filename,
PATH_MAX,
sshp->readdir_longentry,
PATH_MAX,
sshp->readdir_filename, CURL_PATH_MAX,
sshp->readdir_longentry, CURL_PATH_MAX,
&sshp->readdir_attrs);
if(rc == LIBSSH2_ERROR_EAGAIN) {
*blockp = TRUE;
@ -1658,7 +1656,7 @@ static CURLcode sftp_readdir(struct Curl_easy *data,
if((sshp->readdir_attrs.flags & LIBSSH2_SFTP_ATTR_PERMISSIONS) &&
((sshp->readdir_attrs.permissions & LIBSSH2_SFTP_S_IFMT) ==
LIBSSH2_SFTP_S_IFLNK)) {
Curl_dyn_init(&sshp->readdir_link, PATH_MAX);
Curl_dyn_init(&sshp->readdir_link, CURL_PATH_MAX);
result = Curl_dyn_addf(&sshp->readdir_link, "%s%s", sshp->path,
sshp->readdir_filename);
state(data, SSH_SFTP_READDIR_LINK);
@ -1671,8 +1669,6 @@ static CURLcode sftp_readdir(struct Curl_easy *data,
}
}
else if(rc == 0) {
Curl_safefree(sshp->readdir_filename);
Curl_safefree(sshp->readdir_longentry);
state(data, SSH_SFTP_READDIR_DONE);
}
else if(rc < 0) {
@ -1682,8 +1678,6 @@ static CURLcode sftp_readdir(struct Curl_easy *data,
failf(data, "Could not open remote file for reading: %s :: %d",
sftp_libssh2_strerror(sftperr),
libssh2_session_last_errno(sshc->ssh_session));
Curl_safefree(sshp->readdir_filename);
Curl_safefree(sshp->readdir_longentry);
state(data, SSH_SFTP_CLOSE);
}
return result;
@ -2053,13 +2047,13 @@ static CURLcode ssh_statemachine(struct Curl_easy *data, bool *block)
case SSH_SFTP_REALPATH:
{
char tempHome[PATH_MAX];
char tempHome[CURL_PATH_MAX];
/*
* Get the "home" directory
*/
rc = sftp_libssh2_realpath(sshc->sftp_session, ".",
tempHome, PATH_MAX-1);
tempHome, CURL_PATH_MAX-1);
if(rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
@ -2490,20 +2484,7 @@ static CURLcode ssh_statemachine(struct Curl_easy *data, bool *block)
sshc->actualcode = result ? result : CURLE_SSH;
break;
}
sshp->readdir_filename = malloc(PATH_MAX + 1);
if(!sshp->readdir_filename) {
state(data, SSH_SFTP_CLOSE);
sshc->actualcode = CURLE_OUT_OF_MEMORY;
break;
}
sshp->readdir_longentry = malloc(PATH_MAX + 1);
if(!sshp->readdir_longentry) {
Curl_safefree(sshp->readdir_filename);
state(data, SSH_SFTP_CLOSE);
sshc->actualcode = CURLE_OUT_OF_MEMORY;
break;
}
Curl_dyn_init(&sshp->readdir, PATH_MAX * 2);
Curl_dyn_init(&sshp->readdir, CURL_PATH_MAX * 2);
state(data, SSH_SFTP_READDIR);
break;
@ -2523,7 +2504,7 @@ static CURLcode ssh_statemachine(struct Curl_easy *data, bool *block)
(unsigned int)
Curl_dyn_len(&sshp->readdir_link),
sshp->readdir_filename,
PATH_MAX, LIBSSH2_SFTP_READLINK);
CURL_PATH_MAX, LIBSSH2_SFTP_READLINK);
if(rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
@ -2533,8 +2514,6 @@ static CURLcode ssh_statemachine(struct Curl_easy *data, bool *block)
result = Curl_dyn_addf(&sshp->readdir, " -> %s", sshp->readdir_filename);
if(result) {
Curl_safefree(sshp->readdir_filename);
Curl_safefree(sshp->readdir_longentry);
state(data, SSH_SFTP_CLOSE);
sshc->actualcode = result;
break;
@ -2567,8 +2546,6 @@ static CURLcode ssh_statemachine(struct Curl_easy *data, bool *block)
break;
}
sshc->sftp_handle = NULL;
Curl_safefree(sshp->readdir_filename);
Curl_safefree(sshp->readdir_longentry);
/* no data to transfer */
Curl_xfer_setup_nop(data);
@ -3505,8 +3482,6 @@ static CURLcode ssh_done(struct Curl_easy *data, CURLcode status)
result = status;
Curl_safefree(sshp->path);
Curl_safefree(sshp->readdir_filename);
Curl_safefree(sshp->readdir_longentry);
Curl_dyn_free(&sshp->readdir);
if(Curl_pgrsDone(data))

View File

@ -39,6 +39,8 @@
#include <wolfssh/wolfsftp.h>
#endif
#include "curl_path.h"
/****************************************************************************
* SSH unique setup
***************************************************************************/
@ -109,6 +111,8 @@ typedef enum {
SSH_LAST /* never used */
} sshstate;
#define CURL_PATH_MAX 1024
/* this struct is used in the HandleData struct which is part of the
Curl_easy, which means this is used on a per-easy handle basis.
Everything that is strictly related to a connection is banned from this
@ -118,8 +122,8 @@ struct SSHPROTO {
#ifdef USE_LIBSSH2
struct dynbuf readdir_link;
struct dynbuf readdir;
char *readdir_filename;
char *readdir_longentry;
char readdir_filename[CURL_PATH_MAX + 1];
char readdir_longentry[CURL_PATH_MAX + 1];
LIBSSH2_SFTP_ATTRIBUTES quote_attrs; /* used by the SFTP_QUOTE state */