Add MSVC secure library functions

This commit is contained in:
Andy Vandijck 2025-05-10 14:11:26 +02:00
parent 8f47df28bf
commit 1c436f2dfe
26 changed files with 495 additions and 8 deletions

View File

@ -13,8 +13,15 @@ std::string get_xdg_user_config_home()
std::string home(getenv("HOME")); std::string home(getenv("HOME"));
path = home + "/Library/Application Support"; path = home + "/Library/Application Support";
#elif _WIN32 #elif _WIN32
#if __STDC_WANT_SECURE_LIB__
char *app_data_env = NULL;
size_t app_data_env_sz = 0;
_dupenv_s(&app_data_env, &app_data_env_sz, "LOCALAPPDATA");
if (!app_data_env) _dupenv_s(&app_data_env, &app_data_env_sz, "APPDATA");
#else
char *app_data_env = getenv("LOCALAPPDATA"); char *app_data_env = getenv("LOCALAPPDATA");
if (!app_data_env) app_data_env = getenv("APPDATA"); if (!app_data_env) app_data_env = getenv("APPDATA");
#endif
std::string app_data(app_data_env); std::string app_data(app_data_env);
path = app_data; path = app_data;
#else // Unix #else // Unix
@ -40,8 +47,15 @@ std::string get_xdg_user_data_home()
std::string home(getenv("HOME")); std::string home(getenv("HOME"));
path = home + "/Library/Application Support"; path = home + "/Library/Application Support";
#elif _WIN32 #elif _WIN32
#if __STDC_WANT_SECURE_LIB__
char *app_data_env = NULL;
size_t app_data_env_sz = 0;
_dupenv_s(&app_data_env, &app_data_env_sz, "LOCALAPPDATA");
if (!app_data_env) _dupenv_s(&app_data_env, &app_data_env_sz, "APPDATA");
#else
char *app_data_env = getenv("LOCALAPPDATA"); char *app_data_env = getenv("LOCALAPPDATA");
if (!app_data_env) app_data_env = getenv("APPDATA"); if (!app_data_env) app_data_env = getenv("APPDATA");
#endif
std::string app_data(app_data_env); std::string app_data(app_data_env);
path = app_data; path = app_data;
#else // Unix #else // Unix

View File

@ -40,7 +40,7 @@ void utilReadDataMem(const uint8_t *&data, variable_desc *);
#else // !defined(__LIBRETRO__) #else // !defined(__LIBRETRO__)
// strip .gz or .z off end // strip .gz or .z off end
void utilStripDoubleExtension(const char *, char *); void utilStripDoubleExtension(const char *, char *, size_t);
gzFile utilAutoGzOpen(const char *file, const char *mode); gzFile utilAutoGzOpen(const char *file, const char *mode);
gzFile utilGzOpen(const char *file, const char *mode); gzFile utilGzOpen(const char *file, const char *mode);

View File

@ -27,7 +27,13 @@ FILE* utilOpenFile(const char* filename, const char* mode) {
return nullptr; return nullptr;
} }
#if __STDC_WANT_SECURE_LIB__
FILE *ret = NULL;
_wfopen_s(&ret, wfilename.data(), wmode.data());
return ret;
#else
return _wfopen(wfilename.data(), wmode.data()); return _wfopen(wfilename.data(), wmode.data());
#endif
#else #else
return fopen(filename, mode); return fopen(filename, mode);
#endif // _WIN32 #endif // _WIN32

View File

