1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-24 00:58:48 -05:00

ftplistparser: keep state between invokes

Fixes FTP wildcard parsing when doing over a number of read buffers.

Regression from f786d1f14

Reported-by: wncboy on github
Fixes #2445
Closes #2519
This commit is contained in:
Daniel Stenberg 2018-04-19 15:47:05 +02:00
parent 98a768f0a6
commit abbc8457d8
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
5 changed files with 43 additions and 32 deletions

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 2010 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 2010 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -33,14 +33,11 @@ struct fileinfo *Curl_fileinfo_alloc(void)
return calloc(1, sizeof(struct fileinfo)); return calloc(1, sizeof(struct fileinfo));
} }
void Curl_fileinfo_dtor(void *user, void *element) void Curl_fileinfo_cleanup(struct fileinfo *finfo)
{ {
struct fileinfo *finfo = element;
(void) user;
if(!finfo) if(!finfo)
return; return;
Curl_safefree(finfo->info.b_data); Curl_safefree(finfo->info.b_data);
free(finfo); free(finfo);
} }

View File

@ -7,7 +7,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 2010, 2017, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 2010 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -31,7 +31,6 @@ struct fileinfo {
}; };
struct fileinfo *Curl_fileinfo_alloc(void); struct fileinfo *Curl_fileinfo_alloc(void);
void Curl_fileinfo_cleanup(struct fileinfo *finfo);
void Curl_fileinfo_dtor(void *, void *);
#endif /* HEADER_CURL_FILEINFO_H */ #endif /* HEADER_CURL_FILEINFO_H */

View File

