How does runtime detect buffer overflow?

{
char bufBef[32];
char buf[8];
char bufAfter[32];
sprintf(buf,"AAAAAAA\0");
buf[8]='\0';
printf("%s\n",buf);
}

In Windows 7, I compiled the program with Visual Studio 2008 as a debugging project. 3 buffers are adjacent. I find their addresses with the debugger, as follows:

bufBef           0x001afa50 

buf                  0x001afa40

bufAfter          0x001afa18 

The buf [8] = '\ 0' โ€statement writes the address from buf. When I run the program, the operating system reportedโ€œ Debugging error: execution verification error at run time # 2. โ€Damage from the buf variable was damaged.

Then I compiled it as a release project. It works quietly, the error report does not rise.

My question is, how is buffer overflow at runtime?

+3
source share
10 answers

, /RTCs.

Microsoft.NET Microsoft Windows .

:

, Microsoft /RTC , . , - 0xCC.

, ( ).

+3

, . , , , .

, โ€‹โ€‹ ( , ); , , . , ; .

+3

, , . . , . , -, .

+2

. buf bufBef ( ) 16 , buf 8 .

8 , -, 8- "". , , , .

( buf[8] 0x001afa48, buf bufBef).

+2

.

+2

. , , .

+1

0x001afa50 - 0x001afa40 = 0x10 = 16 0x001afa40 - 0x001afa18 = 0x28 = 40, , . , , . - , , -, .

+1

C ( ) , .

n- ( ).

0

, C. , "" . C "" undefined, , , / , undefined, , , , /.

0

Source: https://habr.com/ru/post/1777005/


All Articles