diff --git a/sol/compatibility/compat-5.3.c b/sol/compatibility/compat-5.3.c index a3cdcc49..6623a56b 100644 --- a/sol/compatibility/compat-5.3.c +++ b/sol/compatibility/compat-5.3.c @@ -15,6 +15,25 @@ /* definitions for Lua 5.1 only */ #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501 +#if defined(__GLIBC__) || defined(_POSIX_VERSION) || defined(__APPLE__) || (!defined (__MINGW32__) && defined(__GNUC__) && (__GNUC__ < 6)) +#ifndef COMPAT53_HAVE_STRERROR_R +#define COMPAT53_HAVE_STRERROR_R +#endif // have strerror_r +#if ((defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || (defined(_XOPEN_SOURCE) || _XOPEN_SOURCE >= 600)) && (!defined(_GNU_SOURCE) || !_GNU_SOURCE) +#ifndef COMPAT53_HAVE_STRERROR_R_XSI +#define COMPAT53_HAVE_STRERROR_R_XSI +#endif // XSI-Compliant strerror_r +#else +#ifndef COMPAT53_HAVE_STRERROR_R_GNU +#define COMPAT53_HAVE_STRERROR_R_GNU +#endif // GNU variant strerror_r +#endif // XSI/Posix vs. GNU strerror_r +#elif defined(_MSC_VER) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || defined(__STDC_LIB_EXT1__) +#ifndef COMPAT53_HAVE_STRERROR_S +#define COMPAT53_HAVE_STRERROR_S +#endif // GNU variant strerror_r +#endif // strerror_r vs. strerror_s vs. strerror usage + #ifndef COMPAT53_LUA_FILE_BUFFER_SIZE #define COMPAT53_LUA_FILE_BUFFER_SIZE 4096 #endif // Lua File Buffer Size @@ -350,36 +369,37 @@ COMPAT53_API void luaL_traceback (lua_State *L, lua_State *L1, COMPAT53_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { const char* s = NULL; int en = errno; /* calls to Lua API may change this value */ +#if defined(COMPAT53_HAVE_STRERROR_R) || defined(COMPAT53_HAVE_STRERROR_S) + char buf[512] = { 0 }; +#endif // buffer for threadsafe variants of strerror if possible if (stat) { lua_pushboolean(L, 1); return 1; } else { lua_pushnil(L); -#if defined(__GLIBC__) || defined(_POSIX_VERSION) || defined(__APPLE__) || (!defined (__MINGW32__) && defined(__GNUC__) && (__GNUC__ < 6)) +#if defined(COMPAT53_HAVE_STRERROR_R) /* use strerror_r here, because it's available on these specific platforms */ - char buf[512] = {0}; -#if ((defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || (defined(_XOPEN_SOURCE) || _XOPEN_SOURCE >= 600)) && (!defined(_GNU_SOURCE) || !_GNU_SOURCE) +#if defined(COMPAT53_HAVE_STRERROR_R_XSI) /* XSI Compliant */ strerror_r(en, buf, 512); s = buf; #else - /* GNU-specific which returns const char* */ + /* GNU-specific which returns const char* */ s = strerror_r(en, buf, 512); #endif -#elif defined(_MSC_VER) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) +#elif defined(COMPAT53_HAVE_STRERROR_S) /* for MSVC and other C11 implementations, use strerror_s * since it's provided by default by the libraries - */ - char buf[512] = {0}; + */ strerror_s(buf, 512, en); s = buf; #else /* fallback, but * strerror is not guaranteed to be threadsafe due to modifying * errno itself and some impls not locking a static buffer for it - * ... but it works in 99% of cases, so I don't need the reason to stop - * the train now + * ... but most known systems have threadsafe errno: this might only change + * if the locale is changed out from under someone while this function is being called */ s = strerror(en); #endif @@ -393,7 +413,7 @@ COMPAT53_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { } -static const char* compat53_f_parser_handler (lua_State *L, void *data, size_t *size) { +static const char* compat53_f_parser_handler (lua_State *, void *data, size_t *size) { struct compat53_f_parser_buffer *p = (struct compat53_f_parser_buffer*)data; size_t readcount = fread(p->buffer + p->start, sizeof(*p->buffer), sizeof(p->buffer), p->file); if (ferror(p->file) != 0 || feof(p->file) != 0) { @@ -417,10 +437,10 @@ static int checkmode (lua_State *L, const char *mode, const char *modename, int COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode) { FILE* fp; - int err; int c; int status = LUA_OK; #if defined(_MSC_VER) + int err = 0; err = fopen_s(&fp, filename, "rb"); if (err != 0) { fclose(fp); diff --git a/sol/protected_function.hpp b/sol/protected_function.hpp index bf24b88d..e49ea5aa 100644 --- a/sol/protected_function.hpp +++ b/sol/protected_function.hpp @@ -103,7 +103,7 @@ namespace sol { private: template call_status luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount, detail::protected_handler& h) const { - return static_cast(lua_pcallk(lua_state(), static_cast(argcount), static_cast(resultcount), h.stackindex, 0, nullptr)); + return static_cast(lua_pcall(lua_state(), static_cast(argcount), static_cast(resultcount), h.stackindex)); } template diff --git a/sol/unsafe_function.hpp b/sol/unsafe_function.hpp index c29480f0..087c78e3 100644 --- a/sol/unsafe_function.hpp +++ b/sol/unsafe_function.hpp @@ -33,7 +33,7 @@ namespace sol { class basic_function : public base_t { private: void luacall(std::ptrdiff_t argcount, std::ptrdiff_t resultcount) const { - lua_callk(lua_state(), static_cast(argcount), static_cast(resultcount), 0, nullptr); + lua_call(lua_state(), static_cast(argcount), static_cast(resultcount)); } template