Puddler
Puddler

Reputation: 2939

Strncpy should only be used with fixed length arrays

According to this StackOverflow comment strncpy should never be used with a non-fixed length array.

strncpy should never be used unless you're working with fixed-width, not-necessarily-terminated string fields in structures/binary files. – R.. Jan 11 '12 at 16:22

I understand that it is redundant if you are dynamically allocating memory for the string but is there a reason why it would be bad to use strncpy over strcpy

Upvotes: 2

Views: 3198

Answers (4)

Md. Monirul Islam
Md. Monirul Islam

Reputation: 731

char * strncpy ( char * destination, const char * source, size_t num );

Limitations of strncpy():

  1. It doesn't put a null-terminator on the destination string if it is completely filled. And, no null-character is implicitly appended at the end of destination if source is longer than num.
  2. If num is greater than the length of source string, the destination string is padded with null characters up to num length.

Like strcpy, it is not a memory-safe operation. Because it does not check for sufficient space in destination before it copies source, it is a potential cause of buffer overruns.

Refer: Why should you use strncpy instead of strcpy?

Upvotes: 0

Loc Tran
Loc Tran

Reputation: 1180

We have 2 versions for copy string from one to another 1> strcpy 2> strncpy These two versions is used for fixed and non-fixed length array. The strcpy don't check the upper bound for destination string when copy string, strncpy will check it. When the destination string is reached to this upper bound, the function strncpy will return error code, in the meantime the function strcpy will cause some effect in memory of the current process and terminate the process immediately. So that the strncpy is more secure than strcpy

Upvotes: -1

Jerry Coffin
Jerry Coffin

Reputation: 490108

strncpy will copy data up to the limit you specify--but if it reaches that limit before the end of the string, it'll leave the destination unterminated.

In other words, there are two possibilities with strncpy. One is that you get behavior precisely like strcpy would have produced anyway (except slower, since it fills the remainder of the destination buffer with NULs, which you virtually never actually want or care about). The other is that it produces a result you generally can't put to any real use.

If you want to copy a string up to a maximum length into a fixed-length buffer, you can (for example) use sprintf to do the job:

char buffer[256];

sprintf(buffer, "%255s", source);

Unlike strncpy, this always zero-terminates the result, so the result is always usable as a string.

If you don't want to use sprintf (or similar), I'd advise just writing a function that actually does what you want, something on this general order:

void copy_string(char const *dest, char const *source, size_t max_len) { 
    size_t i;
    for (i=0; i<max_len-1 && source[i]; i++)
        dest[i] = source[i];
    dest[i] = '\0';
}

Since you've tagged this as C++ (in addition to C): my advice would be to generally avoid this whole mess in C++ by just using std::string.

If you really have to work with NUL-terminated sequences in C++, you might consider another possibility:

template <size_t N>
void copy_string(char const (&dest)[N], char const *source) {
    size_t i;
    for (i=0; i<N-1 && source[i]; i++)
        dest[i] = source[i];
    dest[i] = '\0';
}

This only works when the destination is an actual array (not a pointer), but for that case, it gets the compiler to deduce the size of the array, instead of requiring the user to pass it explicitly. This will generally make the code a tiny bit faster (less overhead in the function call) and much harder to screw up and pass the wrong size.

Upvotes: 4

Serdalis
Serdalis

Reputation: 10489

The argument against using strncpy is that it does not guarentee that your string will be null terminated.

The less error prone way to copy a string in C when using non-fixed length arrays is to use snprintf which does guarentee null termination of your string.

A good Blog Post Commenting on *n* functions.

These functions let you specify the size of the buffer but – and this is really important – they do not guarantee null-termination. If you ask these functions to write more characters than will fill the buffer then they will stop – thus avoiding the buffer overrun – but they will not null-terminate the buffer.

Which means that the use of strncpy and other such functions when not dealing with fixed arrays introduces unnessisary risk of non-null terminated strings which can be time-bombs in your code.

Upvotes: 2

Related Questions