Skip to content

Commit

Permalink
Add null check to string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernthedev committed Oct 28, 2023
1 parent 08dc05f commit 34f4b1b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
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>("");
}

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", 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", 0);
}

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

Expand Down

0 comments on commit 34f4b1b

Please sign in to comment.