Commit 807a7058 authored by Clemens Backes's avatar Clemens Backes Committed by Commit Bot

[base] Refactor {RelativePath} method

The method allocates, so it should return the buffer in a {unique_ptr}.
Also, the internals can be simplified by using {size_t} instead of
{int} and removing a redundant special case.

R=mlippautz@chromium.org

Bug: v8:9810
Change-Id: I94ac5814c284bf6ab075841ddbfb768d31dfff4c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1849514
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#64195}
parent 98e25684
...@@ -12,23 +12,18 @@ ...@@ -12,23 +12,18 @@
namespace v8 { namespace v8 {
namespace base { namespace base {
char* RelativePath(char** buffer, const char* exec_path, const char* name) { std::unique_ptr<char[]> RelativePath(const char* exec_path, const char* name) {
DCHECK(exec_path); DCHECK(exec_path);
int path_separator = static_cast<int>(strlen(exec_path)) - 1; size_t basename_start = strlen(exec_path);
while (path_separator >= 0 && while (basename_start > 0 &&
!OS::isDirectorySeparator(exec_path[path_separator])) { !OS::isDirectorySeparator(exec_path[basename_start - 1])) {
path_separator--; --basename_start;
} }
if (path_separator >= 0) { size_t name_length = strlen(name);
int name_length = static_cast<int>(strlen(name)); auto buffer = std::make_unique<char[]>(basename_start + name_length + 1);
*buffer = reinterpret_cast<char*>(malloc(path_separator + name_length + 2)); if (basename_start > 0) memcpy(buffer.get(), exec_path, basename_start);
memcpy(*buffer, exec_path, path_separator + 1); memcpy(buffer.get() + basename_start, name, name_length);
memcpy(*buffer + path_separator + 1, name, name_length); return buffer;
(*buffer)[path_separator + name_length + 1] = '\0';
} else {
*buffer = strdup(name);
}
return *buffer;
} }
} // namespace base } // namespace base
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef V8_BASE_FILE_UTILS_H_ #ifndef V8_BASE_FILE_UTILS_H_
#define V8_BASE_FILE_UTILS_H_ #define V8_BASE_FILE_UTILS_H_
#include <memory>
#include "src/base/base-export.h" #include "src/base/base-export.h"
namespace v8 { namespace v8 {
...@@ -12,8 +14,8 @@ namespace base { ...@@ -12,8 +14,8 @@ namespace base {
// Helper functions to manipulate file paths. // Helper functions to manipulate file paths.
V8_BASE_EXPORT char* RelativePath(char** buffer, const char* exec_path, V8_BASE_EXPORT
const char* name); std::unique_ptr<char[]> RelativePath(const char* exec_path, const char* name);
} // namespace base } // namespace base
} // namespace v8 } // namespace v8
......
...@@ -40,26 +40,23 @@ bool InitializeICUDefaultLocation(const char* exec_path, ...@@ -40,26 +40,23 @@ bool InitializeICUDefaultLocation(const char* exec_path,
const char* icu_data_file) { const char* icu_data_file) {
#if !defined(V8_INTL_SUPPORT) #if !defined(V8_INTL_SUPPORT)
return true; return true;
#else #elif ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
#if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
if (icu_data_file) { if (icu_data_file) {
return InitializeICU(icu_data_file); return InitializeICU(icu_data_file);
} }
char* icu_data_file_default;
#if defined(V8_TARGET_LITTLE_ENDIAN) #if defined(V8_TARGET_LITTLE_ENDIAN)
base::RelativePath(&icu_data_file_default, exec_path, "icudtl.dat"); std::unique_ptr<char[]> icu_data_file_default =
base::RelativePath(exec_path, "icudtl.dat");
#elif defined(V8_TARGET_BIG_ENDIAN) #elif defined(V8_TARGET_BIG_ENDIAN)
base::RelativePath(&icu_data_file_default, exec_path, "icudtb.dat"); std::unique_ptr<char[]> icu_data_file_default =
base::RelativePath(exec_path, "icudtb.dat");
#else #else
#error Unknown byte ordering #error Unknown byte ordering
#endif #endif
bool result = InitializeICU(icu_data_file_default); return InitializeICU(icu_data_file_default.get());
free(icu_data_file_default);
return result;
#else #else
return InitializeICU(nullptr); return InitializeICU(nullptr);
#endif #endif
#endif
} }
bool InitializeICU(const char* icu_data_file) { bool InitializeICU(const char* icu_data_file) {
......
...@@ -82,19 +82,17 @@ void LoadFromFiles(const char* natives_blob, const char* snapshot_blob) { ...@@ -82,19 +82,17 @@ void LoadFromFiles(const char* natives_blob, const char* snapshot_blob) {
void InitializeExternalStartupData(const char* directory_path) { void InitializeExternalStartupData(const char* directory_path) {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA #ifdef V8_USE_EXTERNAL_STARTUP_DATA
char* natives;
char* snapshot;
const char* snapshot_name = "snapshot_blob.bin"; const char* snapshot_name = "snapshot_blob.bin";
#ifdef V8_MULTI_SNAPSHOTS #ifdef V8_MULTI_SNAPSHOTS
if (!FLAG_untrusted_code_mitigations) { if (!FLAG_untrusted_code_mitigations) {
snapshot_name = "snapshot_blob_trusted.bin"; snapshot_name = "snapshot_blob_trusted.bin";
} }
#endif #endif
LoadFromFiles( std::unique_ptr<char[]> natives =
base::RelativePath(&natives, directory_path, "natives_blob.bin"), base::RelativePath(directory_path, "natives_blob.bin");
base::RelativePath(&snapshot, directory_path, snapshot_name)); std::unique_ptr<char[]> snapshot =
free(natives); base::RelativePath(directory_path, snapshot_name);
free(snapshot); LoadFromFiles(natives.get(), snapshot.get());
#endif // V8_USE_EXTERNAL_STARTUP_DATA #endif // V8_USE_EXTERNAL_STARTUP_DATA
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment