The code below gives a segmentation violation:
#include <stdio.h> #include <string.h> void function(char *str) { char buffer[16]; strcpy(buffer,str); } int main() { char large_string[256]; int i; for( i = 0; i < 255; i++) large_string[i] = 'A'; function(large_string); return 1; }
Compiled and works as follows:
gcc -Wall -Wextra hw.cpp && a.exe
But nothing happens.
Note
The above code really rewrites the ret address, etc., if you really understand what is happening under it.
The ret address will be 0x41414141to be specific.
0x41414141
Attention! This requires deep stack knowledge.
. , ( ). , , , . , , large_string.
large_string
, buffer large_string . , strcpy buffer, large_string, . 255 , , , , large_string ( large_string). , , - .
buffer
strcpy
fluke function . buffer , , , , , , . , , :-). , .
function
, , segfault, , . undefined. , .
[Edit: (GCC cygwin), x86 /. segfault.]
.cpp(++), gcc ( g++)... , , Linux (, Windows - . exe), , , :
/tmp/ccSZCCBR.o:(. eh_frame + 0x12): undefined `__gxx_personality_v0 ' collect2: ld 1
UB (undefined). Strcpy , , .
Strcpy
undefined, , . , .
, - , - () ( / /...), . , , .
, strcpy(), , . strlen(buffer) . undefined.
strcpy()
strlen(buffer)
strlcpy(3) .
strlcpy(3)
:
#include <stdlib.h> int main() { int *a=(int *)malloc(10*sizeof(int)); int i; for (i=0;i<1000000; i++) a[i] = i; return 0; }
SIGSEGV = 37000! ( gdb).
, malloc... mallocs, , , . :
gcc -g -Wall docore.c -o c -lefence
SIGSEGV , i=10, .
i=10
, undefined. , , undefined undefined.
, . . , () . ().
, , , i. , , , - , , , - large_string , i, 0 255. , - , . , large_string 256 257 .
, , , . , , , , strcpy, , , - -.
, , "undefined" "".
char [16] ' , \0. strcpy copy, \0 - , 16 .
Source: https://habr.com/ru/post/1739078/More articles:Setting up the library for inclusion in C ++ - test - c ++visual studio Regex Find / Replace Error - regexHow to check which point is causing the MQ problem? - ibm-mqHow to solve the problem when using jquery datepicker and checking at the same time - jquery-validateIs there a way to determine the c / C ++ library version? - c ++No matter what I do, django-admin.py is not found, although it is in my way - pythonclang compiler does not support -pg - clangPHP portal is similar to java portal - phpCasting in MVVM Light CommandParameterValue - silverlightimport content into drupal - importAll Articles