Be careful of mangled out of bounds read

This commit is contained in:
Kelebek1 2023-01-14 19:53:55 +00:00
parent ce0b8d618d
commit 42b16bb33a
2 changed files with 9 additions and 9 deletions

View File

@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#include "common/demangle.h" #include "common/demangle.h"
#include "common/scope_exit.h"
namespace llvm { namespace llvm {
char* itaniumDemangle(const char* mangled_name, char* buf, size_t* n, int* status); char* itaniumDemangle(const char* mangled_name, char* buf, size_t* n, int* status);
@ -13,10 +14,16 @@ std::string DemangleSymbol(const std::string& mangled) {
auto is_itanium = [](const std::string& name) -> bool { auto is_itanium = [](const std::string& name) -> bool {
// A valid Itanium encoding requires 1-4 leading underscores, followed by 'Z'. // A valid Itanium encoding requires 1-4 leading underscores, followed by 'Z'.
auto pos = name.find_first_not_of('_'); auto pos = name.find_first_not_of('_');
return pos > 0 && pos <= 4 && name[pos] == 'Z'; return pos > 0 && pos <= 4 && pos < name.size() && name[pos] == 'Z';
}; };
if (mangled.empty()) {
return mangled;
}
char* demangled = nullptr; char* demangled = nullptr;
SCOPE_EXIT({ std::free(demangled); });
if (is_itanium(mangled)) { if (is_itanium(mangled)) {
demangled = llvm::itaniumDemangle(mangled.c_str(), nullptr, nullptr, nullptr); demangled = llvm::itaniumDemangle(mangled.c_str(), nullptr, nullptr, nullptr);
} }
@ -24,10 +31,7 @@ std::string DemangleSymbol(const std::string& mangled) {
if (!demangled) { if (!demangled) {
return mangled; return mangled;
} }
return demangled;
std::string ret = demangled;
std::free(demangled);
return ret;
} }
} // namespace Common } // namespace Common

View File

@ -1,10 +1,6 @@
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#ifndef _MSC_VER
#include <cxxabi.h>
#endif
#include <map> #include <map>
#include <optional> #include <optional>