17

I am getting this warning but all functions working properly .

what does this really means?

'strcpy': This function or variable may be unsafe. 
Consider using strcpy_s instead. To disable deprecation, 
use _CRT_SECURE_NO_WARNINGS. See online help for details.
14
  • 11
    @GMan: Compilers don't issue warnings "just for fun". There's a reason for these warnings and ignoring them is generally a bad idea. If the compiler is already telling you about a problem you should really listen to it. In this case it's a warning about a potential security problem (or simply a potential source for crashes) and asks you to use a more sane function instead.
    – DarkDust
    Oct 25, 2010 at 6:29
  • 8
    @DarkDust: Apart from the fact that I wouldn’t ever (!) use strcpy (or strcpy_s), using the normal C functions is perfectly fine, and using the “secure” CRT variants only gives the illusion of security, nothing more. What’s more, they’re not portable. Oct 25, 2010 at 6:32
  • 1
    @DarkDust: I believe this kind of warnings is useful for beginner or lazy programers that usually don't care enough about string boundaries. If you know for sure you have a NULL terminated string (not from user input), I can't see why we should avoid the use of a perfectly working function.
    – ereOn
    Oct 25, 2010 at 6:33
  • 2
    @ereOn: Many C programmers (e.g., Torvalds) tend to avoid all C string functions because they are unnecessary and inefficient: use memcpy instead.
    – Philipp
    Oct 25, 2010 at 6:37
  • 5
    @DarkDust: Oh, I'm well aware many warnings are important. But not all of them are, and this is certainly a damn annoying warning. Sometimes, believe it or not, programmers use these functions in a completely safe manner. @Gabe: Because CERT somehow defines the standard...?
    – GManNickG
    Oct 25, 2010 at 6:41

10 Answers 10

22

This function (strcpy) is considered unsafe due to the fact that there is no bounds checking and can lead to buffer overflow. (Actually strcpy is infamous for overflow exploits and all programmers avoid it-or at least should avoid it). The advice is to use a safe function which takes into account the size of the destination buffer to avoid overflow. You could also use strncpy (BUT with caution!). There is no problem with your code, i.e. the functions will run as you say but try giving as input a buffer that is larger than the destination buffer. The function will overflow the destination buffer. Check this also link text

16
  • Well, all programmers avoid it to deal with user input without checkings. But in a context where you know for sure your string is NULL terminated, you can safely use it.
    – ereOn
    Oct 25, 2010 at 6:30
  • 3
    @ereOn:The problem is that standard C functions are unaware of the size of the destination buffer.Even if the input src buffer is properly NULL terminated, there is no way to stop the copy if the destination buffer is of smaller size.This has to be handled either by the calling function or a safe version of a strcpy function MUST be used. You are right that dealing with user input is a big issue. BUT overall still it is better to refrain using strcpy alltogether in all cases.
    – Cratylus
    Oct 25, 2010 at 6:55
  • 3
    Do not use strncpy(); it does not guarantee null-terminated strings, and if used to copy a short string into a large buffer it zealously zeroes out the unused part of the buffer, which is seldom really necessary. It was fine for its original job (copying up to 14 characters into a directory entry in the old Unix file system) but not really for much else. Oct 25, 2010 at 7:08
  • 1
    @user384706: memcpy isn't aware of NUL, but I the programmer am. If I'm writing a safer replacement for strcpy, then I'm going to do it at most once per project (probably less often, since in most C++ projects you can just use std::string), and I can afford to pay attention to both buffer lengths and NUL-termination, and get it right. Any such function written using strncpy would be better written using memcpy, or perhaps with no library calls at all, unless written by someone with no business programming C++ in the first place who would have messed up the memcpy version. Oct 25, 2010 at 12:04
  • 1
    I guess what I'm saying really is, that strcpy is "unsafe" when used a lot, because eventually you'll make a mistake. It's not "unsafe" when used once per project in a well-tested helper function. Or if it is, then the author doesn't need a CERT advisory telling him not to use strcpy, he needs a CERT advisory telling him not to use C or C++. Oct 25, 2010 at 12:06
11

While strcpy is a common string function, it has a history of being the source of many bugs and security holes in software (due to the ease of buffer overflows).

Microsoft, in an effort to promote safer coding in C and C++ has provided a suite of replacement functions for the dangerous string methods. Typically they have the original name postpended with _s. Hence the Microsoft secure version of strcpy is strcpy_s as recommended in the warning. Note this a Microsoft specific feature, it's not ubiquitious.

