ssylee said:
What kind of operations with memcpy usually result in segmentation
errors? Anything that I've missed from the following:
a) pointer manipulation of memory that is not allocated
b) improper pointer dereferencing
Thanks.
You've got 3 parameters in memcpy. So, any kind of error on each of them
may lead to a segmentation fault.
1. First parameter:
1.1. Destination memory area not readable/writable:
char *dest = 0x0;
memcpy (dest, "lol", 3);
1.2. No check on destination memory area allocation:
char *dest = (char*) malloc(0xFFFFFFFF); // Allocation fails
memcpy (dest, "lol", 3);
2. Second parameter:
See 1.
3. Third parameter:
3.1. Size is negative. memcpy's third parameter is unsigned int.
So, a negative parameter will be considered as its positive 2's
complement.
memcpy (dest, src, -1); // -1 == 0xFFFFFFFF -> size too large
3.2. Size is bigger than what dest may contain
char dest[4];
memcpy (dest, "lolwut?", 7);
Hope I've answered your question...
--
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/CM/CC/E/IT/LS/M d-(--) C++++$ UBL++++$ P++++ L+++++$ E--- W+++ w--
PS+++ PE-- Y++ PGP+++ R++ tv-- b++>+++ D+ G>+++ e++>+++++ h* r++ z+++
------END GEEK CODE BLOCK------