Last active
August 29, 2015 14:07
-
-
Save hanswolff/f01ac03e590ce580fe45 to your computer and use it in GitHub Desktop.
Break interned string representation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
class Program | |
{ | |
[StructLayout(LayoutKind.Explicit)] | |
struct Union | |
{ | |
[FieldOffset(0)] | |
public string A; | |
[FieldOffset(0)] | |
public Breaker B; | |
} | |
class Breaker | |
{ | |
public int Length; | |
public char FirstChar; | |
} | |
static void Main(string[] args) | |
{ | |
var u = new Union(); | |
u.A = "aaaa"; | |
u.B.FirstChar = 'b'; | |
Console.WriteLine("aaaa".StartsWith("a")); // false | |
} | |
} |
It's a hack to mutate the interned string literal using a struct with overlapping object references. It's a known glitch in the .NET Framework
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow, I don't even understand what that does :$