Skip to content

Instantly share code, notes, and snippets.

@Milek7

Milek7/test.cpp Secret

Created Dec 4, 2020
Embed
What would you like to do?
#include <stddef.h>
struct SaveLoad {
bool global;
void *address;
};
static inline void *GetVariableAddress(const void *object, const SaveLoad *sld)
{
return const_cast<unsigned char *>((const unsigned char*)(sld->global ? nullptr : object) + (ptrdiff_t)sld->address);
}
struct Settings {
int a;
int b;
int c;
int d;
};
Settings settings;
void setup1() __attribute__ ((optnone));
struct SaveLoad *setup2() __attribute__ ((optnone));
void setup1() {
settings.a = 11;
settings.b = 22;
settings.c = 33;
settings.d = 44;
}
struct SaveLoad *setup2() {
SaveLoad *sl = new SaveLoad();
sl->global = true;
sl->address = &settings.c;
return sl;
}
int test1() {
setup1();
SaveLoad *sl = setup2();
void *ptr1 = GetVariableAddress(nullptr, sl);
return *((int*)ptr1);
}
int test2() {
setup1();
SaveLoad *sl = setup2();
void *ptr1 = GetVariableAddress(&settings, sl);
return *((int*)ptr1);
}
#include <stdio.h>
int main()
{
printf("%d, %d\n", test1(), test2());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment