ftp/sftp: strdup data info memory

Fix the broken implementation to have `data->state` carry pointers into
connectdata members. Always dup the memory and free when easy handle
closes.

Closes #16733
This commit is contained in:
Stefan Eissing 2025-03-15 11:20:15 +01:00 committed by Daniel Stenberg
parent d12129dda5
commit ebce3f0c02
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
4 changed files with 18 additions and 15 deletions

View file

@ -2906,7 +2906,10 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
ftpc->entrypath = dir; /* remember this */
infof(data, "Entry path is '%s'", ftpc->entrypath);
/* also save it where getinfo can access it: */
data->state.most_recent_ftp_entrypath = ftpc->entrypath;
free(data->state.most_recent_ftp_entrypath);
data->state.most_recent_ftp_entrypath = strdup(ftpc->entrypath);
if(!data->state.most_recent_ftp_entrypath)
return CURLE_OUT_OF_MEMORY;
ftp_state(data, FTP_SYST);
break;
}
@ -2915,7 +2918,10 @@ static CURLcode ftp_statemachine(struct Curl_easy *data,
ftpc->entrypath = dir; /* remember this */
infof(data, "Entry path is '%s'", ftpc->entrypath);
/* also save it where getinfo can access it: */
data->state.most_recent_ftp_entrypath = ftpc->entrypath;
free(data->state.most_recent_ftp_entrypath);
data->state.most_recent_ftp_entrypath = strdup(ftpc->entrypath);
if(!data->state.most_recent_ftp_entrypath)
return CURLE_OUT_OF_MEMORY;
}
else {
/* could not get the path */
@ -4095,16 +4101,10 @@ static CURLcode ftp_disconnect(struct Curl_easy *data,
/* The FTP session may or may not have been allocated/setup at this point! */
(void)ftp_quit(data, conn); /* ignore errors on the QUIT */
if(ftpc->entrypath) {
if(data->state.most_recent_ftp_entrypath == ftpc->entrypath) {
data->state.most_recent_ftp_entrypath = NULL;
}
Curl_safefree(ftpc->entrypath);
}
freedirs(ftpc);
Curl_safefree(ftpc->account);
Curl_safefree(ftpc->alternative_to_user);
Curl_safefree(ftpc->entrypath);
Curl_safefree(ftpc->prevpath);
Curl_safefree(ftpc->server_os);
Curl_pp_disconnect(pp);