Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi we know strcpy_s has been introduced as a secure version of strcpy.
It's signature is:
C++
errno_t strcpy_s(
   char *strDestination,
   size_t numberOfElements,
   const char *strSource 
);

But I have a feeling it is still not secure. Although we specify length of destination buffer (e.g., numberOfElements) -- imagine if someone specifies false numberOfElements value, and sets it to 100 say, when the size of destination in reality, is 50? What happens in such a case?

What is your opinion? So I am thinking to evaluate code which contains
C++
strcpy 
functions inside it and must provide some recommendation.
Posted
Updated 14-Nov-12 10:56am
v2

You can only prevent so much stupidity.... with that said, it can't stop you from writing bad code, it prevents you from overwriting a buffer which you've defined... so you should know the size (i.e. it's the size of the destination string or buffer, not the originating string). This function only prevents a large input string from overflowing a destination string.
 
Share this answer
 
As Schiller said: "Against stupidity the gods themselves contend in vain."

strcpy_s is not secure against invalid inputs. It's still worth using instead of the old strcpy, since it's more secure.

There's no absolute security that guards against all concievable invalid parameters, so it's not "secure". It's "more secure".
 
Share this answer
 
As matter of fact, the standard C library (functionally equivalent) function (namely strncpy[^]) it is not 'advertised' as secure.
 
Share this answer
 
Comments
Orjan Westin 15-Nov-12 6:08am    
No, strncpy isn't equivalent, as it doesn't stop at /0 like strcpy and strcpy_s does.

It's intended to solve a different problem, namely copying to fixed-length data fields.

strcpy_s is a standard C library function as of C11. It replaces the common compiler-vendor extension strcpy_r.
CPallini 15-Nov-12 7:52am    
You are right on this (the padding).
In any case it doesn't matter the 'intended purpose' if you can effectively use it to prevent the buffer overflow.
Orjan Westin 15-Nov-12 8:14am    
Using strncpy as a safe strcpy is very likely to lead to other buffer problems, since a null terminator is not given. If you were to do a strlen on the destination string later on, there's no telling what it might be, but you could easily take it at face value and assume that's the length of it. If you take this as a maximum length of that buffer and copy another string to it, you have your buffer overflow.
CPallini 15-Nov-12 8:24am    
Well, you are right again. I stand corrected.
These are the versions with security enhancements. You tagged this C++. This function should not exist in C++ code, it is C, and is made redundant by string classes.
 
Share this answer
 
Comments
Albert Holguin 14-Nov-12 17:29pm    
Unless you see C++ as a superset of C... in which case having C in C++ is not really a problem. Matter of opinion at that point...
Christian Graus 14-Nov-12 17:31pm    
I disagree. C++ was built on C so people would adopt it. It's a bit like saying that because your high school years were built on your primary school years, it's OK to approach a complex problem with only primary school maths. The C part of C++ is utterly redundant today.
Albert Holguin 14-Nov-12 17:35pm    
Like I said... it's a matter of opinion. I know a lot of people still like to use C-style code.
Christian Graus 14-Nov-12 17:37pm    
Mostly because their teachers didn't know C++ but claimed they did, and taught C. Or they learned C first and never learned better
Albert Holguin 14-Nov-12 17:44pm    
My internet access is super slow at work right now... but let's just agree to disagree. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900