On Thu, 15 Apr 2021 at 13:19, Giovanni Mascellani gmascellani@codeweavers.com wrote:
Il 14/04/21 14:57, Henri Verbeet ha scritto:
You pretty much never want to use strcpy(). In this particular case, we already calculated the string length above, and could use memcpy().
Why?
In this case I agree, since you already know the length. But is there a general reason for not wanting to use strcpy?
Because there are broadly two categories of strcpy() usage:
- The ones where you know the length of the string that you're copying. In these cases you can simply use memcpy(). - The ones where you don't know the length of the string you're copying. These are unsafe, because the destination buffer may not be large enough to contain a string of arbitrary length.
There are probably some cases where you know an upper bound instead of an exact length, and using strcpy() kind of works out; hence the "pretty much never" instead of just "never", although even in those cases it may be somewhat questionable whether strcpy() is really worth it.