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

Prevent crashes when demangling symbols exposed in Minecraft: Wii U Edition #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 28 additions & 4 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ static void DemangleAll(StreamReader r, string fileName)
/* e.g. "h__Fi" => "h(int)" */
static string Demangle(string name)
{
if (name.Contains("__ghs_thunk__")) {
name = name.Substring(25);
}

name = Decompress(name);

/* This demangle method has basically turned into a hand-written
Expand Down Expand Up @@ -295,7 +299,13 @@ static string Decompress(string name)

int end = name.IndexOf('J', start + 1);

if (end != -1)
/* if two Js are right next to each other, then just append one J and move forward 2 characters. */
if (end == start + 1)
{
result += "J";
index = start + 2;
}
else if (end != -1)
{
bool valid = true;

Expand Down Expand Up @@ -457,7 +467,7 @@ private static string ReadTemplateArguments(string name, out string remainder)
if (args.Count > 0)
result += ", ";

string type, val;
string type, val = "";

if (remainder.StartsWith("X", StringComparison.Ordinal))
{
Expand All @@ -476,7 +486,7 @@ private static string ReadTemplateArguments(string name, out string remainder)
else
{
/* <type><encoding> */
type = ReadType(args, remainder, out remainder).Replace("#", " #");
type = ReadType(args, remainder, out remainder);

if (remainder.StartsWith("L", StringComparison.Ordinal))
{
Expand All @@ -494,12 +504,16 @@ private static string ReadTemplateArguments(string name, out string remainder)
if (!remainder.StartsWith("_"))
throw new InvalidDataException("Unexpected character after template parameter length \"" + remainder[0] + "\". Expected \"_\".");

type = type.Replace("#", " #");
remainder = remainder.Substring(1);
val = remainder.Substring(0, len);
remainder = remainder.Substring(len);
}
else
throw new InvalidDataException("Unknown template parameter encoding: \"" + remainder[0] + "\".");
{
val = type.Replace("#", "");
type = "class #";
}
}
}
else
Expand Down Expand Up @@ -697,6 +711,16 @@ static string ReadString(string name, out string remainder)
if (len == 0 || name.Length < len)
throw new InvalidDataException("Bad string length \"" + len.ToString(CultureInfo.InvariantCulture) + "\".");

/* if the mangled string starts with __N_, and the number following is equal to the length of the string -9, then it refers to an anonymous namespace.*/
if (name.Length > 4)
{
if (name.Substring(0, 4) == "__N_")
{
remainder = name.Substring(len);
return "<anonymous>";
}
}

remainder = name.Substring(len);
return DemangleTemplate(name.Substring(0, len));
}
Expand Down