Why using memcpy can cause to stack overFlow?

memcpy get the number of bytes to copy, so what is the problem , and how memcpy_s fix it?


1 Answer 1


A buffer overflow happens when a program tries to write too much data into a buffer, or other data structure. The extra data "overflows" the buffer writes over the data structure immediately following the buffer in memory. memcpy allows you to specify the number of bytes to copy, but if that value varies, such as when it is dependent on user input, then your destination buffer might not be big enough. The wikipedia page has a good example.

memcpy_s has an additional parameter, where you can specify the destination buffer size. This allows memcpy_s to perform additional checks on the input, and error if the number of bytes to copy is greater than the destination buffer size.

For a good introduction to exploit analysis, see this course material and videos. Full disclosure: I am a contributor to that website, though not to that particular class material.

Not the answer you're looking for? Browse other questions tagged or ask your own question.