summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibELF
diff options
context:
space:
mode:
authorsin-ack <sin-ack@users.noreply.github.com>2022-07-11 19:53:29 +0000
committerAndreas Kling <kling@serenityos.org>2022-07-12 23:11:35 +0200
commitc70f45ff4498fcb7ce0671e9107ecff8009d7eb2 (patch)
tree6250cc4ba6c43ed57639f3d7ff9c5fd34800989f /Userland/Libraries/LibELF
parente3da0adfe6d278424970dad5a642bda650737e42 (diff)
downloadserenity-c70f45ff4498fcb7ce0671e9107ecff8009d7eb2.zip
Everywhere: Explicitly specify the size in StringView constructors
This commit moves the length calculations out to be directly on the StringView users. This is an important step towards the goal of removing StringView(char const*), as it moves the responsibility of calculating the size of the string to the user of the StringView (which will prevent naive uses causing OOB access).
Diffstat (limited to 'Userland/Libraries/LibELF')
-rw-r--r--Userland/Libraries/LibELF/DynamicLinker.cpp9
-rw-r--r--Userland/Libraries/LibELF/DynamicObject.cpp3
-rw-r--r--Userland/Libraries/LibELF/DynamicObject.h3
3 files changed, 8 insertions, 7 deletions
diff --git a/Userland/Libraries/LibELF/DynamicLinker.cpp b/Userland/Libraries/LibELF/DynamicLinker.cpp
index 5e1fe96a20..5e464cd0cb 100644
--- a/Userland/Libraries/LibELF/DynamicLinker.cpp
+++ b/Userland/Libraries/LibELF/DynamicLinker.cpp
@@ -487,19 +487,20 @@ static Result<void*, DlErrorMessage> __dlsym(void* handle, char const* symbol_na
__pthread_mutex_lock(&s_loader_lock);
ScopeGuard unlock_guard = [] { __pthread_mutex_unlock(&s_loader_lock); };
+ StringView symbol_name_view { symbol_name, strlen(symbol_name) };
Optional<DynamicObject::SymbolLookupResult> symbol;
if (handle) {
auto object = static_cast<DynamicObject*>(handle);
- symbol = object->lookup_symbol(symbol_name);
+ symbol = object->lookup_symbol(symbol_name_view);
} else {
// When handle is 0 (RTLD_DEFAULT) we should look up the symbol in all global modules
// https://pubs.opengroup.org/onlinepubs/009604499/functions/dlsym.html
- symbol = DynamicLinker::lookup_global_symbol(symbol_name);
+ symbol = DynamicLinker::lookup_global_symbol(symbol_name_view);
}
if (!symbol.has_value())
- return DlErrorMessage { String::formatted("Symbol {} not found", symbol_name) };
+ return DlErrorMessage { String::formatted("Symbol {} not found", symbol_name_view) };
if (symbol.value().type == STT_GNU_IFUNC)
return (void*)reinterpret_cast<DynamicObject::IfuncResolver>(symbol.value().address.as_ptr())();
@@ -551,7 +552,7 @@ static Result<void, DlErrorMessage> __dladdr(void* addr, Dl_info* info)
static void read_environment_variables()
{
for (char** env = s_envp; *env; ++env) {
- StringView env_string { *env };
+ StringView env_string { *env, strlen(*env) };
if (env_string == "_LOADER_BREAKPOINT=1"sv) {
s_do_breakpoint_trap_before_entry = true;
}
diff --git a/Userland/Libraries/LibELF/DynamicObject.cpp b/Userland/Libraries/LibELF/DynamicObject.cpp
index 64b4e629ce..29471f6560 100644
--- a/Userland/Libraries/LibELF/DynamicObject.cpp
+++ b/Userland/Libraries/LibELF/DynamicObject.cpp
@@ -336,7 +336,8 @@ auto DynamicObject::HashSection::lookup_gnu_symbol(StringView name, u32 hash_val
StringView DynamicObject::symbol_string_table_string(ElfW(Word) index) const
{
- return StringView { (char const*)base_address().offset(m_string_table_offset + index).as_ptr() };
+ auto const* symbol_string_table_ptr = reinterpret_cast<char const*>(base_address().offset(m_string_table_offset + index).as_ptr());
+ return StringView { symbol_string_table_ptr, strlen(symbol_string_table_ptr) };
}
char const* DynamicObject::raw_symbol_string_table_string(ElfW(Word) index) const
diff --git a/Userland/Libraries/LibELF/DynamicObject.h b/Userland/Libraries/LibELF/DynamicObject.h
index 2d40e8dc70..7bf874f410 100644
--- a/Userland/Libraries/LibELF/DynamicObject.h
+++ b/Userland/Libraries/LibELF/DynamicObject.h
@@ -488,8 +488,7 @@ inline void DynamicObject::for_each_needed_library(F func) const
if (entry.tag() != DT_NEEDED)
return;
ElfW(Word) offset = entry.val();
- StringView name { (const char*)(m_base_address.offset(m_string_table_offset).offset(offset)).as_ptr() };
- func(name);
+ func(symbol_string_table_string(offset));
});
}