You've got a few options.

  1. DEFINE _CRT_SECURE_NO_WARNINGS if you don't want to care about it, leaving the possibility of the security issues in your software.
  2. Replace your string functions with the secure ones, leaving your software less portable as a consequence
  3. Wrap the secure string functions and use the wrappers everywhere, providing enhanced security on Windows platforms, and falling back to the traditional versions on other platforms. The wrapper functions could be via a MACRO or compiled functions.

I typically do #3.

4
  • 6
  • I'm sure it's still possible to make mistakes, but did you have a specific example in mind?
    – Montdidier
    Oct 25, 2010 at 6:35
  • Have you checked the link James McNellis posted in his comment ?
    – ereOn
    Oct 25, 2010 at 6:37
  • Oh. My eyes aren't working today. Ok, so there is a bug in some versions of Visual Studio.
    – Montdidier
    Oct 25, 2010 at 6:39
10

Since you’re programming C++, the correct solution is to ban C-style char* strings from your code where possible, and replace them by std::string (or another appropriate string type).

Do not use functions such as strcpy or strcpy_s or strncpy. Use the copy constructor or assignment operator of the string class. Or if you really need to copy buffers, use std::copy.

0
8

Since VC++ 8 strcpy() and a huge set of other functions are considered to be unsafe since they don't have bounds checking and can lead to a buffer overrun if misused.

You have two options:

  • if you're unsure - do what VC++ says and use "safe" functions. They will trigger an error handler that will terminate your program if something goes wrong.
  • if you know what you're doing - you know that no overrun will ever occur and all edge cases are handled by your code - define _CRT_SECURE_NO_WARNINGS prior to including CRT headers and this will make the warning go away.
1
5

There is actualy a way to avoid this warning, still use strcpy, and be safe:

You can enable the secure template overloads. They will (if possible) deduce the lengths of the buffers used by capturing them with templated overloads. It's a mystery to me why this is not enabled by default in Visual C++.

4

That warning is basically informing you that strcpy is deprecated, because copying a string until \0 can easily lead to nasty problems (buffer overruns). The reason strcpy is still there and works is that it is part of the standard library legacy, but you should really consider using str*_s or strn* functions (which don't exclusively rely on finding the terminating \0).

Since buffer overruns are linked not only to security problems, but also to bugs which are relatively difficult to trace and fix, using plain vanilla str* functions is not only generally frowned upon, but can lead to people rejecting your code as inherently unsafe.

More details: http://www.safercode.com/blog/2008/11/04/unsafe-functions-in-c-and-their-safer-replacements-strings-part-i.html

1
  • Vladski: Your link explicitly says that the strn* functions you recommend are prone to problems.
    – Gabe
    Oct 25, 2010 at 7:20
2
#pragma warning(disable: 4996)

use above code in the first line of your code.

1
  • 1
    This does not answer the question, it just suppresses the warning
    – slfan
    Aug 29, 2015 at 10:43
1

If you have looked at the pros and cons of using C++ purist technique vs. not worrying because you 'know' your strings will be zero terminated, then you can also disable the warning in msvc, this sort of thing:

#ifdef _MSC_VER
  // 4231: nonstandard extension used : 'extern' before template explicit instantiation
  // 4250: dominance
  // 4251: member needs to have dll-interface
  // 4275: base needs to have dll-interface
  // 4660: explicitly instantiating a class that's already implicitly instantiated
  // 4661: no suitable definition provided for explicit template instantiation request
  // 4786: identifer was truncated in debug information
  // 4355: 'this' : used in base member initializer list
  // 4910: '__declspec(dllexport)' and 'extern' are incompatible on an explicit instantiation
#   pragma warning(disable: 4231 4250 4251 4275 4660 4661 4786 4355 4910)
#endif
0
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1 
#define _WINSOCK_DEPRECATED_NO_WARNINGS 1 

at the top of the file worked for me (based on other SO user's answer... but I couldn't find to ref him/her)

-1

use Secure Template Overloads or define wrapper functions not work for dynamically allocated buffers, so this attempt is futile.
Either modify source to use secure replacement, or just ignore it.

if the codes are writing by yourself, you had better change such strcpy to strcpy_s etc. if the modules are imported from trusted soures, you may choose to ignore the warning.

ignore method 1: project globle scope: add _CRT_SECURE_NO_WARNINGS
ignore method 2: ignore particular module: if only one or two of them, then you could simplely forbit warning for these modules when include them:

#pragma warning(push)
#pragma warning(disable: 4996)
#include <sapi.h>  //legacy module
#include <sphelper.h> //legacy module
#pragma warning(pop)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.