@ -3699,7 +3699,7 @@ static CURLcode init_wc_data(struct connectdata *conn)
char *path = conn->data->state.path; char *path = conn->data->state.path;
struct WildcardData *wildcard = &(conn->data->wildcard); struct WildcardData *wildcard = &(conn->data->wildcard);
CURLcode result = CURLE_OK; CURLcode result = CURLE_OK;
struct ftp_wc *ftpwc; struct ftp_wc *ftpwc = NULL;
last_slash = strrchr(conn->data->state.path, '/'); last_slash = strrchr(conn->data->state.path, '/');
if(last_slash) { if(last_slash) {
@ -3734,16 +3734,15 @@ static CURLcode init_wc_data(struct connectdata *conn)
/* allocate ftp protocol specific wildcard data */ /* allocate ftp protocol specific wildcard data */
ftpwc = calloc(1, sizeof(struct ftp_wc)); ftpwc = calloc(1, sizeof(struct ftp_wc));
if(!ftpwc) { if(!ftpwc) {
Curl_safefree(wildcard->pattern); result = CURLE_OUT_OF_MEMORY;
return CURLE_OUT_OF_MEMORY; goto fail;
} }
/* INITIALIZE parselist structure */ /* INITIALIZE parselist structure */
ftpwc->parser = Curl_ftp_parselist_data_alloc(); ftpwc->parser = Curl_ftp_parselist_data_alloc();
if(!ftpwc->parser) { if(!ftpwc->parser) {
Curl_safefree(wildcard->pattern); result = CURLE_OUT_OF_MEMORY;
free(ftpwc); goto fail;
return CURLE_OUT_OF_MEMORY;
} }
wildcard->protdata = ftpwc; /* put it to the WildcardData tmp pointer */ wildcard->protdata = ftpwc; /* put it to the WildcardData tmp pointer */
@ -3756,20 +3755,13 @@ static CURLcode init_wc_data(struct connectdata *conn)
/* try to parse ftp url */ /* try to parse ftp url */
result = ftp_parse_url_path(conn); result = ftp_parse_url_path(conn);
if(result) { if(result) {
Curl_safefree(wildcard->pattern); goto fail;
wildcard->dtor(wildcard->protdata);
wildcard->dtor = ZERO_NULL;
wildcard->protdata = NULL;
return result;
} }
wildcard->path = strdup(conn->data->state.path); wildcard->path = strdup(conn->data->state.path);
if(!wildcard->path) { if(!wildcard->path) {
Curl_safefree(wildcard->pattern); result = CURLE_OUT_OF_MEMORY;
wildcard->dtor(wildcard->protdata); goto fail;
wildcard->dtor = ZERO_NULL;
wildcard->protdata = NULL;
return CURLE_OUT_OF_MEMORY;
} }
/* backup old write_function */ /* backup old write_function */
@ -3783,6 +3775,17 @@ static CURLcode init_wc_data(struct connectdata *conn)
infof(conn->data, "Wildcard - Parsing started\n"); infof(conn->data, "Wildcard - Parsing started\n");
return CURLE_OK; return CURLE_OK;
fail:
if(ftpwc) {
Curl_ftp_parselist_data_free(&ftpwc->parser);
free(ftpwc);
}
Curl_safefree(wildcard->pattern);
wildcard->dtor(wildcard->protdata);
wildcard->dtor = ZERO_NULL;
wildcard->protdata = NULL;
return result;
} }
/* This is called recursively */ /* This is called recursively */
@ -3903,6 +3906,8 @@ static CURLcode wc_statemach(struct connectdata *conn)
case CURLWC_DONE: case CURLWC_DONE:
case CURLWC_ERROR: case CURLWC_ERROR:
case CURLWC_CLEAR: case CURLWC_CLEAR:
if(wildcard->dtor)
wildcard->dtor(wildcard->protdata);
break; break;
} }

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -185,10 +185,13 @@ struct ftp_parselist_data *Curl_ftp_parselist_data_alloc(void)
} }
void Curl_ftp_parselist_data_free(struct ftp_parselist_data **pl_data) void Curl_ftp_parselist_data_free(struct ftp_parselist_data **parserp)
{ {
free(*pl_data); struct ftp_parselist_data *parser = *parserp;
*pl_data = NULL; if(parser)
Curl_fileinfo_cleanup(parser->file_data);
free(parser);
*parserp = NULL;
} }
@ -313,7 +316,7 @@ static CURLcode ftp_pl_insert_finfo(struct connectdata *conn,
Curl_llist_insert_next(llist, llist->tail, finfo, &infop->list); Curl_llist_insert_next(llist, llist->tail, finfo, &infop->list);
} }
else { else {
Curl_fileinfo_dtor(NULL, finfo); Curl_fileinfo_cleanup(infop);
} }
ftpwc->parser->file_data = NULL; ftpwc->parser->file_data = NULL;
@ -381,7 +384,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb,
finfo->b_data = tmp; finfo->b_data = tmp;
} }
else { else {
Curl_fileinfo_dtor(NULL, parser->file_data); Curl_fileinfo_cleanup(parser->file_data);
parser->file_data = NULL; parser->file_data = NULL;
parser->error = CURLE_OUT_OF_MEMORY; parser->error = CURLE_OUT_OF_MEMORY;
goto fail; goto fail;
@ -1003,12 +1006,13 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb,
i++; i++;
} }
return retsize;
fail: fail:
/* Clean up any allocated memory. */ /* Clean up any allocated memory. */
if(parser->file_data) { if(parser->file_data) {
Curl_fileinfo_dtor(NULL, parser->file_data); Curl_fileinfo_cleanup(parser->file_data);
parser->file_data = NULL; parser->file_data = NULL;
} }

View File

@ -30,9 +30,15 @@
#include "curl_memory.h" #include "curl_memory.h"
#include "memdebug.h" #include "memdebug.h"
static void fileinfo_dtor(void *user, void *element)
{
(void)user;
Curl_fileinfo_cleanup(element);
}
CURLcode Curl_wildcard_init(struct WildcardData *wc) CURLcode Curl_wildcard_init(struct WildcardData *wc)
{ {
Curl_llist_init(&wc->filelist, Curl_fileinfo_dtor); Curl_llist_init(&wc->filelist, fileinfo_dtor);
wc->state = CURLWC_INIT; wc->state = CURLWC_INIT;
return CURLE_OK; return CURLE_OK;