The FTP root directory string is now copied so that the parameter doesn't have to live during the whole life of the program

This commit is contained in:
anschrammh 2022-04-29 07:48:07 +02:00
parent 714b167aef
commit 95ae6927e8

View File

@ -27,7 +27,7 @@ class FTPServer : public TCPServer<T>
{ {
if (login != NULL) if (login != NULL)
{ {
if (strlen(login) > 0) if (strlen(login))
{ {
_login = (char *)malloc((sizeof(char) * strlen(login)) + 1); _login = (char *)malloc((sizeof(char) * strlen(login)) + 1);
strcpy(_login, login); strcpy(_login, login);
@ -36,7 +36,7 @@ class FTPServer : public TCPServer<T>
if (password != NULL) if (password != NULL)
{ {
if (strlen(password) > 0) if (strlen(password))
{ {
_password = (char *)malloc((sizeof(char) * strlen(password)) + 1); _password = (char *)malloc((sizeof(char) * strlen(password)) + 1);
strcpy(_password, password); strcpy(_password, password);
@ -53,7 +53,7 @@ class FTPServer : public TCPServer<T>
virtual ~FTPServer() virtual ~FTPServer()
{ {
_dataServer.stop(); _dataServer.stop();
free(_login); free(_password); free(_login); free(_password); free(_FTPDir);
} }
virtual void stop() virtual void stop()
@ -67,7 +67,12 @@ class FTPServer : public TCPServer<T>
virtual void setFTPDir(const char *FTPDir) virtual void setFTPDir(const char *FTPDir)
{ {
_FTPDir = FTPDir; if(FTPDir)
{
free(_FTPDir);
_FTPDir = (char *)malloc((strlen(FTPDir) * sizeof(char)) + 1);
strcpy(_FTPDir, FTPDir);
}
} }
protected: protected:
@ -1028,7 +1033,7 @@ class FTPServer : public TCPServer<T>
char *_login; char *_login;
char *_password; char *_password;
const char *_FTPDir = NULL; //Pointer to constant string and char * const is a constant pointer to string char *_FTPDir = nullptr;
uint16_t _dataPort; uint16_t _dataPort;
WiFiServer _dataServer; //In passive mode, the FTP server opens two different ports (one for the commands and the other for the data stream) WiFiServer _dataServer; //In passive mode, the FTP server opens two different ports (one for the commands and the other for the data stream)