ソースを参照

LibC: Fix memory leak in realpath

Ben Wiederhake 4 年 前
コミット
ed857bc06e
1 ファイル変更6 行追加1 行削除
  1. 6 1
      Userland/Libraries/LibC/stdlib.cpp

+ 6 - 1
Userland/Libraries/LibC/stdlib.cpp

@@ -1049,11 +1049,16 @@ char* realpath(const char* pathname, char* buffer)
         return nullptr;
     }
     size_t size = PATH_MAX;
-    if (buffer == nullptr)
+    bool self_allocated = false;
+    if (buffer == nullptr) {
         buffer = (char*)malloc(size);
+        self_allocated = true;
+    }
     Syscall::SC_realpath_params params { { pathname, strlen(pathname) }, { buffer, size } };
     int rc = syscall(SC_realpath, &params);
     if (rc < 0) {
+        if (self_allocated)
+            free(buffer);
         errno = -rc;
         return nullptr;
     }