3 ms·
Been a while since I used C++... How does this work if the size of the destination isn't known at compile time? I.e., it's not "buffer[5]”?
by mcguire 8y ago
Been a while since I used C++...
How does this work if the size of the destination isn't known at compile time? I.e., it's not "buffer[5]”?
- yarosv 8y agoThe author states > The focus of this post is on fixed-size string buffers, but the technique applies to any type of fixed-length buffer.
- brucedawson 8y agoYou get a compile error. And this is progress. I have fixed code that looked something like this: strncpy(dst, sizeof(dst), src); data[sizeof(dst)-1] = 0; See the bug? Of course not, because it's not visible, but I've seen this pattern used where dst is a pointer. This means that four or eight bytes (three or seven if you don't count the null terminator) gets copied, regardless of how many bytes dst points to. With strcpy_safe an attempt to copy to dst would not work and the developer would have to fix their code. Every mistake that can be made, will be made.