Don't use NULL in realpath() on darwin, doesn't work in older versions
authorRyan Dahl <ry@tinyclouds.org>
Tue, 22 Jun 2010 20:14:03 +0000 (13:14 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Tue, 22 Jun 2010 20:19:49 +0000 (13:19 -0700)
Thanks to Peter Griess <pg@std.in> for the bug report.

src/platform_darwin.cc

index cf0ac71..2ff43f2 100644 (file)
@@ -4,6 +4,7 @@
 #include <mach/task.h>
 #include <mach/mach_init.h>
 #include <mach-o/dyld.h> /* _NSGetExecutablePath */
+#include <limits.h> /* PATH_MAX */
 
 namespace node {
 
@@ -31,10 +32,16 @@ int OS::GetExecutablePath(char* buffer, size_t* size) {
   uint32_t usize = *size;
   int result = _NSGetExecutablePath(buffer, &usize);
   if (result) return result;
-  char *path = realpath(buffer, NULL);
-  if (path == NULL) return -1;
-  strncpy(buffer, path, *size);
-  free(path);
+
+  char *path = new char[2*PATH_MAX];
+
+  char *fullpath = realpath(buffer, path);
+  if (fullpath == NULL) {
+    delete [] path;
+    return -1;
+  }
+  strncpy(buffer, fullpath, *size);
+  delete [] fullpath;
   *size = strlen(buffer);
   return 0;
 }