c++ - How to get rid of "unsafe" warnings / errors in Visual Studio (strcpy, sprintf, strdup) -
i'm trying rid of compiler warnings strcpy, sprintf, etc unsafe. why they're unsafe, can't think of way fix code, in c++ style.
here's excerpt of code:
extlist->names[i]=(char *)malloc(length*sizeof(char)); strcpy(extlist->names[i],extname); // unsafe // strncpy(extlist->names[i],extname,length); // unsafe
here's message:
c4996: 'strcpy': function or variable may unsafe. consider using strcpy_s instead. disable deprecation, use _crt_secure_no_warnings. see online details.
i can't think of safe way copy data on in c++ without knowing length of stuff copy. know there's strlen(), that's unsafe since assumes (maybe incorrectly) data null-terminated.
also:
// used concatenate: sprintf(extstr,"%s%s",platextstr,glextstr);
c4996: 'sprintf': function or variable may unsafe. consider using sprintf_s instead. disable deprecation, use _crt_secure_no_warnings. see online details.
using std::string concatenate easy enough, need data extstr somehow (and not using strcpy, lol). string::c_str() function returns pointer un-modifiable data, can't set extstr equal it. (and i'm not sure if c_str() pointer needs delete called on later? allocate space using "new"?)
any advice on stuff? part of 10,000 line file that's not mine... i'm not keen on re-writing thing in c++ way.
you don't need pragmas disable them.
for win32/msvc, in projectproperties -> configuration properties -> c/c++ -> preprocessor -> preprocessor definitions, add following macros:
_crt_secure_no_deprecate _crt_nonstdc_no_deprecate
or can pass thos in command line parameters (-d_crt_secure_no_deprecate). can #define them @ beginning of *.cpp files. also, there more of them (see crtdefs.h - looks there lot of them...). kind of warnings tell macros can disable them - read compiler output.
Comments
Post a Comment