From 01114f6efda64b9ce3492ed7c7964359de70efe5 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 27 Mar 2023 10:48:44 +0200 Subject: [PATCH] ftplistparser: move out private data from public struct The public 'curl_fileinfo' struct contained three fields that are for internal purposes only. This change makes them unused in the public struct. The new private struct fields are also renamed to make this separation more obvious internally. Closes #10844 --- include/curl/curl.h | 3 +- lib/fileinfo.c | 2 +- lib/fileinfo.h | 3 ++ lib/ftplistparser.c | 94 ++++++++++++++++++++++----------------------- 4 files changed, 53 insertions(+), 49 deletions(-) diff --git a/include/curl/curl.h b/include/curl/curl.h index 63a13823e1..a4872bd472 100644 --- a/include/curl/curl.h +++ b/include/curl/curl.h @@ -331,7 +331,8 @@ struct curl_fileinfo { unsigned int flags; - /* used internally */ + /* These are libcurl private struct fields. Previously used by libcurl, so + they must never be interfered with. */ char *b_data; size_t b_size; size_t b_used; diff --git a/lib/fileinfo.c b/lib/fileinfo.c index 409b38fcbc..c590bc2c44 100644 --- a/lib/fileinfo.c +++ b/lib/fileinfo.c @@ -40,7 +40,7 @@ void Curl_fileinfo_cleanup(struct fileinfo *finfo) if(!finfo) return; - Curl_safefree(finfo->info.b_data); + Curl_safefree(finfo->mem); free(finfo); } #endif diff --git a/lib/fileinfo.h b/lib/fileinfo.h index af44212501..7c3e3162be 100644 --- a/lib/fileinfo.h +++ b/lib/fileinfo.h @@ -30,6 +30,9 @@ struct fileinfo { struct curl_fileinfo info; struct Curl_llist_element list; + char *mem; + size_t size; + size_t used; }; struct fileinfo *Curl_fileinfo_alloc(void); diff --git a/lib/ftplistparser.c b/lib/ftplistparser.c index 39001e3f65..703a63e51f 100644 --- a/lib/ftplistparser.c +++ b/lib/ftplistparser.c @@ -319,7 +319,7 @@ static CURLcode ftp_pl_insert_finfo(struct Curl_easy *data, struct curl_fileinfo *finfo = &infop->info; /* move finfo pointers to b_data */ - char *str = finfo->b_data; + char *str = infop->mem; finfo->filename = str + parser->offsets.filename; finfo->strings.group = parser->offsets.group ? str + parser->offsets.group : NULL; @@ -400,27 +400,27 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, parser->error = CURLE_OUT_OF_MEMORY; goto fail; } - parser->file_data->info.b_data = malloc(FTP_BUFFER_ALLOCSIZE); - if(!parser->file_data->info.b_data) { + parser->file_data->mem = malloc(FTP_BUFFER_ALLOCSIZE); + if(!parser->file_data->mem) { parser->error = CURLE_OUT_OF_MEMORY; goto fail; } - parser->file_data->info.b_size = FTP_BUFFER_ALLOCSIZE; + parser->file_data->size = FTP_BUFFER_ALLOCSIZE; parser->item_offset = 0; parser->item_length = 0; } infop = parser->file_data; finfo = &infop->info; - finfo->b_data[finfo->b_used++] = c; + infop->mem[infop->used++] = c; - if(finfo->b_used >= finfo->b_size - 1) { + if(infop->used >= infop->size - 1) { /* if it is important, extend buffer space for file data */ - char *tmp = realloc(finfo->b_data, - finfo->b_size + FTP_BUFFER_ALLOCSIZE); + char *tmp = realloc(infop->mem, + infop->size + FTP_BUFFER_ALLOCSIZE); if(tmp) { - finfo->b_size += FTP_BUFFER_ALLOCSIZE; - finfo->b_data = tmp; + infop->size += FTP_BUFFER_ALLOCSIZE; + infop->mem = tmp; } else { Curl_fileinfo_cleanup(parser->file_data); @@ -443,7 +443,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, else { parser->state.UNIX.main = PL_UNIX_FILETYPE; /* start FSM again not considering size of directory */ - finfo->b_used = 0; + infop->used = 0; continue; } break; @@ -451,12 +451,12 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, parser->item_length++; if(c == '\r') { parser->item_length--; - finfo->b_used--; + infop->used--; } else if(c == '\n') { - finfo->b_data[parser->item_length - 1] = 0; - if(strncmp("total ", finfo->b_data, 6) == 0) { - char *endptr = finfo->b_data + 6; + infop->mem[parser->item_length - 1] = 0; + if(strncmp("total ", infop->mem, 6) == 0) { + char *endptr = infop->mem + 6; /* here we can deal with directory size, pass the leading whitespace and then the digits */ while(ISBLANK(*endptr)) @@ -468,7 +468,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, goto fail; } parser->state.UNIX.main = PL_UNIX_FILETYPE; - finfo->b_used = 0; + infop->used = 0; } else { parser->error = CURLE_FTP_BAD_FILE_LIST; @@ -526,8 +526,8 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, parser->error = CURLE_FTP_BAD_FILE_LIST; goto fail; } - finfo->b_data[10] = 0; /* terminate permissions */ - perm = ftp_pl_get_permission(finfo->b_data + parser->item_offset); + infop->mem[10] = 0; /* terminate permissions */ + perm = ftp_pl_get_permission(infop->mem + parser->item_offset); if(perm & FTP_LP_MALFORMATED_PERM) { parser->error = CURLE_FTP_BAD_FILE_LIST; goto fail; @@ -546,7 +546,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, case PL_UNIX_HLINKS_PRESPACE: if(c != ' ') { if(c >= '0' && c <= '9') { - parser->item_offset = finfo->b_used - 1; + parser->item_offset = infop->used - 1; parser->item_length = 1; parser->state.UNIX.sub.hlinks = PL_UNIX_HLINKS_NUMBER; } @@ -561,8 +561,8 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, if(c == ' ') { char *p; long int hlinks; - finfo->b_data[parser->item_offset + parser->item_length - 1] = 0; - hlinks = strtol(finfo->b_data + parser->item_offset, &p, 10); + infop->mem[parser->item_offset + parser->item_length - 1] = 0; + hlinks = strtol(infop->mem + parser->item_offset, &p, 10); if(p[0] == '\0' && hlinks != LONG_MAX && hlinks != LONG_MIN) { parser->file_data->info.flags |= CURLFINFOFLAG_KNOWN_HLINKCOUNT; parser->file_data->info.hardlinks = hlinks; @@ -583,7 +583,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, switch(parser->state.UNIX.sub.user) { case PL_UNIX_USER_PRESPACE: if(c != ' ') { - parser->item_offset = finfo->b_used - 1; + parser->item_offset = infop->used - 1; parser->item_length = 1; parser->state.UNIX.sub.user = PL_UNIX_USER_PARSING; } @@ -591,7 +591,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, case PL_UNIX_USER_PARSING: parser->item_length++; if(c == ' ') { - finfo->b_data[parser->item_offset + parser->item_length - 1] = 0; + infop->mem[parser->item_offset + parser->item_length - 1] = 0; parser->offsets.user = parser->item_offset; parser->state.UNIX.main = PL_UNIX_GROUP; parser->state.UNIX.sub.group = PL_UNIX_GROUP_PRESPACE; @@ -605,7 +605,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, switch(parser->state.UNIX.sub.group) { case PL_UNIX_GROUP_PRESPACE: if(c != ' ') { - parser->item_offset = finfo->b_used - 1; + parser->item_offset = infop->used - 1; parser->item_length = 1; parser->state.UNIX.sub.group = PL_UNIX_GROUP_NAME; } @@ -613,7 +613,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, case PL_UNIX_GROUP_NAME: parser->item_length++; if(c == ' ') { - finfo->b_data[parser->item_offset + parser->item_length - 1] = 0; + infop->mem[parser->item_offset + parser->item_length - 1] = 0; parser->offsets.group = parser->item_offset; parser->state.UNIX.main = PL_UNIX_SIZE; parser->state.UNIX.sub.size = PL_UNIX_SIZE_PRESPACE; @@ -628,7 +628,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, case PL_UNIX_SIZE_PRESPACE: if(c != ' ') { if(c >= '0' && c <= '9') { - parser->item_offset = finfo->b_used - 1; + parser->item_offset = infop->used - 1; parser->item_length = 1; parser->state.UNIX.sub.size = PL_UNIX_SIZE_NUMBER; } @@ -643,8 +643,8 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, if(c == ' ') { char *p; curl_off_t fsize; - finfo->b_data[parser->item_offset + parser->item_length - 1] = 0; - if(!curlx_strtoofft(finfo->b_data + parser->item_offset, + infop->mem[parser->item_offset + parser->item_length - 1] = 0; + if(!curlx_strtoofft(infop->mem + parser->item_offset, &p, 10, &fsize)) { if(p[0] == '\0' && fsize != CURL_OFF_T_MAX && fsize != CURL_OFF_T_MIN) { @@ -669,7 +669,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, case PL_UNIX_TIME_PREPART1: if(c != ' ') { if(ISALNUM(c)) { - parser->item_offset = finfo->b_used -1; + parser->item_offset = infop->used -1; parser->item_length = 1; parser->state.UNIX.sub.time = PL_UNIX_TIME_PART1; } @@ -726,10 +726,10 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, case PL_UNIX_TIME_PART3: parser->item_length++; if(c == ' ') { - finfo->b_data[parser->item_offset + parser->item_length -1] = 0; + infop->mem[parser->item_offset + parser->item_length -1] = 0; parser->offsets.time = parser->item_offset; /* - if(ftp_pl_gettime(parser, finfo->b_data + parser->item_offset)) { + if(ftp_pl_gettime(parser, finfo->mem + parser->item_offset)) { parser->file_data->flags |= CURLFINFOFLAG_KNOWN_TIME; } */ @@ -753,7 +753,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, switch(parser->state.UNIX.sub.filename) { case PL_UNIX_FILENAME_PRESPACE: if(c != ' ') { - parser->item_offset = finfo->b_used - 1; + parser->item_offset = infop->used - 1; parser->item_length = 1; parser->state.UNIX.sub.filename = PL_UNIX_FILENAME_NAME; } @@ -764,7 +764,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, parser->state.UNIX.sub.filename = PL_UNIX_FILENAME_WINDOWSEOL; } else if(c == '\n') { - finfo->b_data[parser->item_offset + parser->item_length - 1] = 0; + infop->mem[parser->item_offset + parser->item_length - 1] = 0; parser->offsets.filename = parser->item_offset; parser->state.UNIX.main = PL_UNIX_FILETYPE; result = ftp_pl_insert_finfo(data, infop); @@ -776,7 +776,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, break; case PL_UNIX_FILENAME_WINDOWSEOL: if(c == '\n') { - finfo->b_data[parser->item_offset + parser->item_length - 1] = 0; + infop->mem[parser->item_offset + parser->item_length - 1] = 0; parser->offsets.filename = parser->item_offset; parser->state.UNIX.main = PL_UNIX_FILETYPE; result = ftp_pl_insert_finfo(data, infop); @@ -796,7 +796,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, switch(parser->state.UNIX.sub.symlink) { case PL_UNIX_SYMLINK_PRESPACE: if(c != ' ') { - parser->item_offset = finfo->b_used - 1; + parser->item_offset = infop->used - 1; parser->item_length = 1; parser->state.UNIX.sub.symlink = PL_UNIX_SYMLINK_NAME; } @@ -842,7 +842,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, if(c == ' ') { parser->state.UNIX.sub.symlink = PL_UNIX_SYMLINK_PRETARGET4; /* now place where is symlink following */ - finfo->b_data[parser->item_offset + parser->item_length - 4] = 0; + infop->mem[parser->item_offset + parser->item_length - 4] = 0; parser->offsets.filename = parser->item_offset; parser->item_length = 0; parser->item_offset = 0; @@ -858,7 +858,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, case PL_UNIX_SYMLINK_PRETARGET4: if(c != '\r' && c != '\n') { parser->state.UNIX.sub.symlink = PL_UNIX_SYMLINK_TARGET; - parser->item_offset = finfo->b_used - 1; + parser->item_offset = infop->used - 1; parser->item_length = 1; } else { @@ -872,7 +872,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, parser->state.UNIX.sub.symlink = PL_UNIX_SYMLINK_WINDOWSEOL; } else if(c == '\n') { - finfo->b_data[parser->item_offset + parser->item_length - 1] = 0; + infop->mem[parser->item_offset + parser->item_length - 1] = 0; parser->offsets.symlink_target = parser->item_offset; result = ftp_pl_insert_finfo(data, infop); if(result) { @@ -884,7 +884,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, break; case PL_UNIX_SYMLINK_WINDOWSEOL: if(c == '\n') { - finfo->b_data[parser->item_offset + parser->item_length - 1] = 0; + infop->mem[parser->item_offset + parser->item_length - 1] = 0; parser->offsets.symlink_target = parser->item_offset; result = ftp_pl_insert_finfo(data, infop); if(result) { @@ -938,7 +938,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, case PL_WINNT_TIME_TIME: if(c == ' ') { parser->offsets.time = parser->item_offset; - finfo->b_data[parser->item_offset + parser->item_length -1] = 0; + infop->mem[parser->item_offset + parser->item_length -1] = 0; parser->state.NT.main = PL_WINNT_DIRORSIZE; parser->state.NT.sub.dirorsize = PL_WINNT_DIRORSIZE_PRESPACE; parser->item_length = 0; @@ -954,7 +954,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, switch(parser->state.NT.sub.dirorsize) { case PL_WINNT_DIRORSIZE_PRESPACE: if(c != ' ') { - parser->item_offset = finfo->b_used - 1; + parser->item_offset = infop->used - 1; parser->item_length = 1; parser->state.NT.sub.dirorsize = PL_WINNT_DIRORSIZE_CONTENT; } @@ -962,14 +962,14 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, case PL_WINNT_DIRORSIZE_CONTENT: parser->item_length ++; if(c == ' ') { - finfo->b_data[parser->item_offset + parser->item_length - 1] = 0; - if(strcmp("", finfo->b_data + parser->item_offset) == 0) { + infop->mem[parser->item_offset + parser->item_length - 1] = 0; + if(strcmp("", infop->mem + parser->item_offset) == 0) { finfo->filetype = CURLFILETYPE_DIRECTORY; finfo->size = 0; } else { char *endptr; - if(curlx_strtoofft(finfo->b_data + + if(curlx_strtoofft(infop->mem + parser->item_offset, &endptr, 10, &finfo->size)) { parser->error = CURLE_FTP_BAD_FILE_LIST; @@ -991,7 +991,7 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, switch(parser->state.NT.sub.filename) { case PL_WINNT_FILENAME_PRESPACE: if(c != ' ') { - parser->item_offset = finfo->b_used -1; + parser->item_offset = infop->used -1; parser->item_length = 1; parser->state.NT.sub.filename = PL_WINNT_FILENAME_CONTENT; } @@ -1000,11 +1000,11 @@ size_t Curl_ftp_parselist(char *buffer, size_t size, size_t nmemb, parser->item_length++; if(c == '\r') { parser->state.NT.sub.filename = PL_WINNT_FILENAME_WINEOL; - finfo->b_data[finfo->b_used - 1] = 0; + infop->mem[infop->used - 1] = 0; } else if(c == '\n') { parser->offsets.filename = parser->item_offset; - finfo->b_data[finfo->b_used - 1] = 0; + infop->mem[infop->used - 1] = 0; result = ftp_pl_insert_finfo(data, infop); if(result) { parser->error = result;