]> git.ipfire.org Git - pakfire.git/commitdiff
filelist: Make parsing more robust
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 13 May 2021 15:23:06 +0000 (15:23 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 13 May 2021 15:23:06 +0000 (15:23 +0000)
This code is utterly broken and probably should be rewritten from
scratch.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/filelist.c
src/libpakfire/util.c

index 52faf9be4d498f6b010a0a6f159db20c84c469d5..0386198993b743ce647d2501438dad053754d731 100644 (file)
@@ -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;
index 433105e8f6df2ed9636c75916c4dd93b50679b6a..2dea9b1a8c16ae442b4e2d91a5cc54e92432185a 100644 (file)
@@ -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;