From 083b4ab6e46e157d209764a60e60913426aae7b7 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Sun, 13 Oct 2024 23:50:11 +0200 Subject: [PATCH] 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 --- lib/vssh/curl_path.c | 2 +- lib/vssh/curl_path.h | 13 ------------- lib/vssh/libssh2.c | 39 +++++++-------------------------------- lib/vssh/ssh.h | 8 ++++++-- 4 files changed, 14 insertions(+), 48 deletions(-) diff --git a/lib/vssh/curl_path.c b/lib/vssh/curl_path.c index 144f8803d3..61452a4252 100644 --- a/lib/vssh/curl_path.c +++ b/lib/vssh/curl_path.c @@ -26,9 +26,9 @@ #if defined(USE_SSH) +#include "curl_path.h" #include #include "curl_memory.h" -#include "curl_path.h" #include "escape.h" #include "memdebug.h" diff --git a/lib/vssh/curl_path.h b/lib/vssh/curl_path.h index 6fdb2fddff..8e984174d7 100644 --- a/lib/vssh/curl_path.h +++ b/lib/vssh/curl_path.h @@ -28,19 +28,6 @@ #include #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); diff --git a/lib/vssh/libssh2.c b/lib/vssh/libssh2.c index bf76f77a80..505adc4456 100644 --- a/lib/vssh/libssh2.c +++ b/lib/vssh/libssh2.c @@ -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)) diff --git a/lib/vssh/ssh.h b/lib/vssh/ssh.h index 2ed78649b9..f2d42a3de2 100644 --- a/lib/vssh/ssh.h +++ b/lib/vssh/ssh.h @@ -39,6 +39,8 @@ #include #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 */ -- 2.47.3