简体   繁体   中英

Can I write value to next address of pointer by overflow

I want to write value to (p + 1) through p but I don't know if it is possible in x86_64.

uint8_t *p = (uint8_t*) malloc(2);
*p = 0x100F;

printf("%d %d", *p, *(p+1));

When I try to print the addresses of them I got what I expected:

printf("%d %d", p, p+1);
Output: 12934432 12934433

but in the first printf, I can not get the output what I expected. I want to get an output like 15 16 . If it is possible how can I do?

to avoid aliasing problems

memcpy(p, &(uint16_t){0x100F}, 2);

or

p[0] = 0x0f;
p[1] = 0x10;

//or 

#define LO(x)   ((x) & 0xff)
#define HI(x)   (((x) >> 8) & 0xff)

p[0] = LO(0x100f);
p[1] = HI(0x100f);

You can do it via type-punning:

*(uint16_t *)p = 0x100F;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM