Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add null check to string_view #148

Merged
merged 2 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions shared/utils/il2cpp-utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ namespace il2cpp_utils {
template<CreationType creationType = CreationType::Temporary>
Il2CppString* newcsstr(std::u16string_view inp) {
il2cpp_functions::Init();

// if null string input,
// return an empty allocated il2cpp string
if (inp.data() == nullptr) {
return newcsstr<creationType>(u"");
}

if constexpr (creationType == CreationType::Manual) {
auto len = inp.length();
auto mallocSize = sizeof(Il2CppString) + sizeof(Il2CppChar) * (len + 1);
Expand All @@ -108,6 +115,13 @@ namespace il2cpp_utils {
template<CreationType creationType = CreationType::Temporary>
Il2CppString* newcsstr(std::string_view inp) {
il2cpp_functions::Init();

// if null string input,
// return an empty allocated il2cpp string
if (inp.data() == nullptr) {
return newcsstr<creationType>("");
}

if constexpr (creationType == CreationType::Manual) {
// TODO: Perhaps manually call createManual instead
auto len = inp.length();
Expand Down
9 changes: 9 additions & 0 deletions src/utils/typedefs-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,19 @@ std::size_t convstr(char16_t const* inp, char* outp, int isz, int osz) {

Il2CppString* alloc_str(std::string_view str) {
il2cpp_functions::Init();

if (str.data() == nullptr) {
return il2cpp_functions::string_new_len("", 0);
}

return il2cpp_functions::string_new_len(str.data(), str.size());
}
Il2CppString* alloc_str(std::u16string_view str) {
il2cpp_functions::Init();
if (str.data() == nullptr) {
return il2cpp_functions::string_new_len("", 0);
}

return il2cpp_functions::string_new_utf16((Il2CppChar const*)str.data(), str.size());
}

Expand Down
Loading