Correctly null-terminate the buffer after calling readlink. (Bug 476608) r=bsmedberg
authorL. David Baron <dbaron@dbaron.org>
Tue, 24 Feb 2009 16:14:49 -0800
changeset 25453 9c12373aed0ac391876cfe07f8b89f88e38536b9
parent 25452 cffce9bd0a47fe358060f858d5ea42913e98ea90
child 25454 0e41c1979d2562ec0b769be5a045ed3392b4655b
push idunknown
push userunknown
push dateunknown
reviewersbsmedberg
bugs476608
milestone1.9.2a1pre
Correctly null-terminate the buffer after calling readlink. (Bug 476608) r=bsmedberg
toolkit/xre/nsAppRunner.cpp
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -1349,22 +1349,26 @@ XRE_GetBinaryPath(const char* argv0, nsI
   // 1) look for /proc/<pid>/exe which is a symlink to the executable on newer
   //    Linux kernels
   // 2) use realpath() on argv[0], which works unless we're loaded from the
   //    PATH
   // 3) manually walk through the PATH and look for ourself
   // 4) give up
 
 // #ifdef __linux__
+// Commented out because it used to not work because it used to not deal
+// with readlink not null-terminating the buffer.
 #if 0
   int r = readlink("/proc/self/exe", exePath, MAXPATHLEN);
 
-  // apparently, /proc/self/exe can sometimes return weird data... check it
-  if (r > 0 && r < MAXPATHLEN && stat(exePath, &fileStat) == 0) {
-    rv = NS_OK;
+  if (r > 0 && r < MAXPATHLEN) {
+    exePath[r] = '\0';
+    if (stat(exePath, &fileStat) == 0) {
+      rv = NS_OK;
+    }
   }
 
 #endif
   if (NS_FAILED(rv) &&
       realpath(argv0, exePath) && stat(exePath, &fileStat) == 0) {
     rv = NS_OK;
   }