Questions tagged [strncpy]

A C standard library function: strncpy is used to copy a maximum of n characters of non-overlapping, fixed-width or null-terminated strings. Defined also as std::strncpy in C++ standard library.

Used to copy a maximum of n characters of non-overlapping, fixed-width or null-terminated strings. It is defined in the <string.h> C standard header or the <cstring> C++ standard header.

This function is not recommended to use for any purpose, neither in C nor C++. It was never intended to be a "safe version of strcpy" but is often misused for such purposes. It is in fact considered to be much more dangerous than strcpy, since the null termination mechanism of strncpy is not intuitive and therefore often misunderstood. This is because of the following behavior specified by ISO 9899:2011 7.24.2.4:

char *strncpy(char * restrict s1, 
     const char * restrict s2, 
     size_t n);

/--/

3 If the array pointed to by s2 is a string that is shorter than n characters, null characters are appended to the copy in the array pointed to by s1, until n characters in all have been written.

A very common mistake is to pass an s2 which is exactly as many characters as the n parameter, in which case s1 will not get null terminated. That is: strncpy(dst, src, strlen(src));

/* MCVE of incorrect use of strncpy */
#include <string.h>
#include <stdio.h>

int main (void)
{
  const char* STR = "hello";
  char buf[] = "halt and catch fire";
  strncpy(buf, STR, strlen(STR));
  puts(buf); // prints "helloand catch fire"
  return 0;
}

Recommended practice in C is to check the buffer size in advance and then use strcpy(), alternatively memcpy(). Recommended practice in C++ is to use std::string instead.

References:
- ISO/IEC 9899:2011 Information technology — Programming languages — C. Chapter 7.24.2.4.
- Why are strlcpy and strlcat considered insecure?
- What are the C functions from the standard library that must / should be avoided?

Documentation:
C strncpy documentation.
C++ std::strncpy documentation.

253 questions
109
votes
10 answers

Why should you use strncpy instead of strcpy?

Edit: I've added the source for the example. I came across this example: char source[MAX] = "123456789"; char source1[MAX] = "123456789"; char destination[MAX] = "abcdefg"; char destination1[MAX] = "abcdefg"; char *return_string; int index = 5; /*…
Kredns
  • 36,461
  • 52
  • 152
  • 203
98
votes
11 answers

Why does strncpy not null terminate?

strncpy() supposedly protects from buffer overflows. But if it prevents an overflow without null terminating, in all likelihood a subsequent string operation is going to overflow. So to protect against this I find myself doing: strncpy( dest, src,…
Timothy Pratley
  • 10,586
  • 3
  • 34
  • 63
87
votes
8 answers

Why are strlcpy and strlcat considered insecure?

I understand that strlcpy and strlcat were designed as secure replacements for strncpy and strncat. However, some people are still of the opinion that they are insecure, and simply cause a different type of problem. Can someone give an example of…
Anonymous
  • 871
  • 1
  • 8
  • 4
74
votes
5 answers

Why is strncpy insecure?

I am looking to find out why strncpy is considered insecure. Does anybody have any sort of documentation on this or examples of an exploit using it?
stimms
  • 42,945
  • 30
  • 96
  • 149
45
votes
9 answers

gcc-8 -Wstringop-truncation what is the good practice?

GCC 8 added a -Wstringop-truncation warning. From https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82944 : The -Wstringop-truncation warning added in GCC 8.0 via r254630 for bug 81117 is specifically intended to highlight likely unintended uses of the…
JRR
  • 3,024
  • 2
  • 13
  • 37
25
votes
6 answers

strncpy or strlcpy in my case

what should I use when I want to copy src_str to dst_arr and why? char dst_arr[10]; char *src_str = "hello"; PS: my head is spinning faster than the disk of my computer after reading a lot of things on how good or bad is strncpy and strlcpy.…
hari
  • 9,439
  • 27
  • 76
  • 110
24
votes
6 answers

Difference between strncpy and memcpy?

How can I access s[7] in s? I didn't observe any difference between strncpy and memcpy. If I want to print the output s, along with s[7] (like qwertyA), what are the changes I have to made in the following code: #include #include…
user559208
  • 1,485
  • 3
  • 13
  • 9
23
votes
3 answers

Convert zero-padded bytes to UTF-8 string

I'm unpacking several structs that contain 's' type fields from C. The fields contain zero-padded UTF-8 strings handled by strncpy in the C code (note this function's vestigial behaviour). If I decode the bytes I get a unicode string with lots of…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
23
votes
4 answers

'strncpy' vs. 'sprintf'

I can see many sprintf's used in my applications for copying a string. I have a character array: char myarray[10]; const char *str = "mystring"; Now if I want want to copy the string str into myarray, is is better to use: sprintf(myarray, "%s",…
Vijay
  • 65,327
  • 90
  • 227
  • 319
14
votes
6 answers

What is the best alternative to strncpy()?

The function strncpy() doesn't always null terminate so I want to know what is the best alternative that always null terminates? I want a function that if: strlen(src) >= n /*n is the number of characters to be copied from source*/ there's no need…
dems98
  • 814
  • 3
  • 9
  • 22
13
votes
3 answers

Uninitialised value was created by a heap allocation

I have been chasing this bug around, and I just don't get it. Have I forgotten some basic C or something? ==28357== Conditional jump or move depends on uninitialised value(s) ==28357== at 0x4C261E8: strlen (mc_replace_strmem.c:275) ==28357== …
yavoh
  • 2,645
  • 5
  • 24
  • 21
13
votes
3 answers

What is the difference between memcpy() and strncpy() given the latter can easily be a substitute for the former?

What is the significant difference between memcpy() and strncpy()? I ask this because we can easily alter strncpy() to copy any type of data we want, not just characters, simply by casting the first two non-char* arguments to char* and altering the…
Thokchom
  • 1,602
  • 3
  • 17
  • 32
11
votes
6 answers

utf8 aware strncpy

I find it hard to believe I'm the first person to run into this problem but searched for quite some time and didn't find a solution to this. I'd like to use strncpy but have it be UTF8 aware so it doesn't partially write a utf8 code-point into the…
ideasman42
  • 42,413
  • 44
  • 197
  • 320
9
votes
4 answers

strncpy leading to segmentation fault

I am just messing around with strncpy. My program looks like this typedef struct { char from_str[10]; }test; main () { test s1; memset(&s1,0,sizeof(test)); char src[10]="himansh"; char dest[10]; …
Himanshu Gupta
  • 103
  • 1
  • 1
  • 7
7
votes
7 answers

strncpy equivalent for std::string?

Is there an exact equivalent to strncpy in the C++ Standard Library? I mean a function, that copies a string from one buffer to another until it hits the terminating 0? For instance when I have to parse strings from an unsafe source, such as TCP…
athre0z
  • 442
  • 4
  • 12
1
2 3
16 17