June 2nd, 2005

If strncpy is so dangerous, why does Visual Studio 2005 still support it?

In response to the news that strncpy is so dangerous, at least one person has called for Visual Studio to revoke support for such a dangerous function, considering the continued support for the function grounds for holding the compiler manufacturer liable for any defects in programs compiled with that compiler. Well, for one thing, while it’s true that strncpy is dangerous if used improperly, it is still a valid function, and my original discussion explained the history behind strncpy and the very specific scenario in which it is still useful. It just so happens that most people don’t use the function in the manner it was intended, but instead treat it as a sort of “copy string with a character limit” function, which it isn’t really. For another thing, just because something is dangerous doesn’t mean it shouldn’t be supported. Pointers and casts are dangerous, but I don’t see them disappearing from C or C++ any time soon.

Third, support for strncpy is mandated by the C standard. If you removed it, you couldn’t call yourself a C compiler any more. (Not to mention breaking compatibility with existing source code that uses the strncpy function. How would you like it if you bought a so-called C compiler and found that it couldn’t compile a large class of valid C programs?)

Topics
Other

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.