Skip to content

Instantly share code, notes, and snippets.

Use strncpy instead of strcpy: The strncpy function copies a specified number of characters from the source string to the destination buffer, ensuring that it does not write more than the buffer size. However, strncpy has some limitations and may not always behave as expected. It does not guarantee null termination if the source string is longer than the specified number of characters, which can lead to unexpected behavior. To ensure null termination, it is recommended to manually add a null character to the destination buffer after using strncpy.
Use strlcpy or strlcat: The strlcpy and strlcat functions are safer alternatives to strcpy and strcat respectively. They take an additional parameter specifying the size of the destination buffer and ensure that no more than the specified number of characters are copied or concatenated. These functions always null-terminate the destination buffer to prevent buffer overflows. However, it is important to note that strlcpy and strlcat are not standard C library functio