Daniel Stenberg, the author of the tool for sending and receiving data over the network, curl, announced the discontinuation of the use of the strcpy() function in the codebase of the project and a ban on its use going forward. This decision continues last year's initiative to abandon the use of strncpy(), which copies a specified number of bytes from the input string. The use of strncpy() posed a risk of errors due to the potential omission of the null character at the end of the string or unnecessary zero-padding.
Calls to strncpy() were replaced with the strcpy() function, which involved allocating memory for the target buffer considering the size of the source string, or checking the match between the size of the source string and the target buffer. A replacement with the strlcpy() function was not made because it was required to always copy the entire string or return an error.
Now all calls to strcpy() have been replaced with the new function curlx_strcopy(dest, dsize, src, slen). The curlx_strcopy() function requires the specification of the sizes of the source and target buffers, ensuring that the target buffer must be larger than the source buffer to accommodate the null character at the end, which is forcibly added by the function to prevent its omission during copying. If the size of the target buffer is greater than zero but insufficient for copying the target string, a null byte is added at the beginning. void curlx_strcopy(char *dest, size_t dsize, const char *src, size_t slen) { DEBUGASSERT(slen < dsize); if(slen < dsize) { memcpy(dest, src, slen); dest[slen] = 0; } else if(dsize) dest[0] = 0; }
The replacement of strcpy() with curlx_strcopy() has been made due to the potential for human error leading to the separation of code that performs checking/memory allocation and a call to strcpy(), for instance, when thoughtlessly transferring only part of the code or inserting code between the check and the call to strcpy(). Furthermore, discontinuing the use of strcpy will help eliminate the stream of false vulnerability reports due to incorrect triggers from AI tools that consider the presence of strcpy() a vulnerability without taking into account the checks present in the code.
Izvor: opennet.ru
