]> git.itanic.dy.fi Git - scan-pagemap/commitdiff
parse_maps: Fix issues with white space in file names
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Thu, 12 Jul 2012 14:41:49 +0000 (17:41 +0300)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Thu, 12 Jul 2012 14:41:49 +0000 (17:41 +0300)
If there is any white space character in the file name field, sscanf
would only copy the file name part until the first white space and
leave out everything else.

The fix is trivial; we will use the %n with sscanf to find out all the
chars before the file name part and then skip that part when using
strncpy to copy the file name.

Signed-off-by: Timo Kokkonen <timo.t.kokkonen@iki.fi>
parse.c

diff --git a/parse.c b/parse.c
index afcfea95cb4e74410440068e434f9e0e572f526b..296d2017f341c7ec134ef2fc6ab1902a77fbea4a 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -61,7 +61,7 @@ static struct maps *parse_maps(FILE *file, int pid, int tid)
        while (fgets(line, sizeof(line), file)) {
                struct maps *map = alloc_map();
                unsigned long start, end;
-               char name[1024];
+               int skip;
 
                if (map == NULL)
                        return 0;
@@ -69,8 +69,8 @@ static struct maps *parse_maps(FILE *file, int pid, int tid)
                if (the_map == NULL)
                        the_map = map;
 
-               ret = sscanf(line, "%lx-%lx %*s %*s %*s %*s %s",
-                            &start, &end, name);
+               ret = sscanf(line, "%lx-%lx %*s %*s %*s %*s %n",
+                            &start, &end, &skip);
 
                if (ret < 2) {
                        printf("Error reading input: %s\n", line);
@@ -83,8 +83,10 @@ static struct maps *parse_maps(FILE *file, int pid, int tid)
                map->pid = pid;
                map->tid = tid;
 
-               if (ret >= 3)
-                       strncpy(map->name, name, sizeof(map->name));
+               strncpy(map->name, line + skip, sizeof(map->name));
+
+               /* zero out the newline */
+               map->name[MAX(strlen(map->name) - 1, 0)] = '\0';
 
                list_add_tail(&map->list, &the_map->list);
        }