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

Confusion of memory in buffer overflow while using strncpy()?

Scope: I have been trying to learn how does buffer overflow occur when using the strncpy function in C and I found this really unusual behavior. I have two character array as arr1 and arr2. I am trying to move 20 bytes of data from the arr1, an…
2
votes
2 answers

strncpy making weird output

I'm trying to make an algorithm for strncpy I read how it work in the manual and made my algorithm then I made a test to see if there's any difference between my ft_strncpy and the real strncpy #include #include char …
AzerSD
  • 148
  • 1
  • 9
2
votes
2 answers

What should be the right usage of strncpy_s() - secure string copy - to handle all the possible corner cases?

I got to know about the function - strncpy_s(), which is called the secure version of string copy. More secure than the functions - strcpy() and strncpy(). strncpy_s() is described more at this link…
Darshan L
  • 824
  • 8
  • 30
2
votes
2 answers

How strcpy changes the value of string

I got this homework to decide what the following code will do (on paper, without testing on computer). char s1[]="Short Message Service", *s2, *s3; s2=strchr(s1,'M'); s3=strchr(s2,'S'); strncpy(s1+1,s2,1); strcpy(s1+2,s3); When I wanted to check…
Blu Dog
  • 31
  • 5
2
votes
2 answers

Value of char array changing, upon calling strcpy on a different char array

When experimenting ways to copy strings to a C style array, due to some weird limitations I stumbled upon this weird behavior, firstly trying to copy the string with std::string::copy() seems to work fine until I'm trying to copy the same string…
AlexG
  • 5,649
  • 6
  • 26
  • 43
2
votes
0 answers

strncpy, GCC gives -Werror=stringop-truncation

I'm reading a file block by block; I want to concatenate the blocks together. For this, I do a strncpy to copy the end of the block into the buffer (TMP_BUF_SIZE is the size of the buffer): strncpy(tmpData, &(data[nextToRead]), TMP_BUF_SIZE…
Phantom
  • 833
  • 1
  • 9
  • 26
2
votes
1 answer

Data Loss when trying to copy char* in C

I have been working on a project in C and I am having trouble when trying to copy char* using strcpy/memcpy/strncpy, none of these seem to work. The problem that is arising is that the words that are around 8 or more characters long are not being…
Brandon T
  • 25
  • 6
2
votes
4 answers

Is it Safe to strncpy Into a string That Doesn't Have Room for the Null Terminator?

Consider the following code: const char foo[] = "lorem ipsum"; // foo is an array of 12 characters const auto length = strlen(foo); // length is 11 string bar(length, '\0'); // bar was constructed with string(11, '\0') strncpy(data(bar), foo,…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
2
votes
1 answer

Relation between calloc char array and null terminating character

This whole "debate" on the web about strncpy being safe vs unsafe is driving me crazy. I find some people saying strncpy is the "devil", which to me sounds like they lack the programming discipline. I get that there is no \0 character added to the…
ThatsRightJack
  • 721
  • 6
  • 29
2
votes
3 answers

How to separate a string into multiple other strings of specific lengths in C?

In main, I pass a string to a different function that is supposed to separate the string, then work with each substring. In this case, I need to take a 30 character string and separate it into substrings of length 7, 5, 5, 7, and 6 to be manipulated…
Reol
  • 21
  • 1
2
votes
2 answers

What is wrong with strncpy_s() here?

I am reading a textbook and trying to solve the question given to readers. The fallowing code is function definition from my source file of my answer. I want to copy content of character strings to another ones. I chose functions strncpy_s(). But it…
Stats Cruncher
  • 155
  • 1
  • 1
  • 6
2
votes
4 answers

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…
Puddler
  • 2,619
  • 2
  • 17
  • 26
2
votes
1 answer

strncpy() alternative - what advatages to each version?

I was thinking of making a strncpy alternative with terminating '\0' for my own use in a header file, and am wondering what of the following approaches would be better. int copystring(char *dest,char *source,int elements) { int run; …
user5940189
2
votes
3 answers

Using strncpy on struct

Let's say I have this student struct defined: struct student { char *name; }; typedef struct student Student Now I have the following function: void add_student(const char *student_name) { // create new student Student *new_s; …
2
votes
3 answers

Strange strncpy response

I just run this code and what i get for n=1 is not what i expect to get. Can you explain why is this happening? #include #include #include #define MAXRIGA 11 int main() { char s[MAXRIGA+2]; char…
Synchronyze
  • 128
  • 1
  • 10