From: Michael Tremer Date: Thu, 13 May 2021 15:23:06 +0000 (+0000) Subject: filelist: Make parsing more robust X-Git-Tag: 0.9.28~1285^2~138 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b38802ab3246b7298ceccce27927098481aff3f2;p=pakfire.git filelist: Make parsing more robust This code is utterly broken and probably should be rewritten from scratch. Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/filelist.c b/src/libpakfire/filelist.c index 52faf9be4..038619899 100644 --- a/src/libpakfire/filelist.c +++ b/src/libpakfire/filelist.c @@ -163,25 +163,22 @@ PAKFIRE_EXPORT size_t pakfire_filelist_total_filesize(PakfireFilelist list) { } static int pakfire_filelist_parse_line(PakfireFile* file, Pakfire pakfire, - const char* line, unsigned int format) { + char* line, unsigned int format) { unsigned int i = 0; - char* data = strdupa(line); - if (!data) - return 1; - // Allocate file int r = pakfire_file_create(file, pakfire); if (r) return r; + char buffer[PATH_MAX]; ssize_t size; mode_t mode; time_t time; - unsigned int bytes_read = 0; + char* tok = NULL; + char* word = strtok_r(line, " ", &tok); - char* word = strtok(data, " "); while (word) { if (format >= 4) { switch (i) { @@ -223,9 +220,9 @@ static int pakfire_filelist_parse_line(PakfireFile* file, Pakfire pakfire, break; // path - #warning handle filenames with spaces case 8: - pakfire_file_set_path(*file, line + bytes_read); + pakfire_string_format(buffer, "/%s", word); + pakfire_file_set_path(*file, buffer); break; } @@ -275,13 +272,7 @@ static int pakfire_filelist_parse_line(PakfireFile* file, Pakfire pakfire, } } - // Count the bytes of the line that have been processed so far - // (Skip all padding spaces) - bytes_read += strlen(word) + 1; - while (*(line + bytes_read) == ' ') - bytes_read += 1; - - word = strtok(NULL, " "); + word = strtok_r(NULL, " ", &tok); ++i; } @@ -300,14 +291,11 @@ int pakfire_filelist_create_from_file(PakfireFilelist* list, Pakfire pakfire, return r; PakfireFile file = NULL; - char* p = data; - - while (*p) { - char* eol = strchr(p, '\n'); - if (eol) - *eol = '\0'; + char* tok = NULL; - int r = pakfire_filelist_parse_line(&file, pakfire, p, format); + char* line = strtok_r(data, "\n", &tok); + while (line) { + int r = pakfire_filelist_parse_line(&file, pakfire, line, format); if (r) goto ERROR; @@ -319,7 +307,7 @@ int pakfire_filelist_create_from_file(PakfireFilelist* list, Pakfire pakfire, pakfire_file_unref(file); // Move forward - p += strlen(p) + 1; + line = strtok_r(NULL, "\n", &tok); } return 0; diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index 433105e8f..2dea9b1a8 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -726,7 +726,7 @@ int pakfire_archive_copy_data_to_buffer(Pakfire pakfire, struct archive* a, return 0; // Allocate a block of the required size - *data = malloc(required_size); + *data = calloc(1, required_size + 1); if (!*data) return ENOMEM;