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 spins faster than the drive of my computer after reading many things about how good or bad strncpy and strlcpy .

Note. I know that strlcpy not available everywhere. It's not a problem.

+6
source share
6 answers

strncpy will never be the right answer when your destination string ends in zero. strncpy is a function intended for use with unlimited fixed-width strings. More precisely, its purpose is to convert a zero-terminated string to a string with unlimited length (by copying). In other words, strncpy doesn't make sense here.

The real choice is between strlcpy and plain strcpy .

If you want to perform a “safe” (i.e. potentially truncated) copy to dst_arr , the correct function to use is strlcpy .

As for dst_ptr ... There is no such thing as "copy to dst_ptr ". You can copy to the memory indicated by dst_ptr , but first you need to make sure that it points somewhere and allocates that memory. There are many different ways to do this.

For example, you can just do dst_ptr to point to dst_arr , in which case the answer will be the same as in the previous case - strlcpy .

Or you can allocate memory using malloc . If the amount of allocated memory is guaranteed to be sufficient for the string (i.e., at least strlen(src_str) + 1 bytes is strlen(src_str) + 1 ), you can use plain strcpy or even memcpy to copy the string. There is no need and no reason to use strlcpy in this case, although some people may prefer to use it, as it somehow gives them a sense of added security.

If you intentionally allocate less memory (i.e. want your string to be truncated), then strlcpy becomes the right function.

+20
source

strlcpy () is safer than strncpy (), so you can use it.
Systems that don't have one will often have s_strncpy (), which does the same.

Note that you cannot copy anything to dst_ptr until it points to something

+4
source

I did not know about strlcpy. I just found here that:

The strlcpy () and strlcat () functions copy and concatenate strings, respectively. They are designed to be safer, more consistent, and fewer replacement errors for strncpy (3) and strncat (3).

So strlcpy seams are safer.

Change A full discussion is available here .

Edit2

I understand that what I wrote above does not answer part of your question. If you understand the limitations of strncpy, I think you can use it and write good code around it to avoid its errors; but if you are unsure of your understanding of your limits, use strlcpy.

My understanding of the limitations of strncpy and strlcpy is that you can do something very bad with strncpy (buffer overflow), and the worst thing you can do with strlcpy is to lose one char in the process.

+2
source

You should always use the standard function, which in this case is the C11 strcpy_s() function. Not strncpy() , as this is unsafe without guaranteeing zero termination. Not only OpenBSD strlcpy() , since it is also unsafe, and OpenBSD always offers its own inventions, which usually do not make it into any standard.

See http://en.cppreference.com/w/c/string/byte/strcpy

The strcpy_s function is similar to the BSD strlcpy function, except that strlcpy truncates the source string so that it matches the destination (which is a security risk)

  • strlcpy does not perform all the execution checks performed by strcpy_s
  • strlcpy does not make errors obvious by setting the assignment to an empty string or by calling a handler if the call fails.
  • Although strcpy_s prohibits truncation due to potential security risks, you can truncate a string using bounds-checked strncpy_s.

If your C library does not have strcpy_s , use safec lib. https://rurban.imtqy.com/safeclib/doc/safec-3.1/df/d8e/strcpy__s_8c.html

+1
source

First of all, your dst_ptr does not have allocated space and you should not point it to others, so assigning something to this may lead to a segmentation error.

Strncpy should work fine - just do:

 strncpy(dst_arr, src_str, sizeof(dst_arr)); 

and you know you are not overflowing dst_arr. If you use a larger src_str, you may have to place your null terminator at the end of dst_arr, but in this case your source is <your dest, so it will be filled with zeros anyway.

It works everywhere and is safe, so I would not look at anything other than his intellectual curiosity.

Also note that it would be nice to use a non-magic number for 10 so that you know the size matching the size of strncpy :)

-1
source

you should not use strncpy, not strlcpy for this. Better to use

 *dst_arr=0; strncat(dst_arr,src_arr,(sizeof dst_arr)-1); 

or without initialization

 sprintf(dst_arr,"%.*s",(sizeof dst_arr)-1,src_arr); 

dst_arr here should be an array of NOT pointer.

-1
source

Source: https://habr.com/ru/post/894576/


All Articles