How to get rid of “unsafe” warnings / errors in Visual Studio (strcpy, sprintf, strdup)

后端 未结 9 1439
臣服心动
臣服心动 2020-12-05 10:43

I\'m trying to get rid of some compiler warnings that say strcpy, sprintf, etc are unsafe. I get why they\'re unsafe, but I can\'t think of a good way to fix the code, in a

相关标签:
9条回答
  • 2020-12-05 11:02

    If this code is only to be compiled for windows platform then it is better to use the secured version of these functions. However, if this code is going to compiled across multiple platforms(linux, Aix etc) then either you can disable the warning in your windows project configuration file (for example, .vcxproj) by using _CRT_SECURE_NO_WARNINGS or, you can use a code snippet like this one in places where those functions have been called in the .cpp file.

    #if _OS_ == _OS__WINDOWS
    //secure function call
    #else
    //already written code
    #endif
    
    0 讨论(0)
  • 2020-12-05 11:04

    as it is suggested in the message, use _CRT_SECURE_NO_WARNINGS to disable this warnings.

    in ProjectProperties -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions, add following macros:

    _CRT_SECURE_NO_WARNINGS

    0 讨论(0)
  • 2020-12-05 11:06

    If portability is not a concern, you can use 'strcpy_s'.

    0 讨论(0)
  • 2020-12-05 11:09

    You do know how much to copy - you allocated space for it!

    Surely you wouldn't willingly copy more than the space you allocated?

    I would prefer to use a method that explicitly avoids buffer overruns by limiting the number of items copied. Back when I was a C programmer we used

    dest = malloc(len);         // note: where did we get len?
    if ( dest is null )  panic!  // note: malloc can fail
    strncpy(dest, src, len);
    dest[len-1] =0;
    

    This is slightly messy, and has been pointed out is using strncpy() a method which really was originally designed for fixed-width fields rather than strings. However it does ach

    There are methods such as strdup() and strlcpy() which may we help.

    My recommendations:

    1). Your target should not be to suppress warnings but to make the code robust.

    2). When copying strings you need to ensure these things:

    • Protect yourself from bad input, for example an unterminated or excessively long string.
    • Protect yourself from malloc failures,
    • Strongly prefer copies of counted numbers of characters to copying until we see a null
    • If you claim to build a string, then make abolsutely sure you null terminate it

    If strlcpy() is available in your environment then you could use it, otherwise why not write your own little utilityy function? Then if there are warnings in just that function you've localised then problem.

    0 讨论(0)
  • 2020-12-05 11:10

    Here is another answer to this question.

    #ifdef _MSC_VER
    #pragma warning(push)
    #pragma warning(disable : 4996)
    #endif
    
            strcpy(destination, source);
    
    #ifdef _MSC_VER
    #pragma warning(pop)
    #endif
    
    0 讨论(0)
  • 2020-12-05 11:11

    In your first example, you know the length already. Since you aren't allocating length+1 bytes I'll assume that length INCLUDES the null terminator. In that case, just std::copy the string: std::copy(extName, extName + length, expList->names[i]);

    In your second example assuming the source strings are null terminated you could compute the destination string length and use std::copy again to concatenate manually, or you could use std::string and the std::copy from the results of c_str into your destination (Again assuming you allocated enough space for it).

    c_str() does not allocate memory that would require external deletion.

    Finally note that sizeof(char) will always be one and so is redundant in your malloc, although the number of bits in that character may not be 8 (See CHAR_BIT).

    0 讨论(0)
提交回复
热议问题