![]() |
![]() |
![]() |
Goodbye strcpy Of course, not until Whidbey or VS 2005.
BSharper
If you still use strcpy(), strcat(), strncpy(), or strncat(), then you have no business writing C code.
Kassel v. Consolidated Freightways Corporation
MS is making the right decision, absolutely. strcpy should have been deprecated long ago along with scanf and all the others. Any review of strange code should include a search for all these functions and elimination of them.
Dennis Atkins
Hm, that's interesting about strncpy. I have a wrapper of it myself that always puts the NUL at the end because I did note the bizarre point in its doc that it does not always truncate (doh how stupid is that anyway obviously a bug bf Dennis Ritchie that should have been fixed don't tell be that someone relies on that behavior). However, I am quite skeptical that all strncpy's fill the remainder of the string with NULs; I have never heard of that. Seems to be precisely less than useless when coupled with the behavior that at least one NUL is not guaranteed.
Dennis Atkins
http://www.opengroup.org/onlinepubs/007908799/xsh/strncpy.html
A small white object.
The filling up with NULs is mandated by ISO C.
a2800276
Actually, what bugs me is, oracles pro*c precompiler changes strlcpy and strncpy to strcpy. In fact, it removes the line argument in the function too, so it must be aware of it removing this, so I have to run it through my post-oracles-precompiler , annoying.
fw
What about the 'mem...' functions then?
sedwo
strcpy has its uses. You just have to be very careful where and how you use it. Not every use is a possible buffer overflow. Worrying too much about it misses the point: the safe functions also have to be used carefully and correctly or you'll run into problems with them too.
Sum Dum Gai
I would like to point out that those deprecated functions can be used safely, when string lengths are known, properly sized buffers are used, and the programmer takes pains to ensure that there is a terminating NULL. It's a nuisance that the replacement functions make easier, but the business of calculating the length of strings to make sure you allocate a big enough buffer doesn't really go away. They just make sure that you won't overflow and you will be NULL terminated.
Clay Dowling
You can say "use it correctly" all you want, but using strcpy correctly basically results in you rewriting strncpy all ofer the place.
Tito
Nearly every C++ program in the real world has to deal with C libraries that expect C style strings. You can't just use std::string exclusively, so it's still a very real issue for C++ code.
Sum Dum Gai
"oracles pro*c precompiler changes strlcpy and strncpy to strcpy"
Dennis Atkins
<pedant>
Dennis Atkins
I understand clay's point but agree with Tito - strlcpy should be used with sizeof(bufername) when fixed size buffer is involved. This is because buffer lengths have a tendency to get changed during maintenance, as do constant string lengths. Any 100 line program written by you today is a candidate for changes three years from now when the programmer who wrote it is not around and the code needs to be translated into Cherokee.
Dennis Atkins
template <class T> T NULL() { return static_cast<T>(0); }
Are you serious? Is that from a real C++ distribution? If that is really the official way to write 0 in C++ I am going to delete my C++ compiler and go live in the wilderness, husking cocoanuts.
Dennis Atkins
That is definately not the "official" way to write NULL. It's still #define'd to be 0.
Tito
"For me though, I'll deal with the code being safe when it becomes production code, and not before, as I find it's a time consuming waste if you don't need it. I only bother up front if I KNOW it's going to be production code to start with. "
MikeMcNertney
|