Fix reading /proc/<pid>/maps file 95/182995/2 accepted/tizen/unified/20180703.163701 submit/tizen/20180629.144736
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>
Fri, 29 Jun 2018 11:31:38 +0000 (13:31 +0200)
committerMateusz Moscicki <m.moscicki2@partner.samsung.com>
Fri, 29 Jun 2018 11:47:33 +0000 (13:47 +0200)
Macro is expanded by preprocessor, but sizeof() is evaluated at compile time.

Change-Id: I4ca20ff703cf89fba2137ec3eb24550d1013fdff

src/crash-stack/crash-stack.c

index 7581e08..3ddffd7 100644 (file)
 
 #define BUF_SIZE (BUFSIZ)
 #define HEXA 16
-#define PERM_LEN 5
-#define ADDR_LEN 16
+#define PERM_LEN 4
+#define ADDR_LEN 33
 #define STR_ANONY "[anony]"
 #define STR_ANONY_LEN 8
 
-#define STR_FS(length) "%"#length"s"
+#define STRING_FORMAT_SPECIFIER_WITH_MACRO(macro) "%"#macro"s"
+#define STR_FS(macro) STRING_FORMAT_SPECIFIER_WITH_MACRO(macro)
 
 static FILE *outputfile = NULL;                ///< global output stream
 static FILE *errfile = NULL;           ///< global error stream
@@ -93,7 +94,7 @@ const struct option opts[] = {
 struct addr_node {
        uintptr_t startaddr;
        uintptr_t endaddr;
-       char perm[PERM_LEN];
+       char perm[PERM_LEN+1];
        char *fpath;
        struct addr_node *next;
 };
@@ -637,9 +638,9 @@ static struct addr_node *get_addr_list_from_maps(int fd)
        int fpath_len, result;
        uintptr_t saddr;
        uintptr_t eaddr;
-       char perm[PERM_LEN];
-       char path[PATH_MAX];
-       char addr[ADDR_LEN * 2 + 2];
+       char perm[PERM_LEN+1];
+       char path[PATH_MAX+1];
+       char addr[ADDR_LEN+1];
        char linebuf[BUF_SIZE];
        struct addr_node *head = NULL;
        struct addr_node *tail = NULL;
@@ -647,14 +648,14 @@ static struct addr_node *get_addr_list_from_maps(int fd)
 
        /* parsing the maps to get executable code address */
        while (fgets_fd(linebuf, BUF_SIZE, fd) != NULL) {
-               memset(path, 0, PATH_MAX);
-               result = sscanf(linebuf, STR_FS(sizeof(addr)-1)
-                                STR_FS(sizeof(perm)-1)
+               memset(path, 0, PATH_MAX+1);
+               result = sscanf(linebuf, STR_FS(ADDR_LEN)
+                                STR_FS(PERM_LEN)
                                 "%*s %*s %*s"
-                                STR_FS(sizeof(path)-1), addr, perm, path);
+                                STR_FS(PATH_MAX), addr, perm, path);
                if (result < 0)
                        continue;
-               perm[PERM_LEN - 1] = 0;
+               perm[PERM_LEN] = 0;
                /* rwxp */
                if ((perm[2] == 'x' && path[0] == '/') ||
                    (perm[1] == 'w' && path[0] != '/')) {
@@ -672,7 +673,7 @@ static struct addr_node *get_addr_list_from_maps(int fd)
                                fprintf(errfile, "error : mmap\n");
                                return NULL;
                        }
-                       memcpy(t_node->perm, perm, PERM_LEN);
+                       memcpy(t_node->perm, perm, PERM_LEN+1);
                        t_node->startaddr = saddr;
                        t_node->endaddr = eaddr;
                        t_node->fpath = NULL;