diff --git a/gtk/src/gtk_file.cpp b/gtk/src/gtk_file.cpp index 64422fcb..d86376be 100644 --- a/gtk/src/gtk_file.cpp +++ b/gtk/src/gtk_file.cpp @@ -234,7 +234,7 @@ S9xBasename (const char *f) const char * S9xBasenameNoExt (const char *f) { - static char filename[PATH_MAX + 1]; + static char filename[PATH_MAX]; const char *base, *ext; if (!(base = strrchr (f, SLASH_CHAR))) @@ -245,11 +245,11 @@ S9xBasenameNoExt (const char *f) ext = strrchr (f, '.'); if (!ext) - strncpy (filename, base, PATH_MAX); + sstrncpy (filename, base, PATH_MAX); else { int len = ext - base; - strncpy (filename, base, len); + sstrncpy (filename, base, len); filename[len] = '\0'; } @@ -284,7 +284,7 @@ S9xOpenSnapshotFile (const char *fname, bool8 read_only, STREAM *file) if (*drive || *dir == '/' || (*dir == '.' && (*(dir + 1) == '/'))) { - strncpy (filename, fname, PATH_MAX); + sstrncpy (filename, fname, PATH_MAX + 1); if (!file_exists (filename)) { diff --git a/port.h b/port.h index aeb22043..4a671f2f 100644 --- a/port.h +++ b/port.h @@ -9,6 +9,7 @@ #include #include +#include #include #ifndef __LIBRETRO__ #include @@ -148,8 +149,20 @@ void SetInfoDlgColor(unsigned char, unsigned char, unsigned char); #endif // __LIBRETRO__ #endif // __WIN32__ -#define ssnprintf(dst, size, fmt, ...) if (snprintf(dst, size, fmt, __VA_ARGS__) >= (int) size) dst[size - 1] = '\0'; -#define sstrncpy(dst, src, size) strncpy(dst, src, size - 1); dst[size - 1] = '\0'; +inline void ssnprintf(char *dst, size_t size, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + if (vsnprintf(dst, size, fmt, args) >= (int) size) + dst[size - 1] = '\0'; + va_end(args); +} + +inline void sstrncpy(char *dst, const char *src, size_t size) +{ + strncpy(dst, src, size - 1); + dst[size - 1] = '\0'; +} #if defined(__DJGPP) || defined(__WIN32__) #define SLASH_STR "\\"