Last active
May 14, 2023 13:16
-
-
Save mrange/774072703a534790f98bc1885ab7cdba to your computer and use it in GitHub Desktop.
Mutate C# strings
This file contains 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.Runtime.CompilerServices; | |
// An "immutable" string | |
var x = "I am immutable"; | |
// *Insert evil laugh here* | |
var y = Unsafe.As<char[]>(x); | |
var z = "mutable!!"; | |
// Overwriting the string | |
// Might only "work" on x64 but for hackers 'Might' is good enough ;) | |
for (int i = 0; i < z.Length; ++i) y[i+3] = z[i]; | |
// Prints "I am mutable!!" | |
Console.WriteLine(x); |
This file contains 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
open System | |
open System.Runtime.CompilerServices | |
let x = "I am immutable"; | |
let y = Unsafe.As<char[]>(x); | |
let z = "mutable!!"; | |
for i = 0 to z.Length - 1 do y[i+3] <- z[i]; | |
Console.WriteLine x |
This file contains 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
Imports System.Runtime.CompilerServices | |
Module Program | |
Sub Main(args As String()) | |
Dim x = "I am immutable" | |
Dim y = Unsafe.As(Of Char())(x) | |
Dim z = "mutable!!" | |
For i = 0 To z.Length - 1 | |
y(i + 3) = z(i) | |
Next | |
Console.WriteLine(x) | |
End Sub | |
End Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment