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 @@
namespace v8 {
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);
int path_separator = static_cast<int>(strlen(exec_path)) - 1;
while (path_separator >= 0 &&
!OS::isDirectorySeparator(exec_path[path_separator])) {
path_separator--;
size_t basename_start = strlen(exec_path);
while (basename_start > 0 &&
!OS::isDirectorySeparator(exec_path[basename_start - 1])) {
--basename_start;
}
if (path_separator >= 0) {
int name_length = static_cast<int>(strlen(name));
*buffer = reinterpret_cast<char*>(malloc(path_separator + name_length + 2));
memcpy(*buffer, exec_path, path_separator + 1);
memcpy(*buffer + path_separator + 1, name, name_length);
(*buffer)[path_separator + name_length + 1] = '\0';
} else {
*buffer = strdup(name);
}
return *buffer;
size_t name_length = strlen(name);
auto buffer = std::make_unique<char[]>(basename_start + name_length + 1);
if (basename_start > 0) memcpy(buffer.get(), exec_path, basename_start);
memcpy(buffer.get() + basename_start, name, name_length);
return buffer;
}
} // namespace base
......
......@@ -5,6 +5,8 @@
#ifndef V8_BASE_FILE_UTILS_H_
#define V8_BASE_FILE_UTILS_H_
#include <memory>
#include "src/base/base-export.h"
namespace v8 {
......@@ -12,8 +14,8 @@ namespace base {
// Helper functions to manipulate file paths.
V8_BASE_EXPORT char* RelativePath(char** buffer, const char* exec_path,
const char* name);
V8_BASE_EXPORT
std::unique_ptr<char[]> RelativePath(const char* exec_path, const char* name);
} // namespace base
} // namespace v8
......
......@@ -40,26 +40,23 @@ bool InitializeICUDefaultLocation(const char* exec_path,
const char* icu_data_file) {
#if !defined(V8_INTL_SUPPORT)
return true;
#else
#if ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
#elif ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE
if (icu_data_file) {
return InitializeICU(icu_data_file);
}
char* icu_data_file_default;
#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)
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
#error Unknown byte ordering
#endif
bool result = InitializeICU(icu_data_file_default);
free(icu_data_file_default);
return result;
return InitializeICU(icu_data_file_default.get());
#else
return InitializeICU(nullptr);
#endif
#endif
}
bool InitializeICU(const char* icu_data_file) {
......
......@@ -82,19 +82,17 @@ void LoadFromFiles(const char* natives_blob, const char* snapshot_blob) {
void InitializeExternalStartupData(const char* directory_path) {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
char* natives;
char* snapshot;
const char* snapshot_name = "snapshot_blob.bin";
#ifdef V8_MULTI_SNAPSHOTS
if (!FLAG_untrusted_code_mitigations) {
snapshot_name = "snapshot_blob_trusted.bin";
}
#endif
LoadFromFiles(
base::RelativePath(&natives, directory_path, "natives_blob.bin"),
base::RelativePath(&snapshot, directory_path, snapshot_name));
free(natives);
free(snapshot);
std::unique_ptr<char[]> natives =
base::RelativePath(directory_path, "natives_blob.bin");
std::unique_ptr<char[]> snapshot =
base::RelativePath(directory_path, snapshot_name);
LoadFromFiles(natives.get(), snapshot.get());
#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