]> git.cworth.org Git - apitrace/commitdiff
os: Ensure len is properly computed on all getProcessName code paths.
authorJosé Fonseca <jfonseca@vmware.com>
Mon, 17 Jun 2013 20:05:14 +0000 (21:05 +0100)
committerJosé Fonseca <jfonseca@vmware.com>
Mon, 17 Jun 2013 20:05:14 +0000 (21:05 +0100)
And that truncate is always called.

Thanks to Alexander Monakov for spotting this.

common/os_posix.cpp

index 7ddd89530f4883fda0efc85fdac69e4e4df67c4d..9e8580f2f8f505b2a3753b34d413038d11bb88dd 100644 (file)
@@ -71,25 +71,33 @@ getProcessName(void)
 #ifdef __APPLE__
     uint32_t len = size;
     if (_NSGetExecutablePath(buf, &len) != 0) {
-        *buf = 0;
-        return path;
+        // grow buf and retry
+        buf = path.buf(len);
+        _NSGetExecutablePath(buf, &len);
     }
     len = strlen(buf);
 #else
     ssize_t len;
     len = readlink("/proc/self/exe", buf, size - 1);
-    if (len == -1) {
+    if (len <= 0) {
         // /proc/self/exe is not available on setuid processes, so fallback to
         // /proc/self/cmdline.
         int fd = open("/proc/self/cmdline", O_RDONLY);
         if (fd >= 0) {
-            len = read(fd, buf, size - 1);
+            // buf already includes trailing zero
+            len = read(fd, buf, size);
             close(fd);
+            if (len >= 0) {
+                len = strlen(buf);
+            }
         }
     }
     if (len <= 0) {
-        snprintf(buf, size, "%i", (int)getpid());
-        return path;
+        // fallback to process ID
+        len = snprintf(buf, size, "%i", (int)getpid());
+        if (len >= size) {
+            len = size - 1;
+        }
     }
 #endif
     path.truncate(len);