@ -48,10 +48,15 @@ fex_t* scanArchive(const char* file, bool (*accept)(const char*), char (&buffer)
// Scan filenames // Scan filenames
bool found = false; bool found = false;
while (!fex_done(fe)) { while (!fex_done(fe)) {
#ifdef __STDC_WANT_SECURE_LIB__
strncpy_s(buffer, sizeof buffer, fex_name(fe), sizeof buffer);
#else
strncpy(buffer, fex_name(fe), sizeof buffer); strncpy(buffer, fex_name(fe), sizeof buffer);
#endif
buffer[sizeof buffer - 1] = '\0'; buffer[sizeof buffer - 1] = '\0';
utilStripDoubleExtension(buffer, buffer); utilStripDoubleExtension(buffer, buffer, sizeof buffer);
if (accept(buffer)) { if (accept(buffer)) {
found = true; found = true;
@ -153,9 +158,16 @@ IMAGE_TYPE utilFindType(const char* file) {
return utilFindType(file, buffer); return utilFindType(file, buffer);
} }
void utilStripDoubleExtension(const char* file, char* buffer) { void utilStripDoubleExtension(const char* file, char* buffer, size_t len) {
#if !__STDC_WANT_SECURE_LIB__
(void)len;
#endif
if (buffer != file) // allows conversion in place if (buffer != file) // allows conversion in place
#if __STDC_WANT_SECURE_LIB__
strcpy_s(buffer, len, file);
#else
strcpy(buffer, file); strcpy(buffer, file);
#endif
if (utilIsGzipFile(file)) { if (utilIsGzipFile(file)) {
char* p = strrchr(buffer, '.'); char* p = strrchr(buffer, '.');

View File

@ -100,7 +100,12 @@ bool utilWritePNGFile(const char* fileName, int w, int h, uint8_t* pix) {
bool utilWriteBMPFile(const char* fileName, int w, int h, uint8_t* pix) { bool utilWriteBMPFile(const char* fileName, int w, int h, uint8_t* pix) {
uint8_t writeBuffer[512 * 3]; uint8_t writeBuffer[512 * 3];
#if __STDC_WANT_SECURE_LIB__
FILE* fp = NULL;
fopen_s(&fp, fileName, "wb");
#else
FILE* fp = fopen(fileName, "wb"); FILE* fp = fopen(fileName, "wb");
#endif
if (!fp) { if (!fp) {
systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), fileName); systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), fileName);

View File

@ -518,7 +518,11 @@ static FILE* blargg_fopen( const char path [], const char mode [] )
{ {
wmode = blargg_to_wide( mode ); wmode = blargg_to_wide( mode );
if ( wmode ) if ( wmode )
#if __STDC_WANT_SECURE_LIB__
_wfopen_s(&file, wpath, wmode );
#else
file = _wfopen( wpath, wmode ); file = _wfopen( wpath, wmode );
#endif
} }
// Save and restore errno in case free() clears it // Save and restore errno in case free() clears it

View File

@ -288,7 +288,11 @@ blargg_err_t Zip7_Extractor::next_v()
time_t _time = time; time_t _time = time;
#ifdef _WIN32 #ifdef _WIN32
#if __STDC_WANT_SECURE_LIB__
localtime_s(&tm, &_time);
#else
tm = *localtime( &_time ); tm = *localtime( &_time );
#endif
#else #else
localtime_r( &_time, &tm ); localtime_r( &_time, &tm );
#endif #endif

View File

@ -66,7 +66,11 @@ std::vector<VBamIoVec> g_vbamIoVecs;
void ResetMBC3RTC() { void ResetMBC3RTC() {
time(&gbDataMBC3.mapperLastTime); time(&gbDataMBC3.mapperLastTime);
struct tm* lt; struct tm* lt;
#if __STDC_WANT_SECURE_LIB__
localtime_s(lt, &gbDataMBC3.mapperLastTime);
#else
lt = localtime(&gbDataMBC3.mapperLastTime); lt = localtime(&gbDataMBC3.mapperLastTime);
#endif
gbDataMBC3.mapperSeconds = lt->tm_sec; gbDataMBC3.mapperSeconds = lt->tm_sec;
gbDataMBC3.mapperMinutes = lt->tm_min; gbDataMBC3.mapperMinutes = lt->tm_min;
gbDataMBC3.mapperHours = lt->tm_hour; gbDataMBC3.mapperHours = lt->tm_hour;
@ -80,7 +84,11 @@ void ResetTama5RTC() {
31, 31, 30, 31, 30, 31}; 31, 31, 30, 31, 30, 31};
time(&gbDataTAMA5.mapperLastTime); time(&gbDataTAMA5.mapperLastTime);
struct tm* lt; struct tm* lt;
#if __STDC_WANT_SECURE_LIB__
localtime_s(lt, &gbDataTAMA5.mapperLastTime);
#else
lt = localtime(&gbDataTAMA5.mapperLastTime); lt = localtime(&gbDataTAMA5.mapperLastTime);
#endif
gbDataTAMA5.mapperSeconds = lt->tm_sec; gbDataTAMA5.mapperSeconds = lt->tm_sec;
gbDataTAMA5.mapperMinutes = lt->tm_min; gbDataTAMA5.mapperMinutes = lt->tm_min;
gbDataTAMA5.mapperHours = lt->tm_hour; gbDataTAMA5.mapperHours = lt->tm_hour;
@ -111,7 +119,12 @@ void ResetTama5RTC() {
void ResetHuc3RTC() { void ResetHuc3RTC() {
time(&gbRTCHuC3.mapperLastTime); time(&gbRTCHuC3.mapperLastTime);
#if __STDC_WANT_SECURE_LIB__
struct tm* lt;
localtime_s(lt, &gbRTCHuC3.mapperLastTime);
#else
localtime(&gbRTCHuC3.mapperLastTime); localtime(&gbRTCHuC3.mapperLastTime);
#endif
} }
bool WriteBatteryFile(const char* file_name) { bool WriteBatteryFile(const char* file_name) {

View File

@ -209,8 +209,13 @@ bool gbAddGsCheat(const char* code, const char* desc)
int i = gbCheatNumber; int i = gbCheatNumber;
#if __STDC_WANT_SECURE_LIB__
strcpy_s(gbCheatList[i].cheatCode, sizeof(gbCheatList[i].cheatCode), code);
strcpy_s(gbCheatList[i].cheatDesc, sizeof(gbCheatList[i].cheatDesc), desc);
#else
strcpy(gbCheatList[i].cheatCode, code); strcpy(gbCheatList[i].cheatCode, code);
strcpy(gbCheatList[i].cheatDesc, desc); strcpy(gbCheatList[i].cheatDesc, desc);
#endif
gbCheatList[i].code = GBCHEAT_HEX_VALUE(code[0]) << 4 | GBCHEAT_HEX_VALUE(code[1]); gbCheatList[i].code = GBCHEAT_HEX_VALUE(code[0]) << 4 | GBCHEAT_HEX_VALUE(code[1]);
@ -318,8 +323,13 @@ bool gbAddGgCheat(const char* code, const char* desc)
size_t len = strlen(code); size_t len = strlen(code);
#if __STDC_WANT_SECURE_LIB__
strcpy_s(gbCheatList[i].cheatCode, sizeof(gbCheatList[i].cheatCode), code);
strcpy_s(gbCheatList[i].cheatDesc, sizeof(gbCheatList[i].cheatDesc), desc);
#else
strcpy(gbCheatList[i].cheatCode, code); strcpy(gbCheatList[i].cheatCode, code);
strcpy(gbCheatList[i].cheatDesc, desc); strcpy(gbCheatList[i].cheatDesc, desc);
#endif
gbCheatList[i].code = 0x101; gbCheatList[i].code = 0x101;
gbCheatList[i].value = (GBCHEAT_HEX_VALUE(code[0]) << 4) + GBCHEAT_HEX_VALUE(code[1]); gbCheatList[i].value = (GBCHEAT_HEX_VALUE(code[0]) << 4) + GBCHEAT_HEX_VALUE(code[1]);

View File

@ -899,6 +899,10 @@ static int input ( void );
#endif #endif
#if __STDC_WANT_SECURE_LIB__
#define sscanf sscanf_s
#endif
/* No semi-colon after return; correct usage is to write "yyterminate();" - /* No semi-colon after return; correct usage is to write "yyterminate();" -
* we don't want an extra ';' after the "return" because that will cause * we don't want an extra ';' after the "return" because that will cause
* some compilers to complain about unreachable statements. * some compilers to complain about unreachable statements.

View File

@ -1516,7 +1516,12 @@ void dexp_saveVars(char *file)
{ {
std::map<std::string, uint32_t>::iterator iter; std::map<std::string, uint32_t>::iterator iter;
#if __STDC_WANT_SECURE_LIB__
FILE* f = NULL;
fopen_s(&f, file, "w");
#else
FILE *f = fopen(file, "w"); FILE *f = fopen(file, "w");
#endif
if (!f) { if (!f) {
printf("Could not open file %s\n", file); printf("Could not open file %s\n", file);
return; return;
@ -1535,14 +1540,23 @@ void dexp_loadVars(char *file)
char name[500]; char name[500];
uint32_t val; uint32_t val;
#if __STDC_WANT_SECURE_LIB__
FILE* f = NULL;
fopen_s(&f, file, "r");
#else
FILE *f = fopen(file, "r"); FILE *f = fopen(file, "r");
#endif
if (!f) { if (!f) {
printf("Could not open file %s\n", file); printf("Could not open file %s\n", file);
return; return;
} }
while (fgets(buffer, 500, f) != NULL) { while (fgets(buffer, 500, f) != NULL) {
#if __STDC_WANT_SECURE_LIB__
if (sscanf_s(buffer, "%s = %x", name, (unsigned)_countof(name), &val) == 2) {
#else
if (sscanf(buffer, "%s = %x", name, &val) == 2) { if (sscanf(buffer, "%s = %x", name, &val) == 2) {
#endif
dexp_setVar(name, val); dexp_setVar(name, val);
} }
} }

View File

@ -11,6 +11,7 @@
#if __STDC_WANT_SECURE_LIB__ #if __STDC_WANT_SECURE_LIB__
#define snprintf sprintf_s #define snprintf sprintf_s
#define sscanf sscanf_s
#endif #endif
/** /**
@ -1299,8 +1300,13 @@ void cheatsAdd(const char* codeStr,
cheatsList[x].rawaddress = rawaddress; cheatsList[x].rawaddress = rawaddress;
cheatsList[x].address = address; cheatsList[x].address = address;
cheatsList[x].value = value; cheatsList[x].value = value;
#if __STDC_WANT_SECURE_LIB__
strcpy_s(cheatsList[x].codestring, sizeof(cheatsList[x].codestring), codeStr);
strcpy_s(cheatsList[x].desc, sizeof(cheatsList[x].desc), desc);
#else
strcpy(cheatsList[x].codestring, codeStr); strcpy(cheatsList[x].codestring, codeStr);
strcpy(cheatsList[x].desc, desc); strcpy(cheatsList[x].desc, desc);
#endif
cheatsList[x].enabled = true; cheatsList[x].enabled = true;
cheatsList[x].status = 0; cheatsList[x].status = 0;
@ -1461,7 +1467,11 @@ bool cheatsVerifyCheatCode(const char* code, const char* desc)
uint32_t value = 0; uint32_t value = 0;
char buffer[10]; char buffer[10];
#if __STDC_WANT_SECURE_LIB__
strncpy_s(buffer, sizeof(buffer), code, 8);
#else
strncpy(buffer, code, 8); strncpy(buffer, code, 8);
#endif
buffer[8] = 0; buffer[8] = 0;
sscanf(buffer, "%x", &address); sscanf(buffer, "%x", &address);
@ -1486,7 +1496,12 @@ bool cheatsVerifyCheatCode(const char* code, const char* desc)
return false; return false;
} }
#if __STDC_WANT_SECURE_LIB__
strncpy_s(buffer, sizeof(buffer), &code[9], 8);
#else
strncpy(buffer, &code[9], 8); strncpy(buffer, &code[9], 8);
#endif
sscanf(buffer, "%x", &value); sscanf(buffer, "%x", &value);
int type = 0; int type = 0;
if (len == 13) if (len == 13)
@ -1574,11 +1589,19 @@ void cheatsAddGSACode(const char* code, const char* desc, bool v3)
} }
char buffer[10]; char buffer[10];
#if __STDC_WANT_SECURE_LIB__
strncpy_s(buffer, sizeof(buffer), code, 8);
#else
strncpy(buffer, code, 8); strncpy(buffer, code, 8);
#endif
buffer[8] = 0; buffer[8] = 0;
uint32_t address; uint32_t address;
sscanf(buffer, "%x", &address); sscanf(buffer, "%x", &address);
#if __STDC_WANT_SECURE_LIB__
strncpy_s(buffer, sizeof(buffer), &code[8], 8);
#else
strncpy(buffer, &code[8], 8); strncpy(buffer, &code[8], 8);
#endif
buffer[8] = 0; buffer[8] = 0;
uint32_t value; uint32_t value;
sscanf(buffer, "%x", &value); sscanf(buffer, "%x", &value);
@ -2459,11 +2482,19 @@ void cheatsAddCBACode(const char* code, const char* desc)
} }
char buffer[10]; char buffer[10];
#if __STDC_WANT_SECURE_LIB__
strncpy_s(buffer, sizeof(buffer), code, 8);
#else
strncpy(buffer, code, 8); strncpy(buffer, code, 8);
#endif
buffer[8] = 0; buffer[8] = 0;
uint32_t address; uint32_t address;
sscanf(buffer, "%x", &address); sscanf(buffer, "%x", &address);
#if __STDC_WANT_SECURE_LIB__
strncpy_s(buffer, sizeof(buffer), &code[9], 4);
#else
strncpy(buffer, &code[9], 4); strncpy(buffer, &code[9], 4);
#endif
buffer[4] = 0; buffer[4] = 0;
uint32_t value; uint32_t value;
sscanf(buffer, "%x", &value); sscanf(buffer, "%x", &value);
@ -2640,12 +2671,20 @@ void cheatsReadGame(gzFile file, int version)
if (cheatsList[i].code == 512 && firstCodeBreaker) { if (cheatsList[i].code == 512 && firstCodeBreaker) {
firstCodeBreaker = false; firstCodeBreaker = false;
char buffer[10]; char buffer[10];
#if __STDC_WANT_SECURE_LIB__
strncpy_s(buffer, sizeof(buffer), cheatsList[i].codestring, 8);
#else
strncpy(buffer, cheatsList[i].codestring, 8); strncpy(buffer, cheatsList[i].codestring, 8);
#endif
buffer[8] = 0; buffer[8] = 0;
uint32_t address; uint32_t address;
sscanf(buffer, "%x", &address); sscanf(buffer, "%x", &address);
if ((address >> 28) == 9) { if ((address >> 28) == 9) {
#if __STDC_WANT_SECURE_LIB__
strncpy_s(buffer, sizeof(buffer), &cheatsList[i].codestring[9], 4);
#else
strncpy(buffer, &cheatsList[i].codestring[9], 4); strncpy(buffer, &cheatsList[i].codestring[9], 4);
#endif
buffer[4] = 0; buffer[4] = 0;
uint32_t value; uint32_t value;
sscanf(buffer, "%x", &value); sscanf(buffer, "%x", &value);
@ -2780,12 +2819,20 @@ bool cheatsLoadCheatList(const char* file)
if (cheatsList[i].code == 512 && firstCodeBreaker) { if (cheatsList[i].code == 512 && firstCodeBreaker) {
firstCodeBreaker = false; firstCodeBreaker = false;
char buffer[10]; char buffer[10];
#if __STDC_WANT_SECURE_LIB__
strncpy_s(buffer, sizeof(buffer), cheatsList[i].codestring, 8);
#else
strncpy(buffer, cheatsList[i].codestring, 8); strncpy(buffer, cheatsList[i].codestring, 8);
#endif
buffer[8] = 0; buffer[8] = 0;
uint32_t address; uint32_t address;
sscanf(buffer, "%x", &address); sscanf(buffer, "%x", &address);
if ((address >> 28) == 9) { if ((address >> 28) == 9) {
#if __STDC_WANT_SECURE_LIB__
strncpy_s(buffer, sizeof(buffer), &cheatsList[i].codestring[9], 4);
#else
strncpy(buffer, &cheatsList[i].codestring[9], 4); strncpy(buffer, &cheatsList[i].codestring[9], 4);
#endif
buffer[4] = 0; buffer[4] = 0;
uint32_t value; uint32_t value;
sscanf(buffer, "%x", &value); sscanf(buffer, "%x", &value);

View File

@ -20,8 +20,12 @@
#endif #endif
#ifdef _MSC_VER #ifdef _MSC_VER
#if __STDC_WANT_SECURE_LIB__
#define snprintf sprintf_s
#else
#define snprintf _snprintf #define snprintf _snprintf
#endif #endif
#endif
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////

View File

@ -280,7 +280,11 @@ const char* elfGetAddressSymbol(uint32_t addr)
if (offset) if (offset)
snprintf(buffer, 256, "%s+%d", name, offset); snprintf(buffer, 256, "%s+%d", name, offset);
else { else {
#if __STDC_WANT_SECURE_LIB__
strncpy_s(buffer, sizeof(buffer), name, 255); //strncpy_s does not allways append a '\0'
#else
strncpy(buffer, name, 255); //strncpy does not allways append a '\0' strncpy(buffer, name, 255); //strncpy does not allways append a '\0'
#endif
buffer[255] = '\0'; buffer[255] = '\0';
} }
return buffer; return buffer;
@ -300,16 +304,28 @@ const char* elfGetAddressSymbol(uint32_t addr)
if (offset) if (offset)
snprintf(buffer, 256,"%s+%d", name, addr - s->value); snprintf(buffer, 256,"%s+%d", name, addr - s->value);
else { else {
#if __STDC_WANT_SECURE_LIB__
strncpy_s(buffer, sizeof(buffer), name, 255);
#else
strncpy(buffer, name, 255); strncpy(buffer, name, 255);
#endif
buffer[255] = '\0'; buffer[255] = '\0';
} }
return buffer; return buffer;
} else if (addr == s->value) { } else if (addr == s->value) {
if (s->name) { if (s->name) {
#if __STDC_WANT_SECURE_LIB__
strncpy_s(buffer, sizeof(buffer), s->name, 255);
#else
strncpy(buffer, s->name, 255); strncpy(buffer, s->name, 255);
#endif
buffer[255] = '\0'; buffer[255] = '\0';
} else } else
#if __STDC_WANT_SECURE_LIB__
strcpy_s(buffer, sizeof(buffer), "");
#else
strcpy(buffer, ""); strcpy(buffer, "");
#endif
return buffer; return buffer;
} }
} }

View File

@ -538,21 +538,37 @@ bool GetLinkServerHost(char* const host, size_t size)
host[0] = '\0'; host[0] = '\0';
if (linkDriver && linkDriver->mode == LINK_GAMECUBE_DOLPHIN) { if (linkDriver && linkDriver->mode == LINK_GAMECUBE_DOLPHIN) {
#if __STDC_WANT_SECURE_LIB__
strncpy_s(host, size, joybusHostAddr.toString().c_str(), size);
#else
strncpy(host, joybusHostAddr.toString().c_str(), size); strncpy(host, joybusHostAddr.toString().c_str(), size);
#endif
} else if (lanlink.server) { } else if (lanlink.server) {
if (IP_LINK_BIND_ADDRESS == "*") { if (IP_LINK_BIND_ADDRESS == "*") {
auto local_addr = sf::IpAddress::getLocalAddress(); auto local_addr = sf::IpAddress::getLocalAddress();
if (local_addr) { if (local_addr) {
#if __STDC_WANT_SECURE_LIB__
strncpy_s(host, size, local_addr.value().toString().c_str(), size);
#else
strncpy(host, local_addr.value().toString().c_str(), size); strncpy(host, local_addr.value().toString().c_str(), size);
#endif
} else { } else {
return false; return false;
} }
} else { } else {
#if __STDC_WANT_SECURE_LIB__
strncpy_s(host, size, IP_LINK_BIND_ADDRESS.c_str(), size);
#else
strncpy(host, IP_LINK_BIND_ADDRESS.c_str(), size); strncpy(host, IP_LINK_BIND_ADDRESS.c_str(), size);
#endif
} }
} }
else { else {
#if __STDC_WANT_SECURE_LIB__
strncpy_s(host, size, lc.serveraddr.toString().c_str(), size);
#else
strncpy(host, lc.serveraddr.toString().c_str(), size); strncpy(host, lc.serveraddr.toString().c_str(), size);
#endif
} }
return true; return true;

View File

@ -50,6 +50,7 @@
#if __STDC_WANT_SECURE_LIB__ #if __STDC_WANT_SECURE_LIB__
#define snprintf sprintf_s #define snprintf sprintf_s
#define sscanf sscanf_s
#endif #endif
extern int emulating; extern int emulating;
@ -392,7 +393,11 @@ void debuggerDumpLoad(int n, char** args)
return; return;
} }
#if __STDC_WANT_SECURE_LIB__
fopen_s(&f, file, "rb");
#else
f = fopen(file, "rb"); f = fopen(file, "rb");
#endif
if (f == NULL) { if (f == NULL) {
{ {
snprintf(monbuf, sizeof(monbuf), "Error opening file.\n"); snprintf(monbuf, sizeof(monbuf), "Error opening file.\n");
@ -442,7 +447,12 @@ void debuggerDumpSave(int n, char** args)
return; return;
} }
#if __STDC_WANT_SECURE_LIB__
fopen_s(&f, file, "wb");
#else
f = fopen(file, "wb"); f = fopen(file, "wb");
#endif
if (f == NULL) { if (f == NULL) {
{ {
snprintf(monbuf, sizeof(monbuf), "Error opening file.\n"); snprintf(monbuf, sizeof(monbuf), "Error opening file.\n");
@ -935,10 +945,18 @@ void debuggerFindText(int n, char** args)
if (n == 4) { if (n == 4) {
sscanf(args[2], "%u", &SearchMaxMatches); sscanf(args[2], "%u", &SearchMaxMatches);
#if __STDC_WANT_SECURE_LIB__
strncpy_s((char*)SearchData, sizeof(SearchData), args[3], 64);
#else
strncpy((char*)SearchData, args[3], 64); strncpy((char*)SearchData, args[3], 64);
#endif
SearchLength = (unsigned int)strlen(args[3]); SearchLength = (unsigned int)strlen(args[3]);
} else if (n == 3) { } else if (n == 3) {
#if __STDC_WANT_SECURE_LIB__
strncpy_s((char*)SearchData, sizeof(SearchData), args[2], 64);
#else
strncpy((char*)SearchData, args[2], 64); strncpy((char*)SearchData, args[2], 64);
#endif
SearchLength = (unsigned int)strlen(args[2]); SearchLength = (unsigned int)strlen(args[2]);
}; };
@ -971,10 +989,18 @@ void debuggerFindHex(int n, char** args)
char SearchHex[128]; char SearchHex[128];
if (n == 4) { if (n == 4) {
sscanf(args[2], "%u", &SearchMaxMatches); sscanf(args[2], "%u", &SearchMaxMatches);
#if __STDC_WANT_SECURE_LIB__
strncpy_s(SearchHex, sizeof(SearchHex), args[3], 128);
#else
strncpy(SearchHex, args[3], 128); strncpy(SearchHex, args[3], 128);
#endif
SearchLength = (unsigned int)strlen(args[3]); SearchLength = (unsigned int)strlen(args[3]);
} else if (n == 3) { } else if (n == 3) {
#if __STDC_WANT_SECURE_LIB__
strncpy_s(SearchHex, sizeof(SearchHex), args[2], 128);
#else
strncpy(SearchHex, args[2], 128); strncpy(SearchHex, args[2], 128);
#endif
SearchLength = (unsigned int)strlen(args[2]); SearchLength = (unsigned int)strlen(args[2]);
}; };
@ -1507,7 +1533,12 @@ void debuggerReadCharTable(int n, char** args)
} }
return; return;
} }
#if __STDC_WANT_SECURE_LIB__
FILE* tlb = NULL;
fopen_s(&tlb, args[1], "r");
#else
FILE* tlb = fopen(args[1], "r"); FILE* tlb = fopen(args[1], "r");
#endif
if (!tlb) { if (!tlb) {
{ {
snprintf(monbuf, sizeof(monbuf), "Could not open specified file. Abort.\n"); snprintf(monbuf, sizeof(monbuf), "Could not open specified file. Abort.\n");
@ -1520,8 +1551,11 @@ void debuggerReadCharTable(int n, char** args)
char* character = (char*)calloc(10, sizeof(char)); char* character = (char*)calloc(10, sizeof(char));
wordSymbol = (char**)calloc(256, sizeof(char*)); wordSymbol = (char**)calloc(256, sizeof(char*));
while (fgets(buffer, 30, tlb)) { while (fgets(buffer, 30, tlb)) {
#if __STDC_WANT_SECURE_LIB__
sscanf_s(buffer, "%02x=%s", &slot, character, 10);
#else
sscanf(buffer, "%02x=%s", &slot, character); sscanf(buffer, "%02x=%s", &slot, character);
#endif
if (character[0]) { if (character[0]) {
if (strlen(character) == 4) { if (strlen(character) == 4) {
@ -1789,7 +1823,12 @@ void debuggerExecuteCommands(int n, char** args)
n--; n--;
args++; args++;
while (n) { while (n) {
#if __STDC_WANT_SECURE_LIB__
FILE* toExec = NULL;
fopen_s(&toExec, args[0], "r");
#else
FILE* toExec = fopen(args[0], "r"); FILE* toExec = fopen(args[0], "r");
#endif
if (toExec) { if (toExec) {
while (fgets(buffer, 4096, toExec)) { while (fgets(buffer, 4096, toExec)) {
std::string buf(buffer); std::string buf(buffer);
@ -3388,7 +3427,11 @@ void dbgExecute(char* toRun)
void dbgExecute(std::string& cmd) void dbgExecute(std::string& cmd)
{ {
char* dbgCmd = new char[cmd.length() + 1]; char* dbgCmd = new char[cmd.length() + 1];
#if __STDC_WANT_SECURE_LIB__
strcpy_s(dbgCmd, cmd.length() + 1, cmd.c_str());
#else
strcpy(dbgCmd, cmd.c_str()); strcpy(dbgCmd, cmd.c_str());
#endif
dbgExecute(dbgCmd); dbgExecute(dbgCmd);
delete[] dbgCmd; delete[] dbgCmd;
} }
@ -4314,7 +4357,11 @@ void monprintf(std::string line)
if (output.length() <= 1000) { if (output.length() <= 1000) {
char dbgReply[1000]; char dbgReply[1000];
#if __STDC_WANT_SECURE_LIB__
strcpy_s(dbgReply, sizeof(dbgReply), output.c_str());
#else
strcpy(dbgReply, output.c_str()); strcpy(dbgReply, output.c_str());
#endif
remotePutPacket(dbgReply); remotePutPacket(dbgReply);
} }
} }

View File

@ -111,7 +111,11 @@ void SetGBATime()
{ {
time_t long_time; time_t long_time;
time(&long_time); /* Get time as long integer. */ time(&long_time); /* Get time as long integer. */
#if __STDC_WANT_SECURE_LIB__
localtime_s(&gba_time, &long_time); /* Convert to local time. */
#else
gba_time = *localtime(&long_time); /* Convert to local time. */ gba_time = *localtime(&long_time); /* Convert to local time. */
#endif
} }
void rtcUpdateTime(int ticks) void rtcUpdateTime(int ticks)

View File

@ -110,6 +110,7 @@ sw, sword, int32_t, int --> signed word
#if (defined __WIN32__ || defined _WIN32) #if (defined __WIN32__ || defined _WIN32)
#define strdup _strdup #define strdup _strdup
#define sscanf sscanf_s
#endif #endif
extern bool dexp_eval(char*, uint32_t*); extern bool dexp_eval(char*, uint32_t*);

View File

@ -18,6 +18,7 @@
#if __STDC_WANT_SECURE_LIB__ #if __STDC_WANT_SECURE_LIB__
#define snprintf sprintf_s #define snprintf sprintf_s
#define sscanf sscanf_s
#endif #endif
#define getcwd _getcwd #define getcwd _getcwd
@ -472,7 +473,13 @@ const char* FindConfigFile(const char *name)
} }
#ifdef _WIN32 #ifdef _WIN32
#if __STDC_WANT_SECURE_LIB__
char *home = NULL;
size_t home_sz = 0;
_dupenv_s(&home, &home_sz, "USERPROFILE");
#else
char *home = getenv("USERPROFILE"); char *home = getenv("USERPROFILE");
#endif
if (home != NULL) { if (home != NULL) {
snprintf(path, "%s%c%s", home, kFileSep, name); snprintf(path, "%s%c%s", home, kFileSep, name);
if (FileExists(path)) if (FileExists(path))
@ -483,12 +490,28 @@ const char* FindConfigFile(const char *name)
if (!strchr(arg0, '/') && if (!strchr(arg0, '/') &&
!strchr(arg0, '\\')) { !strchr(arg0, '\\')) {
#if __STDC_WANT_SECURE_LIB__
char *env_path = NULL;
size_t env_path_sz = 0;
_dupenv_s(&env_path, &env_path_sz, "PATH");
#else
char *env_path = getenv("PATH"); char *env_path = getenv("PATH");
#endif
if (env_path != NULL) { if (env_path != NULL) {
#if __STDC_WANT_SECURE_LIB__
strncpy_s(buffer, sizeof(buffer), env_path, 4096);
#else
strncpy(buffer, env_path, 4096); strncpy(buffer, env_path, 4096);
#endif
buffer[4095] = 0; buffer[4095] = 0;
#if __STDC_WANT_SECURE_LIB__
char *next_tok1 = NULL;
char *next_tok2 = NULL;
char *tok = strtok_s(buffer, PATH_SEP, &next_tok1);
#else
char *tok = strtok(buffer, PATH_SEP); char *tok = strtok(buffer, PATH_SEP);
#endif
while (tok) { while (tok) {
snprintf(env_path, 4096, "%s%c%s", tok, kFileSep, EXE_NAME); snprintf(env_path, 4096, "%s%c%s", tok, kFileSep, EXE_NAME);
@ -499,13 +522,21 @@ const char* FindConfigFile(const char *name)
return path2; return path2;
} }
} }
#if __STDC_WANT_SECURE_LIB__
tok = strtok_s(NULL, PATH_SEP, &next_tok2);
#else
tok = strtok(NULL, PATH_SEP); tok = strtok(NULL, PATH_SEP);
#endif
} }
} }
} }
else { else {
// executable is relative to some directory // executable is relative to some directory
#if __STDC_WANT_SECURE_LIB__
strcpy_s(buffer, sizeof(buffer), arg0);
#else
strcpy(buffer, arg0); strcpy(buffer, arg0);
#endif
char *p = strrchr(buffer, kFileSep); char *p = strrchr(buffer, kFileSep);
if (p) { if (p) {
*p = 0; *p = 0;
@ -550,7 +581,11 @@ void SaveConfigFile()
FILE *f = utilOpenFile(configFile, "w"); FILE *f = utilOpenFile(configFile, "w");
if (f == NULL) { if (f == NULL) {
char err_msg[4096] = "unknown error"; char err_msg[4096] = "unknown error";
#if __STDC_WANT_SECURE_LIB__
strerror_s(err_msg, sizeof(err_msg), errno);
#else
strncpy(err_msg, strerror(errno), 4096); strncpy(err_msg, strerror(errno), 4096);
#endif
fprintf(stderr, "Configuration file '%s' could not be written to: %s\n", configFile, err_msg); fprintf(stderr, "Configuration file '%s' could not be written to: %s\n", configFile, err_msg);
return; return;
} }
@ -633,7 +668,11 @@ static char *xstrdup(const char *s)
return NULL; return NULL;
t = (char *)malloc(strlen(s) + 1); t = (char *)malloc(strlen(s) + 1);
if (t) { if (t) {
#if __STDC_WANT_SECURE_LIB__
strcpy_s(t, strlen(s) + 1, s);
#else
strcpy(t, s); strcpy(t, s);
#endif
} }
return t; return t;
} }
@ -659,7 +698,11 @@ int ReadOpts(int argc, char ** argv)
{ {
//char* cpy; //char* cpy;
//cpy = (char *)malloc(1 + strlen(optarg)); //cpy = (char *)malloc(1 + strlen(optarg));
//#if __STDC_WANT_SECURE_LIB__
//strcpy_s(cpy, 1 + strlen(optarg), optarg);
//#else
//strcpy(cpy, optarg); //strcpy(cpy, optarg);
//#endif
//preparedCheatCodes[preparedCheats++] = cpy; //preparedCheatCodes[preparedCheats++] = cpy;
std::string cpy = optarg; std::string cpy = optarg;
preparedCheatCodes[preparedCheats++] = cpy.c_str(); preparedCheatCodes[preparedCheats++] = cpy.c_str();
@ -712,7 +755,11 @@ int ReadOpts(int argc, char ** argv)
} }
else { else {
patchNames[patchNum] = (char *)malloc(1 + strlen(optarg)); patchNames[patchNum] = (char *)malloc(1 + strlen(optarg));
#if __STDC_WANT_SECURE_LIB__
strcpy_s(patchNames[patchNum], 1 + strlen(optarg), optarg);
#else
strcpy(patchNames[patchNum], optarg); strcpy(patchNames[patchNum], optarg);
#endif
patchNum++; patchNum++;
} }
break; break;

View File

@ -388,9 +388,17 @@ char* sdlGetFilename(const char* name)
char path[1024]; char path[1024];
const char *_filename = strrchr(name, kFileSep); const char *_filename = strrchr(name, kFileSep);
if (_filename) if (_filename)
#if __STDC_WANT_SECURE_LIB__
strcpy_s(path, sizeof(path), _filename + 1);
#else
strcpy(path, _filename + 1); strcpy(path, _filename + 1);
#endif
else else
#if __STDC_WANT_SECURE_LIB__
strcpy_s(path, sizeof(path), name);
#else
strcpy(path, name); strcpy(path, name);
#endif
return strdup(path); return strdup(path);
} }
@ -432,7 +440,12 @@ FILE* sdlFindFile(const char* name)
fprintf(stdout, "Searching current directory: %s\n", buffer); fprintf(stdout, "Searching current directory: %s\n", buffer);
} }
#if __STDC_WANT_SECURE_LIB__
FILE* f = NULL;
fopen_s(&f, name, "r");
#else
FILE* f = fopen(name, "r"); FILE* f = fopen(name, "r");
#endif
if (f != NULL) { if (f != NULL) {
return f; return f;
} }
@ -440,55 +453,105 @@ FILE* sdlFindFile(const char* name)
if (strlen(homeDataDir)) { if (strlen(homeDataDir)) {
fprintf(stdout, "Searching home directory: %s\n", homeDataDir); fprintf(stdout, "Searching home directory: %s\n", homeDataDir);
snprintf(path, sizeof(path), "%s%c%s", homeDataDir, kFileSep, name); snprintf(path, sizeof(path), "%s%c%s", homeDataDir, kFileSep, name);
#if __STDC_WANT_SECURE_LIB__
fopen_s(&f, path, "r");
#else
f = fopen(path, "r"); f = fopen(path, "r");
#endif
if (f != NULL) if (f != NULL)
return f; return f;
} }
#ifdef _WIN32 #ifdef _WIN32
#if __STDC_WANT_SECURE_LIB__
char* profileDir = NULL;
size_t profileDirSz = 0;
_dupenv_s(&profileDir, &profileDirSz, "USERPROFILE");
#else
char* profileDir = getenv("USERPROFILE"); char* profileDir = getenv("USERPROFILE");
#endif
if (profileDir != NULL) { if (profileDir != NULL) {
fprintf(stdout, "Searching user profile directory: %s\n", profileDir); fprintf(stdout, "Searching user profile directory: %s\n", profileDir);
snprintf(path, sizeof(path), "%s%c%s", profileDir, kFileSep, name); snprintf(path, sizeof(path), "%s%c%s", profileDir, kFileSep, name);
#if __STDC_WANT_SECURE_LIB__
fopen_s(&f, path, "r");
#else
f = fopen(path, "r"); f = fopen(path, "r");
#endif
if (f != NULL) if (f != NULL)
return f; return f;
} }
if (!strchr(home, '/') && !strchr(home, '\\')) { if (!strchr(home, '/') && !strchr(home, '\\')) {
#if __STDC_WANT_SECURE_LIB__
char* _path = NULL;
size_t _pathSz = 0;
_dupenv_s(&_path, &_pathSz, "PATH");
#else
char* _path = getenv("PATH"); char* _path = getenv("PATH");
#endif
if (_path != NULL) { if (_path != NULL) {
fprintf(stdout, "Searching PATH\n"); fprintf(stdout, "Searching PATH\n");
#if __STDC_WANT_SECURE_LIB__
strcpy_s(buffer, sizeof(buffer), _path);
#else
strcpy(buffer, _path); strcpy(buffer, _path);
#endif
buffer[sizeof(buffer) - 1] = 0; buffer[sizeof(buffer) - 1] = 0;
#if __STDC_WANT_SECURE_LIB__
char* tok_next1 = NULL;
char* tok_next2 = NULL;
char* tok = strtok_s(buffer, PATH_SEP, &tok_next1);
#else
char* tok = strtok(buffer, PATH_SEP); char* tok = strtok(buffer, PATH_SEP);
#endif
while (tok) { while (tok) {
snprintf(path, sizeof(path), "%s%c%s", tok, kFileSep, EXE_NAME); snprintf(path, sizeof(path), "%s%c%s", tok, kFileSep, EXE_NAME);
#if __STDC_WANT_SECURE_LIB__
fopen_s(&f, path, "r");
#else
f = fopen(path, "r"); f = fopen(path, "r");
#endif
if (f != NULL) { if (f != NULL) {
char path2[2048]; char path2[2048];
fclose(f); fclose(f);
snprintf(path2, sizeof(path2), "%s%c%s", tok, kFileSep, name); snprintf(path2, sizeof(path2), "%s%c%s", tok, kFileSep, name);
#if __STDC_WANT_SECURE_LIB__
fopen_s(&f, path2, "r");
#else
f = fopen(path2, "r"); f = fopen(path2, "r");
#endif
if (f != NULL) { if (f != NULL) {
fprintf(stdout, "Found at %s\n", path2); fprintf(stdout, "Found at %s\n", path2);
return f; return f;
} }
} }
#if __STDC_WANT_SECURE_LIB__
tok = strtok_s(NULL, PATH_SEP, &tok_next2);
#else
tok = strtok(NULL, PATH_SEP); tok = strtok(NULL, PATH_SEP);
#endif
} }
} }
} else { } else {
// executable is relative to some directory // executable is relative to some directory
fprintf(stdout, "Searching executable directory\n"); fprintf(stdout, "Searching executable directory\n");
#if __STDC_WANT_SECURE_LIB__
strcpy_s(buffer, sizeof(buffer), home);
#else
strcpy(buffer, home); strcpy(buffer, home);
#endif
char* p = strrchr(buffer, kFileSep); char* p = strrchr(buffer, kFileSep);
if (p) { if (p) {
*p = 0; *p = 0;
snprintf(path, sizeof(path), "%s%c%s", buffer, kFileSep, name); snprintf(path, sizeof(path), "%s%c%s", buffer, kFileSep, name);
#if __STDC_WANT_SECURE_LIB__
fopen_s(&f, path, "r");
#else
f = fopen(path, "r"); f = fopen(path, "r");
#endif
if (f != NULL) if (f != NULL)
return f; return f;
} }
@ -496,13 +559,21 @@ FILE* sdlFindFile(const char* name)
#else // ! _WIN32 #else // ! _WIN32
fprintf(stdout, "Searching data directory: %s\n", PKGDATADIR); fprintf(stdout, "Searching data directory: %s\n", PKGDATADIR);
snprintf(path, sizeof(path), "%s%c%s", PKGDATADIR, kFileSep, name); snprintf(path, sizeof(path), "%s%c%s", PKGDATADIR, kFileSep, name);
#if __STDC_WANT_SECURE_LIB__
fopen_s(&f, path, "r");
#else
f = fopen(path, "r"); f = fopen(path, "r");
#endif
if (f != NULL) if (f != NULL)
return f; return f;
fprintf(stdout, "Searching system config directory: %s\n", SYSCONF_INSTALL_DIR); fprintf(stdout, "Searching system config directory: %s\n", SYSCONF_INSTALL_DIR);
snprintf(path, sizeof(path), "%s%c%s", SYSCONF_INSTALL_DIR, kFileSep, name); snprintf(path, sizeof(path), "%s%c%s", SYSCONF_INSTALL_DIR, kFileSep, name);
#if __STDC_WANT_SECURE_LIB__
fopen_s(&f, path, "r");
#else
f = fopen(path, "r"); f = fopen(path, "r");
#endif
if (f != NULL) if (f != NULL)
return f; return f;
#endif // ! _WIN32 #endif // ! _WIN32
@ -631,7 +702,12 @@ static void sdlApplyPerImagePreferences()
if (p) if (p)
*p = 0; *p = 0;
#if __STDC_WANT_SECURE_LIB__
char* token_next = NULL;
char* token = strtok_s(s, " \t\n\r=", &token_next);
#else
char* token = strtok(s, " \t\n\r="); char* token = strtok(s, " \t\n\r=");
#endif
if (!token) if (!token)
continue; continue;
@ -655,7 +731,12 @@ static void sdlApplyPerImagePreferences()
if (p) if (p)
*p = 0; *p = 0;
#if __STDC_WANT_SECURE_LIB__
char* token_next = NULL;
char* token = strtok_s(s, " \t\n\r=", &token_next);
#else
char* token = strtok(s, " \t\n\r="); char* token = strtok(s, " \t\n\r=");
#endif
if (!token) if (!token)
continue; continue;
if (strlen(token) == 0) if (strlen(token) == 0)
@ -663,7 +744,12 @@ static void sdlApplyPerImagePreferences()
if (token[0] == '[') // starting another image settings if (token[0] == '[') // starting another image settings
break; break;
#if __STDC_WANT_SECURE_LIB__
char* value_next = NULL;
char* value = strtok_s(NULL, "\t\n\r=", &value_next);
#else
char* value = strtok(NULL, "\t\n\r="); char* value = strtok(NULL, "\t\n\r=");
#endif
if (value == NULL) if (value == NULL)
continue; continue;
@ -774,13 +860,25 @@ void sdlWriteBackupStateExchange(int from, int to, int backup)
dmp = sdlStateName(from); dmp = sdlStateName(from);
stateNameOrig = (char*)realloc(stateNameOrig, strlen(dmp) + 1); stateNameOrig = (char*)realloc(stateNameOrig, strlen(dmp) + 1);
#if __STDC_WANT_SECURE_LIB__
strcpy_s(stateNameOrig, strlen(dmp) + 1, dmp);
#else
strcpy(stateNameOrig, dmp); strcpy(stateNameOrig, dmp);
#endif
dmp = sdlStateName(to); dmp = sdlStateName(to);
stateNameDest = (char*)realloc(stateNameDest, strlen(dmp) + 1); stateNameDest = (char*)realloc(stateNameDest, strlen(dmp) + 1);
#if __STDC_WANT_SECURE_LIB__
strcpy_s(stateNameDest, strlen(dmp) + 1, dmp);
#else
strcpy(stateNameDest, dmp); strcpy(stateNameDest, dmp);
#endif
dmp = sdlStateName(backup); dmp = sdlStateName(backup);
stateNameBack = (char*)realloc(stateNameBack, strlen(dmp) + 1); stateNameBack = (char*)realloc(stateNameBack, strlen(dmp) + 1);
#if __STDC_WANT_SECURE_LIB__
strcpy_s(stateNameBack, strlen(dmp) + 1, dmp);
#else
strcpy(stateNameBack, dmp); strcpy(stateNameBack, dmp);
#endif
/* on POSIX, rename would not do anything anyway for identical names, but let's check it ourselves anyway */ /* on POSIX, rename would not do anything anyway for identical names, but let's check it ourselves anyway */
if (to != backup) { if (to != backup) {
@ -2078,7 +2176,7 @@ int main(int argc, char** argv)
if (optind < argc) { if (optind < argc) {
char* szFile = argv[optind]; char* szFile = argv[optind];
utilStripDoubleExtension(szFile, filename); utilStripDoubleExtension(szFile, filename, sizeof(filename));
char* p = strrchr(filename, '.'); char* p = strrchr(filename, '.');
if (p) if (p)
@ -2173,7 +2271,11 @@ int main(int argc, char** argv)
} else { } else {
soundInit(); soundInit();
cartridgeType = 0; cartridgeType = 0;
#if __STDC_WANT_SECURE_LIB__
strcpy_s(filename, sizeof(filename), "gnu_stub");
#else
strcpy(filename, "gnu_stub"); strcpy(filename, "gnu_stub");
#endif
g_rom = (uint8_t*)malloc(0x2000000); g_rom = (uint8_t*)malloc(0x2000000);
g_workRAM = (uint8_t*)calloc(1, 0x40000); g_workRAM = (uint8_t*)calloc(1, 0x40000);
g_bios = (uint8_t*)calloc(1, 0x4000); g_bios = (uint8_t*)calloc(1, 0x4000);
@ -2678,7 +2780,11 @@ void systemConsoleMessage(const char* msg)
struct tm now_time_broken; struct tm now_time_broken;
now_time = time(NULL); now_time = time(NULL);
#if __STDC_WANT_SECURE_LIB__
localtime_s(&now_time_broken, &now_time);
#else
now_time_broken = *(localtime(&now_time)); now_time_broken = *(localtime(&now_time));
#endif
fprintf( fprintf(
stdout, stdout,
"%02d:%02d:%02d %02d.%02d.%4d: %s\n", "%02d:%02d:%02d %02d.%02d.%4d: %s\n",
@ -2697,10 +2803,18 @@ void systemScreenMessage(const char* msg)
screenMessage = true; screenMessage = true;
screenMessageTime = systemGetClock(); screenMessageTime = systemGetClock();
if (strlen(msg) > 20) { if (strlen(msg) > 20) {
#if __STDC_WANT_SECURE_LIB__
strncpy_s(screenMessageBuffer, sizeof(screenMessageBuffer), msg, 20);
#else
strncpy(screenMessageBuffer, msg, 20); strncpy(screenMessageBuffer, msg, 20);
#endif
screenMessageBuffer[20] = 0; screenMessageBuffer[20] = 0;
} else } else
#if __STDC_WANT_SECURE_LIB__
strcpy_s(screenMessageBuffer, sizeof(screenMessageBuffer), msg);
#else
strcpy(screenMessageBuffer, msg); strcpy(screenMessageBuffer, msg);
#endif
systemConsoleMessage(msg); systemConsoleMessage(msg);
} }
@ -2799,7 +2913,11 @@ void log(const char* defaultMsg, ...)
static FILE* out = NULL; static FILE* out = NULL;
if (out == NULL) { if (out == NULL) {
#if __STDC_WANT_SECURE_LIB__
fopen_s(&out, "trace.log", "w");
#else
out = fopen("trace.log", "w"); out = fopen("trace.log", "w");
#endif
} }
va_list valist; va_list valist;

View File

@ -68,7 +68,11 @@ static char *xstrdup(const char *s)
return NULL; return NULL;
t = (char *)malloc(strlen(s) + 1); t = (char *)malloc(strlen(s) + 1);
if (t) { if (t) {
#if __STDC_WANT_SECURE_LIB__
strcpy_s(t, strlen(s) + 1, s);
#else
strcpy(t, s); strcpy(t, s);
#endif
} }
return t; return t;
} }

View File

@ -13,6 +13,7 @@
#if __STDC_WANT_SECURE_LIB__ #if __STDC_WANT_SECURE_LIB__
#define snprintf sprintf_s #define snprintf sprintf_s
#define sscanf sscanf_s
#endif #endif
/*---------------------------- Defines -------------------------------------*/ /*---------------------------- Defines -------------------------------------*/
@ -89,7 +90,11 @@ static char *strstrip(const char *s)
while (isspace((int)*s) && *s) while (isspace((int)*s) && *s)
s++; s++;
memset(l, 0, ASCIILINESZ + 1); memset(l, 0, ASCIILINESZ + 1);
#if __STDC_WANT_SECURE_LIB__
strcpy_s(l, sizeof(l), s);
#else
strcpy(l, s); strcpy(l, s);
#endif
last = l + strlen(l); last = l + strlen(l);
while (last > l) { while (last > l) {
if (!isspace((int)*(last - 1))) if (!isspace((int)*(last - 1)))
@ -577,7 +582,11 @@ static line_status iniparser_line(const char *input_line, char *section, char *k
char line[ASCIILINESZ + 1]; char line[ASCIILINESZ + 1];
int len; int len;
#if __STDC_WANT_SECURE_LIB__
strcpy_s(line, sizeof(line), strstrip(input_line));
#else
strcpy(line, strstrip(input_line)); strcpy(line, strstrip(input_line));
#endif
len = (int)strlen(line); len = (int)strlen(line);
sta = LINE_UNPROCESSED; sta = LINE_UNPROCESSED;
@ -589,6 +598,19 @@ static line_status iniparser_line(const char *input_line, char *section, char *k
sta = LINE_COMMENT; sta = LINE_COMMENT;
} else if (line[0] == '[' && line[len - 1] == ']') { } else if (line[0] == '[' && line[len - 1] == ']') {
/* Section name */ /* Section name */
#if __STDC_WANT_SECURE_LIB__
sscanf_s(line, "[%[^]]", section, (unsigned)_countof(section));
strcpy_s(section, ASCIILINESZ, strstrip(section));
strcpy_s(section, ASCIILINESZ, strlwc(section));
sta = LINE_SECTION;
} else if (sscanf_s(line, "%[^=] = \"%[^\"]\"", key, (unsigned)_countof(key), value, (unsigned)_countof(value)) == 2 ||
sscanf_s(line, "%[^=] = '%[^\']'", key, (unsigned)_countof(key), value, (unsigned)_countof(value)) == 2 ||
sscanf_s(line, "%[^=] = %[^;#]", key, (unsigned)_countof(key), value, (unsigned)_countof(value)) == 2) {
/* Usual key=value, with or without comments */
strcpy_s(key, ASCIILINESZ, strstrip(key));
strcpy_s(key, ASCIILINESZ, strlwc(key));
strcpy_s(value, ASCIILINESZ, strstrip(value));
#else
sscanf(line, "[%[^]]", section); sscanf(line, "[%[^]]", section);
strcpy(section, strstrip(section)); strcpy(section, strstrip(section));
strcpy(section, strlwc(section)); strcpy(section, strlwc(section));
@ -600,6 +622,7 @@ static line_status iniparser_line(const char *input_line, char *section, char *k
strcpy(key, strstrip(key)); strcpy(key, strstrip(key));
strcpy(key, strlwc(key)); strcpy(key, strlwc(key));
strcpy(value, strstrip(value)); strcpy(value, strstrip(value));
#endif
/* /*
* sscanf cannot handle '' or "" as empty values * sscanf cannot handle '' or "" as empty values
* this is done here * this is done here
@ -608,16 +631,26 @@ static line_status iniparser_line(const char *input_line, char *section, char *k
value[0] = 0; value[0] = 0;
} }
sta = LINE_VALUE; sta = LINE_VALUE;
#if __STDC_WANT_SECURE_LIB__
} else if (sscanf_s(line, "%[^=] = %[;#]", key, (unsigned)_countof(key), value, (unsigned)_countof(value)) == 2 ||
sscanf_s(line, "%[^=] %[=]", key, (unsigned)_countof(key), value, (unsigned)_countof(value)) == 2) {
#else
} else if (sscanf(line, "%[^=] = %[;#]", key, value) == 2 || } else if (sscanf(line, "%[^=] = %[;#]", key, value) == 2 ||
sscanf(line, "%[^=] %[=]", key, value) == 2) { sscanf(line, "%[^=] %[=]", key, value) == 2) {
#endif
/* /*
* Special cases: * Special cases:
* key= * key=
* key=; * key=;
* key=# * key=#
*/ */
#if __STDC_WANT_SECURE_LIB__
strcpy_s(key, ASCIILINESZ, strstrip(key));
strcpy_s(key, ASCIILINESZ, strlwc(key));
#else
strcpy(key, strstrip(key)); strcpy(key, strstrip(key));
strcpy(key, strlwc(key)); strcpy(key, strlwc(key));
#endif
value[0] = 0; value[0] = 0;
sta = LINE_VALUE; sta = LINE_VALUE;
} else { } else {
@ -658,7 +691,12 @@ dictionary *iniparser_load(const char *ininame)
dictionary *dict; dictionary *dict;
#if __STDC_WANT_SECURE_LIB__
fopen_s(&in, ininame, "r");
if (in == NULL) {
#else
if ((in = fopen(ininame, "r")) == NULL) { if ((in = fopen(ininame, "r")) == NULL) {
#endif
fprintf(stderr, "iniparser: cannot open %s\n", ininame); fprintf(stderr, "iniparser: cannot open %s\n", ininame);
return NULL; return NULL;
} }

View File

@ -48,9 +48,15 @@ const char* msg_prefix = "bin2c: ";
void format_perror(const char* fmt, va_list args) { void format_perror(const char* fmt, va_list args) {
static char error_str[MSG_SIZE]; static char error_str[MSG_SIZE];
static char fmt_str[MSG_SIZE]; static char fmt_str[MSG_SIZE];
#if __STDC_WANT_SECURE_LIB__
strcpy_s(fmt_str, sizeof(fmt_str), msg_prefix);
strncat_s(fmt_str, sizeof(fmt_str), fmt, MSG_SIZE - strlen(msg_prefix));
vsprintf_s(error_str, MSG_SIZE, fmt_str, args);
#else
strcpy(fmt_str, msg_prefix); strcpy(fmt_str, msg_prefix);
strncat(fmt_str, fmt, MSG_SIZE - strlen(msg_prefix)); strncat(fmt_str, fmt, MSG_SIZE - strlen(msg_prefix));
vsnprintf(error_str, MSG_SIZE, fmt_str, args); vsnprintf(error_str, MSG_SIZE, fmt_str, args);
#endif
perror(error_str); perror(error_str);
} }
@ -80,7 +86,11 @@ char* file_name_to_identifier(const char* file_name_cstr) {
size_t file_name_len = 0; size_t file_name_len = 0;
int between_tokens = 0; int between_tokens = 0;
#if __STDC_WANT_SECURE_LIB__
mbstowcs_s(&file_name_len, file_name, WBUF_SIZE, file_name_cstr, BUF_SIZE - 1);
#else
file_name_len = mbstowcs(file_name, file_name_cstr, BUF_SIZE - 1); file_name_len = mbstowcs(file_name, file_name_cstr, BUF_SIZE - 1);
#endif
if (file_name_len == (size_t)(-1)) { if (file_name_len == (size_t)(-1)) {
die("cannot convert '%s' to locale representation", file_name_cstr); die("cannot convert '%s' to locale representation", file_name_cstr);
} }
@ -96,7 +106,12 @@ char* file_name_to_identifier(const char* file_name_cstr) {
between_tokens = 0; between_tokens = 0;
} }
else if (!between_tokens) { else if (!between_tokens) {
#if __STDC_WANT_SECURE_LIB__
size_t identifier_ptr_sz = 0;
mbstowcs_s(&identifier_ptr_sz, identifier_ptr, WBUF_SIZE, "_", 1);
#else
mbstowcs(identifier_ptr, "_", 1); mbstowcs(identifier_ptr, "_", 1);
#endif
identifier_ptr++; identifier_ptr++;
between_tokens = 1; between_tokens = 1;
} }
@ -109,7 +124,13 @@ char* file_name_to_identifier(const char* file_name_cstr) {
*identifier_ptr = 0; *identifier_ptr = 0;
#if __STDC_WANT_SECURE_LIB__
size_t identifier_cstr_sz = 0;
wcstombs_s(&identifier_cstr_sz, identifier_cstr, BUF_SIZE, identifier, WBUF_SIZE - 1);
if (identifier_cstr_sz == (size_t)(-1))
#else
if (wcstombs(identifier_cstr, identifier, WBUF_SIZE - 1) == (size_t)(-1)) if (wcstombs(identifier_cstr, identifier, WBUF_SIZE - 1) == (size_t)(-1))
#endif
die("failed to convert wide character string to bytes"); die("failed to convert wide character string to bytes");
free(file_name); free(file_name);
@ -151,10 +172,19 @@ void die_usage(const char* fmt, ...) {
static char fmt_str[MSG_SIZE]; static char fmt_str[MSG_SIZE];
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
#if __STDC_WANT_SECURE_LIB__
strcpy_s(fmt_str, sizeof(fmt_str), msg_prefix);
#else
strcpy(fmt_str, msg_prefix); strcpy(fmt_str, msg_prefix);
#endif
// Need to reserve 1 byte for the newline. // Need to reserve 1 byte for the newline.
#if __STDC_WANT_SECURE_LIB__
strncat_s(fmt_str, sizeof(fmt_str), fmt, MSG_SIZE - strlen(msg_prefix) - 1);
strcat_s(fmt_str, sizeof(fmt_str), "\n");
#else
strncat(fmt_str, fmt, MSG_SIZE - strlen(msg_prefix) - 1); strncat(fmt_str, fmt, MSG_SIZE - strlen(msg_prefix) - 1);
strcat(fmt_str, "\n"); strcat(fmt_str, "\n");
#endif
vfprintf(stderr, fmt_str, args); vfprintf(stderr, fmt_str, args);
va_end(args); va_end(args);
usage(1); usage(1);
@ -200,7 +230,11 @@ int main(int argc, const char** argv) {
if (!in_file_name || !strcmp(in_file_name, "-")) { if (!in_file_name || !strcmp(in_file_name, "-")) {
in_file = stdin; in_file = stdin;
} else { } else {
#if __STDC_WANT_SECURE_LIB__
fopen_s(&in_file, in_file_name, "rb");
#else
in_file = fopen(in_file_name, "rb"); in_file = fopen(in_file_name, "rb");
#endif
if (!in_file) { if (!in_file) {
die("can't open input file '%s'", in_file_name); die("can't open input file '%s'", in_file_name);
} }
@ -209,7 +243,11 @@ int main(int argc, const char** argv) {
if (!out_file_name || !strcmp(out_file_name, "-")) { if (!out_file_name || !strcmp(out_file_name, "-")) {
out_file = stdout; out_file = stdout;
} else { } else {
#if __STDC_WANT_SECURE_LIB__
fopen_s(&out_file, out_file_name, "w");
#else
out_file = fopen(out_file_name, "w"); out_file = fopen(out_file_name, "w");
#endif
if (!out_file) { if (!out_file) {
die("can't open output file '%s'", out_file_name); die("can't open output file '%s'", out_file_name);
} }

View File

@ -67,6 +67,10 @@ const
#undef wxvbam #undef wxvbam
#endif #endif
#if __STDC_WANT_SECURE_LIB__
#define sscanf sscanf_s
#endif
#define GetXRCDialog(n) \ #define GetXRCDialog(n) \
wxStaticCast(wxGetApp().frame->FindWindowByName(n), wxDialog) wxStaticCast(wxGetApp().frame->FindWindowByName(n), wxDialog)
@ -650,7 +654,11 @@ public:
} else if (ce_desc != odesc) { } else if (ce_desc != odesc) {
*dirty = true; *dirty = true;
char* p = isgb ? gbCheatList[id].cheatDesc : cheatsList[id].desc; char* p = isgb ? gbCheatList[id].cheatDesc : cheatsList[id].desc;
#if __STDC_WANT_SECURE_LIB__
strncpy_s(p, sizeof(cheatsList[0].desc), ce_desc.utf8_str(), sizeof(cheatsList[0].desc));
#else
strncpy(p, ce_desc.utf8_str(), sizeof(cheatsList[0].desc)); strncpy(p, ce_desc.utf8_str(), sizeof(cheatsList[0].desc));
#endif
p[sizeof(cheatsList[0].desc) - 1] = 0; p[sizeof(cheatsList[0].desc) - 1] = 0;
item1.SetId(id); item1.SetId(id);
item1.SetText(wxString(p, wxConvUTF8)); item1.SetText(wxString(p, wxConvUTF8));

View File

@ -107,8 +107,15 @@ int __stdcall WinMain(HINSTANCE hInstance,
// https://github.com/dolphin-emu/dolphin/blob/6cf99195c645f54d54c72322ad0312a0e56bc985/Source/Core/DolphinQt/Main.cpp#L112 // https://github.com/dolphin-emu/dolphin/blob/6cf99195c645f54d54c72322ad0312a0e56bc985/Source/Core/DolphinQt/Main.cpp#L112
HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
if (console_attached && stdout_handle) { if (console_attached && stdout_handle) {
#if __STDC_WANT_SECURE_LIB__
FILE *ostream;
FILE *estream;
freopen_s(&ostream, "CONOUT$", "w", stdout);
freopen_s(&estream, "CONOUT$", "w", stderr);
#else
freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr); freopen("CONOUT$", "w", stderr);
#endif
} }
// Set up logging. // Set up logging.

View File

@ -56,7 +56,13 @@ std::FILE* openFile(const ghc::filesystem::path& filename, std::string mode)
{ {
#ifdef SFML_SYSTEM_WINDOWS #ifdef SFML_SYSTEM_WINDOWS
const std::wstring wmode(mode.begin(), mode.end()); const std::wstring wmode(mode.begin(), mode.end());
#if __STDC_WANT_SECURE_LIB__
std::FILE* ret = NULL;
_wfopen_s(&ret, filename.c_str(), wmode.data());
return ret;
#else
return _wfopen(filename.c_str(), wmode.data()); return _wfopen(filename.c_str(), wmode.data());
#endif
#else #else
return std::fopen(filename.c_str(), mode.data()); return std::fopen(filename.c_str(), mode.data());
#endif #endif