snapshot-interface: bugfix - terminate string from readlink()
authorBrian Norris <banorris@uci.edu>
Fri, 19 Apr 2013 22:21:07 +0000 (15:21 -0700)
committerBrian Norris <banorris@uci.edu>
Fri, 19 Apr 2013 22:25:29 +0000 (15:25 -0700)
readlink() doesn't terminate the string for us, so our string doesn't
match properly (it will have a little extra garbage at the end).

snapshot-interface.cc

index 5f8a68773eeba95fca07678ee73d6e39ace1c62d..fdabcf33549349acb203ac201608461aa25d049c 100644 (file)
@@ -85,10 +85,16 @@ static void SnapshotGlobalSegments()
 
 static void get_binary_name(char *buf, size_t len)
 {
 
 static void get_binary_name(char *buf, size_t len)
 {
-       if (readlink("/proc/self/exe", buf, len) == -1) {
+       ssize_t size = readlink("/proc/self/exe", buf, len);
+       if (size < 0) {
                perror("readlink");
                exit(EXIT_FAILURE);
        }
                perror("readlink");
                exit(EXIT_FAILURE);
        }
+
+       /* Terminate string */
+       if ((size_t)size > len)
+               size = len;
+       buf[size] = '\0';
 }
 
 /** The SnapshotGlobalSegments function computes the memory regions
 }
 
 /** The SnapshotGlobalSegments function computes the memory regions