开发者

Is memcpy() safe when copy some content larger than dst?

开发者 https://www.devze.com 2023-03-11 20:57 出处:网络
Is it safe when the request_token.size() is larger than LEN? char dst[LEN]; mem开发者_JAVA百科cpy(dst, request_token.c_str(), request_token.size());

Is it safe when the request_token.size() is larger than LEN?

char dst[LEN];
mem开发者_JAVA百科cpy(dst, request_token.c_str(), request_token.size());


No, it's not safe; you'll cause a buffer overflow. The reason is, memcpy has no way to know the size of your target buffer, other than the size you pass in the third argument.


No, definitely not, that will lead to buffer overrun and trigger undefined behavior which will lead to all sorts of bad things, included but not limited to data corruption and program crashing.


No, it is not safe. Read this: http://msdn.microsoft.com/en-us/library/dswaw1wk(VS.80).aspx

Use memcpy_s(): http://msdn.microsoft.com/en-us/library/dswaw1wk(VS.80).aspx


No, you will end up in trying to write to invalid memory locations which causes undefined behavior.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号