Hi,
To silence warnings and make the code more secure I opted for changing most of the affected functions to the safe ‘_s’ versions instead of tellng MSVC to ingore them completly (warnings are warnings and should not be igonered imho).
To do this i added two files to the root of the project callled Fixups.h and Fixups.cpp.
Fixups.h:
#ifndef FIXUPS
#define FIXUPS
#ifndef WIN32
errno_t strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource );
errno_t strcat_s(char * strDestination, size_t numberOfElements, const char * strSource );
errno_t strncpy_s(char * strDest, size_t numberOfElements, const char *strSource, size_t count);
int sprintf_s(char *buffer, size_t, sizeOfBuffer, const char *format, ... );
//FILE *fopen(const char *filename, const char *mode);
#else
#define countof(_Array) sizeof(_Array)
#include <stdlib.h>
#endif
#endif FIXUPS
Fixups.cpp:
#include "Fixups.h"
#ifndef WIN32
#include <stdio.h> //printf
#include <stdarg.h> //va_list, va_start
#include <string.h>
//The FixupCode is not tested as I cannot compile for *Nix.
errno_t strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource ) {
//Call unsafe version (and maybe add some safety here..)
strcpy_s(strDestination, strSource);
//Assume no error...
return 0;
}
errno_t strcat_s(char * strDestination, size_t numberOfElements, const char * strSource )
{
//Call unsafe version (and maybe add some safety here..)
strcat(strDestination, strSource);
//Assume no error...
return 0;
}
errno_t strncpy_s(char * strDest, size_t numberOfElements, const char *strSource, size_t count)
{
//Call unsafe version (and maybe add some safety here..)
strncpy(strDest, strSource, count);
return 0;
}
int sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ... )
{
va_list arguments;
va_start(arguments, format);
//Call unsafe version (and maybe add some safety here..)
return sprintf(buffer, format, arguments);
}
//errno_t fopen_s(FILE** pFile, const char *filename, const char *mode) {
// pFile = fopen(filename, mode);
//}
#endif
To use these in the RakNet sources, add
#include "../../Fixups.h"
to the includes at the top of files that emit warnings.
To change the function calls causing the warning, add the ‘_s’ suffix to the name and insert a second function parameter either:
_countof('first parameter name')
or
sizeof('first parameter name').
The _countof() macro only works on arrays (and is element size aware so is unicode proof), sizeof can be used on all others that cannot use _countof()
Note that I did not change function like fopen, mkdir and vsnprintf but that should not be to hard to add.
For *Nix the Fixup code just calls the ‘unsafe’ versions as before (but safety checks could be added easily).