What happens if I try to access memory beyond a malloc()'d region?

后端 未结 5 1114
死守一世寂寞
死守一世寂寞 2020-12-01 22:15

I\'ve allocated a chuck of memory with char* memoryChunk = malloc ( 80* sizeof(char) + 1); What is keeping me from writing into the memory location beyond 81 un

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 23:19

    Nothing keeps you from writing beyond that bound, and what happens depends on what is beyond that bound. Standard hacker trick (buffer overflow) for hacking programs that don't check and ensure that they do not overwrite buffer limits.

    As mentioned by other posters, you just have to program carefully. Don't use calls like strlen, strcpy - use the length-limited versoins like strncpy etc.

提交回复
热议问题