Bug 1371279 - Try to use reserved stack space instead of heap allocation in nsLocalFile::GetNativeTarget(); r=froydnj
authorEhsan Akhgari <ehsan@mozilla.com>
Thu, 08 Jun 2017 10:04:12 -0400
changeset 411276 ea0c86248f0aee0133154de3d763917f42748300
parent 411275 830842118f8a0f636aa298dd27a34918d924f746
child 411277 7680538080d1bc8dd25be592d94ede01145fdb4a
push id7391
push usermtabara@mozilla.com
push dateMon, 12 Jun 2017 13:08:53 +0000
treeherdermozilla-beta@2191d7f87e2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1371279
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1371279 - Try to use reserved stack space instead of heap allocation in nsLocalFile::GetNativeTarget(); r=froydnj
xpcom/io/nsLocalFileUnix.cpp
--- a/xpcom/io/nsLocalFileUnix.cpp
+++ b/xpcom/io/nsLocalFileUnix.cpp
@@ -1748,42 +1748,40 @@ nsLocalFile::GetNativeTarget(nsACString&
     return NSRESULT_FOR_ERRNO();
   }
 
   if (!S_ISLNK(symStat.st_mode)) {
     return NS_ERROR_FILE_INVALID_PATH;
   }
 
   int32_t size = (int32_t)symStat.st_size;
-  char* target = (char*)moz_xmalloc(size + 1);
-  if (!target) {
+  nsAutoCString target;
+  if (!target.SetLength(size, mozilla::fallible)) {
     return NS_ERROR_OUT_OF_MEMORY;
   }
 
-  if (readlink(mPath.get(), target, (size_t)size) < 0) {
-    free(target);
+  if (readlink(mPath.get(), target.BeginWriting(), (size_t)size) < 0) {
     return NSRESULT_FOR_ERRNO();
   }
-  target[size] = '\0';
 
   nsresult rv = NS_OK;
   nsCOMPtr<nsIFile> self(this);
   int32_t maxLinks = 40;
   while (true) {
     if (maxLinks-- == 0) {
       rv = NS_ERROR_FILE_UNRESOLVABLE_SYMLINK;
       break;
     }
 
     if (target[0] != '/') {
       nsCOMPtr<nsIFile> parent;
       if (NS_FAILED(rv = self->GetParent(getter_AddRefs(parent)))) {
         break;
       }
-      if (NS_FAILED(rv = parent->AppendRelativeNativePath(nsDependentCString(target)))) {
+      if (NS_FAILED(rv = parent->AppendRelativeNativePath(target))) {
         break;
       }
       if (NS_FAILED(rv = parent->GetNativePath(aResult))) {
         break;
       }
       self = parent;
     } else {
       aResult = target;
@@ -1798,36 +1796,31 @@ nsLocalFile::GetNativeTarget(nsACString&
     }
 
     // And of course we're done if it isn't a symlink.
     if (!S_ISLNK(symStat.st_mode)) {
       break;
     }
 
     int32_t newSize = (int32_t)symStat.st_size;
-    if (newSize > size) {
-      char* newTarget = (char*)moz_xrealloc(target, newSize + 1);
-      if (!newTarget) {
-        rv = NS_ERROR_OUT_OF_MEMORY;
-        break;
-      }
-      target = newTarget;
-      size = newSize;
+    size = newSize;
+    nsAutoCString newTarget;
+    if (!newTarget.SetLength(size, mozilla::fallible)) {
+      rv = NS_ERROR_OUT_OF_MEMORY;
+      break;
     }
 
-    int32_t linkLen = readlink(flatRetval.get(), target, size);
+    int32_t linkLen = readlink(flatRetval.get(), newTarget.BeginWriting(), size);
     if (linkLen == -1) {
       rv = NSRESULT_FOR_ERRNO();
       break;
     }
-    target[linkLen] = '\0';
+    target = newTarget;
   }
 
-  free(target);
-
   if (NS_FAILED(rv)) {
     aResult.Truncate();
   }
   return rv;
 }
 
 NS_IMETHODIMP
 nsLocalFile::GetFollowLinks(bool* aFollowLinks)