]> git.itanic.dy.fi Git - scan-pagemap/blobdiff - parse.c
read_pageframe: Do not add empty entries in process list
[scan-pagemap] / parse.c
diff --git a/parse.c b/parse.c
index 850b2a1a55938961f493e0969c69839d72c725e9..3934fff77ee58b6e160756d0adeed1d1b45e506b 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -297,13 +297,12 @@ static int read_pageframe(int pid, int tid, struct pageframe *pageframe,
        process->pid = pid;
        process->tid = tid;
 
-       list_add_tail(&process->list, &(*process_list)->list);
 
        snprintf(path, sizeof(path), "/proc/%d/task/%d/maps", pid, tid);
        file = fopen(path, "rb");
 
        if (!file)
-               return 0;
+               goto free;
 
        maps = parse_maps(file, pid, tid);
        fclose(file);
@@ -313,23 +312,28 @@ static int read_pageframe(int pid, int tid, struct pageframe *pageframe,
        file = fopen(path, "rb");
 
        if (!file)
-               return 0;
+               goto free;
 
        parse_pageframe(file, pageframe, maps, opts);
        fclose(file);
 
        if (read_cmdline(pid, tid, process->name, sizeof(process->name)))
-               return 1;
-
-       if (maps == NULL)
-               return 1;
+               goto free;
 
-       list_for_each_entry(maps, &process->maps->list, list) {
-               process->pages_present += maps->pages_present;
-               process->pages_swapped += maps->pages_swapped;
+       if (maps != NULL) {
+               list_for_each_entry(maps, &process->maps->list, list) {
+                       process->pages_present += maps->pages_present;
+                       process->pages_swapped += maps->pages_swapped;
+               }
        }
 
+       list_add_tail(&process->list, &(*process_list)->list);
+
        return 1;
+free:
+       free(process);
+
+       return 0;
 }
 
 static int parse_pid(DIR **dir)