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
3
votes
1 answer

gcc-8 Wstringop-truncation

I'm trying to fix some C code where gcc-8 complains about Wstringop-truncation (code is here) When compiling that code on a server which I can not control neither can add pragma statements nor can disable Wstringop-truncation diagnostics, the…
user1600826
3
votes
3 answers

how to use strncpy correctly?

I know strncpy is a safer version of strcpy as said here. However, when I want to copy from src to dst and dst is not a clean buffer, I get unwanted results, which can be avoided by strcpy: char *p = "123"; char a[10] =…
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
3
votes
3 answers

c strncpy null terminated or not

I am reading this document, it says: char *strncpy(char *destination, const char *source, size_t num); Copy characters from string Copies the first num characters of source to destination. If the end of the source C string (which is signaled by…
beetlej
  • 1,841
  • 4
  • 13
  • 27
3
votes
3 answers

What is the trick behind strcpy()/uninitialized char pointer this code?

#include #include #include void main () { char *imsi; unsigned int i; int val; char *dest; imsi = "405750111"; strncpy(dest,imsi,5); printf("%s",dest); /* i = 10; */ } In the above code, with…
3
votes
4 answers

Changing static array

I have a static variable declared in a file: static char *msgToUser[] = { "MSG1 ", "MSG2 ", }; Inside one of the methods of a class I'm doing this: void InfoUser::ModifyMsg( BYTE msgIdx, char *msgString ){ …
Megacan
  • 2,510
  • 3
  • 20
  • 31
3
votes
1 answer

cppcheck - terminateStrncpy

New to cppcheck. Couldn't figure out how to solve this issue (cppcheck warning). any help would be appreciated. if (!call_initialized) { char id1[16]; char id1[16]; char* dummy_char_ptr = inet_ntoa(*((in_addr*)&source_ip)); …
H.Jerry
  • 31
  • 1
  • 7
3
votes
3 answers

Difference in memcpy and strncpy for struct copying

I have the below code. I am trying to copy a struct to a string. I want to understand why the output varies between strncpy and memcpy. #include #include struct a{ int len; int type; }; int main(){ struct a…
mayur
  • 33
  • 6
3
votes
1 answer

format ’%s’ expects argument of type ’char *’

For exercising my programming skills in C I'm trying to write the strncpy function by myself. Doing that I kept hitting errors, solving most of them eventually I'm stuck with no further inspiration to go on. The error I receive is: ex2-1.c:29:3:…
mjrc
  • 43
  • 1
  • 1
  • 5
3
votes
2 answers

strncpy() and memcpy() are different?

strncpy() and memcpy() are the same? Because of the fact that strncpy() only accepts char * as parameter, Icasts the integer arrays to char *. Why it gets the different output? Here is My code, #define _CRT_SECURE_NO_WARNINGS #include…
C learner
  • 53
  • 2
  • 9
3
votes
1 answer

How to strncpy() to WCHAR

I got a piece of code that works under Multi-Byte Character Set. However, I want to convert this piece of code to UNICODE. So I fixed lots of stuff, but failed at the strncpy() line. This is the line that I want to change: strncpy(a.szTip,…
3
votes
4 answers

C++ copy const char* to char*

I have a function ClassA::FuncA(const char *filePath) and want to copy this const char string* to a char*! My solution: char *argv[2]; int length = strlen(filePath); argv[1] = new char(length +1); strncpy(argv[1], filePath, length); after this I…
leon22
  • 5,280
  • 19
  • 62
  • 100
3
votes
2 answers

copy character from string to another string in C

I have a string AAbbCC what I need is to copy the first two and add them to an array then copy the middle two and add them to an array and finally the last two and add them to an array. this is what I do: char color1[2]; char color2[2]; char…
Markus
  • 686
  • 1
  • 11
  • 18
2
votes
5 answers

strncpy introduces funny character

When I run some code on my machine then it behaves as I expect it to. When I run it on a colleagues it misbehaves. This is what happens. I have a string with a value of: croc_data_0001.idx when I do a strncpy on the string providing 18 as the…
Dunc
  • 7,688
  • 10
  • 31
  • 38
2
votes
3 answers

strncpy segfault

I've been having trouble getting this section of code to work. I'm trying to get a character array to be copied so I can get a count of how many tokens there are to dynamically allocate and save them to be examined for environment variables.…
GFXGunblade
  • 97
  • 3
  • 10
2
votes
0 answers

Wrong destination size for strncpy

Visual Studio Code 1.73.1, PlatformIO 6.1.5, toolchain-xtensa-esp32 8.4.0. In a third-party library there are these lines of code: #define TZNAME_MAX_LEN 50 #define TIMEZONE_MAX_LEN 50 typedef struct { WiFi_Credentials…
Mark
  • 4,338
  • 7
  • 58
  • 120
1 2
3
16 17