3 ms·
Should just be using memcpy and not strcpy, or strncpy (which is definitely the worst option of the three). The code already knows the size of the source buffer
by Arch-TK 3y ago
Should just be using memcpy and not strcpy, or strncpy (which is definitely the worst option of the three). The code already knows the size of the source buffer and has checked if it will fit in the destination buffer, there is no reason to call strcpy and have it strlen the string unnecessarily.
Genuinely the LLM recommendation is something I would actively discourage. If you don't know the size, and don't care about silent truncation, and don't care about performance (strncpy will unnecessarily zero the rest of the buffer), just use snprintf. Unless you're dealing with some UI or something, you probably SHOULD care about truncation and in that case strncpy won't save you.