Skip to content

Instantly share code, notes, and snippets.

@duncte123
Created March 9, 2024 17:07
Show Gist options
  • Save duncte123/b45574d44f4a22e9fc8c2b3a30c78084 to your computer and use it in GitHub Desktop.
Save duncte123/b45574d44f4a22e9fc8c2b3a30c78084 to your computer and use it in GitHub Desktop.
Storing a string in an integer
package me.duncte123.fuck;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import sun.misc.Unsafe;
public class Main {
private static Integer integer;
public static void main(String[] args) throws Throwable {
Constructor<Unsafe> c = Unsafe.class.getDeclaredConstructor();
c.setAccessible(true);
Unsafe u = c.newInstance();
Field f = Main.class.getDeclaredField("integer");
Object b = u.staticFieldBase(f);
long o = u.staticFieldOffset(f);
u.putObject(b, o, "this is a string");
String s = (String)(Object)integer;
System.out.println(s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment