How to get the same strcpy() result with strcpy_s()?

Asked

Viewed 403 times

3

I downloaded a C++ code from the internet and when I open the solution, the following error:

Error C4996 'strcpy': This Function or variable may be unsafe. Consider using strcpy_s Instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

Following the instruction, I modified the strcpy for strcpy_s. But generated other 2 errors:

C2660 'strcpy_s' error: function does not take 2 arguments.

Error (active) E0304 no overworked function instance "strcpy_s" corresponds to the list of arguments.

Why did the first error occur? What am I doing wrong?

Code:

strcpy((char*)m_pOriginalCVar->pszName, m_szDummyName);
  • What is the type of m_szDummyName?

  • @bfavaretto char[128]

  • Like the mustache said strcpy is passive from bufferoverflow attacks and so created the strcpy_s that copies only a specific font size

1 answer

3


It would be something like that:

strcpy_s((char*)m_pOriginalCVar->pszName, /*tamanho aqui*/, m_szDummyName);

I put in the Github for future reference.

This size should be the size of (char*)m_pOriginalCVar->pszName or the size of m_szDummyName, usually the one that is smaller and that makes more sense, if it is bigger the function does not make miracle and error of the programmer in the good choice will bring the same consequences of the insecure function.

You may have other things to look at to ensure that the function can perform the safe copy, the function will not do anything unsafe, and the programmer must ensure that everything is in order. The question has no details that can help.

The error occurred because strcpy() is insecure.

Documentation. And in the microsoft documentation.

  • Sorry, I misspelled, actually I meant with _s, I believe that it should be: strcpy_S((char*)m_pOriginalCVar->pszName, /*tamanho aqui*/, m_szDummyName);

  • Yeah, I was wrong, I guess now ok :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.