Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

memcpy

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

char dst[LEN];
memcpy(dst, request_token.c_str(), request_token.size());
like image 284
amazingjxq Avatar asked Dec 28 '22 19:12

amazingjxq


2 Answers

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.

like image 133
Chris Jester-Young Avatar answered Jan 12 '23 04:01

Chris Jester-Young


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.

like image 40
sharptooth Avatar answered Jan 12 '23 04:01

sharptooth