Why should you use strncpy instead of strcpy?

后端 未结 10 921

Edit: I\'ve added the source for the example.

I came across this example:

char source[MAX] = \"123456789\";
char source1[MAX] = \"12         


        
相关标签:
10条回答
  • 2020-11-22 06:17

    That depends on our requirement. For windows users

    We use strncpy whenever we don't want to copy entire string or we want to copy only n number of characters. But strcpy copies the entire string including terminating null character.

    These links will help you more to know about strcpy and strncpy and where we can use.

    about strcpy

    about strncpy

    0 讨论(0)
  • 2020-11-22 06:19

    The strncpy() function was designed with a very particular problem in mind: manipulating strings stored in the manner of original UNIX directory entries. These used a fixed sized array, and a nul-terminator was only used if the filename was shorter than the array.

    That's what's behind the two oddities of strncpy():

    • It doesn't put a nul-terminator on the destination if it is completely filled; and
    • It always completely fills the destination, with nuls if necessary.

    For a "safer strcpy()", you are better off using strncat() like so:

    if (dest_size > 0)
    {
        dest[0] = '\0';
        strncat(dest, source, dest_size - 1);
    }
    

    That will always nul-terminate the result, and won't copy more than necessary.

    0 讨论(0)
  • 2020-11-22 06:25

    This may be used in many other scenarios, where you need to copy only a portion of your original string to the destination. Using strncpy() you can copy a limited portion of the original string as opposed by strcpy(). I see the code you have put up comes from publib.boulder.ibm.com.

    0 讨论(0)
  • 2020-11-22 06:27

    The strncpy() function is the safer one: you have to pass the maximum length the destination buffer can accept. Otherwise it could happen that the source string is not correctly 0 terminated, in which case the strcpy() function could write more characters to destination, corrupting anything which is in the memory after the destination buffer. This is the buffer-overrun problem used in many exploits

    Also for POSIX API functions like read() which does not put the terminating 0 in the buffer, but returns the number of bytes read, you will either manually put the 0, or copy it using strncpy().

    In your example code, index is actually not an index, but a count - it tells how many characters at most to copy from source to destination. If there is no null byte among the first n bytes of source, the string placed in destination will not be null terminated

    0 讨论(0)
  • 2020-11-22 06:31

    strncpy combats buffer overflow by requiring you to put a length in it. strcpy depends on a trailing \0, which may not always occur.

    Secondly, why you chose to only copy 5 characters on 7 character string is beyond me, but it's producing expected behavior. It's only copying over the first n characters, where n is the third argument.

    The n functions are all used as defensive coding against buffer overflows. Please use them in lieu of older functions, such as strcpy.

    0 讨论(0)
  • 2020-11-22 06:31

    strncpy is NOT safer than strcpy, it just trades one type of bugs with another. In C, when handling C strings, you need to know the size of your buffers, there is no way around it. strncpy was justified for the directory thing mentioned by others, but otherwise, you should never use it:

    • if you know the length of your string and buffer, why using strncpy ? It is a waste of computing power at best (adding useless 0)
    • if you don't know the lengths, then you risk silently truncating your strings, which is not much better than a buffer overflow
    0 讨论(0)
提交回复
热议问题