-3

I'm wondering why does the following code causes an execution error?

#include <stdlib.h>
int main(void) {
    asm("pushf\norl $ 0x40000, (%esp)\npopf\n");
    *((int*) (((char*) malloc(5)) + 1)) = 23;
    return 0;
}

Thanks !

Casper Beyer
  • 2,203
  • 2
  • 22
  • 35
user3020233
  • 327
  • 3
  • 10
  • 2
    Q: What did you find when you stepped through it under the debugger? Q: What is your platform? Compiler? I'm guessing Linux and GCC, but I don't know... What exactly are you trying to accomplish with this code? – FoggyDay Apr 21 '14 at 03:16
  • 2
    You are setting the alignment-check bit in EFLAGS [(More)](http://stackoverflow.com/questions/548164/mis-aligned-pointers-on-x86), and writing a 32-bit value to an unaligned address. BTW, 'execution error' is pretty vague. The actual error would be more useful. – Brett Hale Apr 21 '14 at 04:23
  • If `sizeof(int)` larger, like 8, then not enough memory allocated. – chux - Reinstate Monica Apr 21 '14 at 04:43

1 Answers1

0

Probably because you are modifying alignment check flag, which is AFAIK protected. Or because malloc() returns an aligned region, and your access is unaligned? Not sure, but why do you need to touch this flag at all?

valplo
  • 693
  • 5
  • 13