add compat53_ prefix where appropriate

make sure all checking macros define to 0 if not defined
make sure to vet use of `fsopen` on Windows since it it may or may not lock: use non-locking security version by default if possible
This commit is contained in:
ThePhD 2017-09-11 13:15:27 -04:00
parent b4c1ab0915
commit ac849a54c7
2 changed files with 140 additions and 64 deletions

View File

@ -20,8 +20,8 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// This file was generated with a script. // This file was generated with a script.
// Generated 2017-09-11 15:41:55.640032 UTC // Generated 2017-09-11 17:13:51.701888 UTC
// This header was generated with sol v2.18.2 (revision 7a29676) // This header was generated with sol v2.18.2 (revision b4c1ab0)
// https://github.com/ThePhD/sol2 // https://github.com/ThePhD/sol2
#ifndef SOL_SINGLE_INCLUDE_HPP #ifndef SOL_SINGLE_INCLUDE_HPP
@ -1933,6 +1933,18 @@ COMPAT53_API void luaL_requiref (lua_State *L, const char *modname,
/* definitions for Lua 5.1 only */ /* definitions for Lua 5.1 only */
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501 #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
#ifndef COMPAT53_FOPEN_NO_LOCK
# if defined(_MSC_VER)
# define COMPAT53_FOPEN_NO_LOCK 1
# else /* otherwise */
# define COMPAT53_FOPEN_NO_LOCK 0
# endif /* VC++ only so far */
#endif /* No-lock fopen_s usage if possible */
#if defined(_MSC_VER) && COMPAT53_FOPEN_NO_LOCK
#include <share.h>
#endif // VC++ _fsopen for share-allowed file read
#ifndef COMPAT53_HAVE_STRERROR_R #ifndef COMPAT53_HAVE_STRERROR_R
# if defined(__GLIBC__) || defined(_POSIX_VERSION) || defined(__APPLE__) || (!defined (__MINGW32__) && defined(__GNUC__) && (__GNUC__ < 6)) # if defined(__GLIBC__) || defined(_POSIX_VERSION) || defined(__APPLE__) || (!defined (__MINGW32__) && defined(__GNUC__) && (__GNUC__ < 6))
# define COMPAT53_HAVE_STRERROR_R 1 # define COMPAT53_HAVE_STRERROR_R 1
@ -1940,13 +1952,25 @@ COMPAT53_API void luaL_requiref (lua_State *L, const char *modname,
# ifndef COMPAT53_HAVE_STRERROR_R_XSI # ifndef COMPAT53_HAVE_STRERROR_R_XSI
# define COMPAT53_HAVE_STRERROR_R_XSI 1 # define COMPAT53_HAVE_STRERROR_R_XSI 1
# endif /* XSI-Compliant strerror_r */ # endif /* XSI-Compliant strerror_r */
# ifndef COMPAT53_HAVE_STRERROR_R_GNU
# define COMPAT53_HAVE_STRERROR_R_GNU 0
# endif /* GNU strerror_r */
# else /* XSI/Posix vs. GNU strerror_r */ # else /* XSI/Posix vs. GNU strerror_r */
# ifndef COMPAT53_HAVE_STRERROR_R_GNU # ifndef COMPAT53_HAVE_STRERROR_R_GNU
# define COMPAT53_HAVE_STRERROR_R_GNU 1 # define COMPAT53_HAVE_STRERROR_R_GNU 1
# endif /* GNU variant strerror_r */ # endif /* GNU variant strerror_r */
# ifndef COMPAT53_HAVE_STRERROR_R_XSI
# define COMPAT53_HAVE_STRERROR_R_XSI 0
# endif /* XSI strerror_r */
# endif /* XSI/Posix vs. GNU strerror_r */ # endif /* XSI/Posix vs. GNU strerror_r */
# else /* none of the defines matched: define to 0 */ # else /* none of the defines matched: define to 0 */
# define COMPAT53_HAVE_STRERROR_R 0 # define COMPAT53_HAVE_STRERROR_R 0
# ifndef COMPAT53_HAVE_STRERROR_R_XSI
# define COMPAT53_HAVE_STRERROR_R_XSI 0
# endif /* XSI strerror_r */
# ifndef COMPAT53_HAVE_STRERROR_R_GNU
# define COMPAT53_HAVE_STRERROR_R_GNU 0
# endif /* GNU strerror_r */
# endif /* have strerror_r of some form */ # endif /* have strerror_r of some form */
#endif /* strerror_r */ #endif /* strerror_r */
@ -1965,7 +1989,7 @@ COMPAT53_API void luaL_requiref (lua_State *L, const char *modname,
static char* compat53_strerror(int en, char* buff, size_t sz) { static char* compat53_strerror(int en, char* buff, size_t sz) {
#if COMPAT53_HAVE_STRERROR_R #if COMPAT53_HAVE_STRERROR_R
/* use strerror_r here, because it's available on these specific platforms */ /* use strerror_r here, because it's available on these specific platforms */
#if defined(COMPAT53_HAVE_STRERROR_R_XSI) #if COMPAT53_HAVE_STRERROR_R_XSI
/* XSI Compliant */ /* XSI Compliant */
strerror_r(en, buff, sz); strerror_r(en, buff, sz);
return buff; return buff;
@ -2329,7 +2353,7 @@ typedef struct compat53_LoadF {
char buff[COMPAT53_LUA_FILE_BUFFER_SIZE]; /* area for reading file */ char buff[COMPAT53_LUA_FILE_BUFFER_SIZE]; /* area for reading file */
} compat53_LoadF; } compat53_LoadF;
static const char *getF (lua_State *L, void *ud, size_t *size) { static const char *compat53_getF (lua_State *L, void *ud, size_t *size) {
compat53_LoadF *lf = (compat53_LoadF *)ud; compat53_LoadF *lf = (compat53_LoadF *)ud;
(void)L; /* not used */ (void)L; /* not used */
if (lf->n > 0) { /* are there pre-read characters to be read? */ if (lf->n > 0) { /* are there pre-read characters to be read? */
@ -2338,7 +2362,7 @@ static const char *getF (lua_State *L, void *ud, size_t *size) {
} }
else { /* read a block from file */ else { /* read a block from file */
/* 'fread' can return > 0 *and* set the EOF flag. If next call to /* 'fread' can return > 0 *and* set the EOF flag. If next call to
'getF' called 'fread', it might still wait for user input. 'compat53_getF' called 'fread', it might still wait for user input.
The next check avoids this problem. */ The next check avoids this problem. */
if (feof(lf->f)) return NULL; if (feof(lf->f)) return NULL;
*size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */ *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */
@ -2400,13 +2424,27 @@ COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char
else { else {
lua_pushfstring(L, "@%s", filename); lua_pushfstring(L, "@%s", filename);
#if defined(_MSC_VER) #if defined(_MSC_VER)
/* a quick check shows that fopen_s this goes back to VS 2005, possibly even before that /* a quick check shows that fopen_s this goes back to VS 2005,
* and _fsopen goes back to VS 2003 .NET, possibly even before that
* so we don't need to do any version number checks, * so we don't need to do any version number checks,
* since this has been there since forever * since this has been there since forever
*/ */
if (fopen_s(&lf.f, filename, "r") != 0) return compat53_errfile(L, "open", fnameindex);
/* TO USER: if you want the behavior of typical fopen_s/fopen,
* which does lock the file on VC++, define the macro used below
*/
#if COMPAT53_FOPEN_NO_LOCK
lf.f = _fsopen(filename, mode, _SH_DENYNO); /* do not lock the file in any way */
if (lf.f == NULL) {
return compat53_errfile(L, "open", fnameindex);
}
#else /* use default locking version */
if (fopen_s(&lf.f, filename, "r") != 0) {
return compat53_errfile(L, "open", fnameindex);
}
#endif /* Locking vs. No-locking fopen variants */
#else #else
lf.f = fopen(filename, "r"); lf.f = fopen(filename, "r"); /* default stdlib doesn't forcefully lock files here */
if (lf.f == NULL) return compat53_errfile(L, "open", fnameindex); if (lf.f == NULL) return compat53_errfile(L, "open", fnameindex);
#endif #endif
} }
@ -2435,7 +2473,7 @@ COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char
} }
if (c != EOF) if (c != EOF)
lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
status = lua_load(L, getF, &lf, lua_tostring(L, -1)); status = lua_load(L, &compat53_getF, &lf, lua_tostring(L, -1));
readstatus = ferror(lf.f); readstatus = ferror(lf.f);
if (filename) fclose(lf.f); /* close file (even in case of errors) */ if (filename) fclose(lf.f); /* close file (even in case of errors) */
if (readstatus) { if (readstatus) {

View File

@ -15,6 +15,18 @@
/* definitions for Lua 5.1 only */ /* definitions for Lua 5.1 only */
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501 #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
#ifndef COMPAT53_FOPEN_NO_LOCK
# if defined(_MSC_VER)
# define COMPAT53_FOPEN_NO_LOCK 1
# else /* otherwise */
# define COMPAT53_FOPEN_NO_LOCK 0
# endif /* VC++ only so far */
#endif /* No-lock fopen_s usage if possible */
#if defined(_MSC_VER) && COMPAT53_FOPEN_NO_LOCK
#include <share.h>
#endif // VC++ _fsopen for share-allowed file read
#ifndef COMPAT53_HAVE_STRERROR_R #ifndef COMPAT53_HAVE_STRERROR_R
# if defined(__GLIBC__) || defined(_POSIX_VERSION) || defined(__APPLE__) || (!defined (__MINGW32__) && defined(__GNUC__) && (__GNUC__ < 6)) # if defined(__GLIBC__) || defined(_POSIX_VERSION) || defined(__APPLE__) || (!defined (__MINGW32__) && defined(__GNUC__) && (__GNUC__ < 6))
# define COMPAT53_HAVE_STRERROR_R 1 # define COMPAT53_HAVE_STRERROR_R 1
@ -22,13 +34,25 @@
# ifndef COMPAT53_HAVE_STRERROR_R_XSI # ifndef COMPAT53_HAVE_STRERROR_R_XSI
# define COMPAT53_HAVE_STRERROR_R_XSI 1 # define COMPAT53_HAVE_STRERROR_R_XSI 1
# endif /* XSI-Compliant strerror_r */ # endif /* XSI-Compliant strerror_r */
# ifndef COMPAT53_HAVE_STRERROR_R_GNU
# define COMPAT53_HAVE_STRERROR_R_GNU 0
# endif /* GNU strerror_r */
# else /* XSI/Posix vs. GNU strerror_r */ # else /* XSI/Posix vs. GNU strerror_r */
# ifndef COMPAT53_HAVE_STRERROR_R_GNU # ifndef COMPAT53_HAVE_STRERROR_R_GNU
# define COMPAT53_HAVE_STRERROR_R_GNU 1 # define COMPAT53_HAVE_STRERROR_R_GNU 1
# endif /* GNU variant strerror_r */ # endif /* GNU variant strerror_r */
# ifndef COMPAT53_HAVE_STRERROR_R_XSI
# define COMPAT53_HAVE_STRERROR_R_XSI 0
# endif /* XSI strerror_r */
# endif /* XSI/Posix vs. GNU strerror_r */ # endif /* XSI/Posix vs. GNU strerror_r */
# else /* none of the defines matched: define to 0 */ # else /* none of the defines matched: define to 0 */
# define COMPAT53_HAVE_STRERROR_R 0 # define COMPAT53_HAVE_STRERROR_R 0
# ifndef COMPAT53_HAVE_STRERROR_R_XSI
# define COMPAT53_HAVE_STRERROR_R_XSI 0
# endif /* XSI strerror_r */
# ifndef COMPAT53_HAVE_STRERROR_R_GNU
# define COMPAT53_HAVE_STRERROR_R_GNU 0
# endif /* GNU strerror_r */
# endif /* have strerror_r of some form */ # endif /* have strerror_r of some form */
#endif /* strerror_r */ #endif /* strerror_r */
@ -47,7 +71,7 @@
static char* compat53_strerror(int en, char* buff, size_t sz) { static char* compat53_strerror(int en, char* buff, size_t sz) {
#if COMPAT53_HAVE_STRERROR_R #if COMPAT53_HAVE_STRERROR_R
/* use strerror_r here, because it's available on these specific platforms */ /* use strerror_r here, because it's available on these specific platforms */
#if defined(COMPAT53_HAVE_STRERROR_R_XSI) #if COMPAT53_HAVE_STRERROR_R_XSI
/* XSI Compliant */ /* XSI Compliant */
strerror_r(en, buff, sz); strerror_r(en, buff, sz);
return buff; return buff;
@ -431,7 +455,7 @@ typedef struct compat53_LoadF {
} compat53_LoadF; } compat53_LoadF;
static const char *getF (lua_State *L, void *ud, size_t *size) { static const char *compat53_getF (lua_State *L, void *ud, size_t *size) {
compat53_LoadF *lf = (compat53_LoadF *)ud; compat53_LoadF *lf = (compat53_LoadF *)ud;
(void)L; /* not used */ (void)L; /* not used */
if (lf->n > 0) { /* are there pre-read characters to be read? */ if (lf->n > 0) { /* are there pre-read characters to be read? */
@ -440,7 +464,7 @@ static const char *getF (lua_State *L, void *ud, size_t *size) {
} }
else { /* read a block from file */ else { /* read a block from file */
/* 'fread' can return > 0 *and* set the EOF flag. If next call to /* 'fread' can return > 0 *and* set the EOF flag. If next call to
'getF' called 'fread', it might still wait for user input. 'compat53_getF' called 'fread', it might still wait for user input.
The next check avoids this problem. */ The next check avoids this problem. */
if (feof(lf->f)) return NULL; if (feof(lf->f)) return NULL;
*size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */ *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); /* read block */
@ -506,13 +530,27 @@ COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char
else { else {
lua_pushfstring(L, "@%s", filename); lua_pushfstring(L, "@%s", filename);
#if defined(_MSC_VER) #if defined(_MSC_VER)
/* a quick check shows that fopen_s this goes back to VS 2005, possibly even before that /* a quick check shows that fopen_s this goes back to VS 2005,
* and _fsopen goes back to VS 2003 .NET, possibly even before that
* so we don't need to do any version number checks, * so we don't need to do any version number checks,
* since this has been there since forever * since this has been there since forever
*/ */
if (fopen_s(&lf.f, filename, "r") != 0) return compat53_errfile(L, "open", fnameindex);
/* TO USER: if you want the behavior of typical fopen_s/fopen,
* which does lock the file on VC++, define the macro used below
*/
#if COMPAT53_FOPEN_NO_LOCK
lf.f = _fsopen(filename, mode, _SH_DENYNO); /* do not lock the file in any way */
if (lf.f == NULL) {
return compat53_errfile(L, "open", fnameindex);
}
#else /* use default locking version */
if (fopen_s(&lf.f, filename, "r") != 0) {
return compat53_errfile(L, "open", fnameindex);
}
#endif /* Locking vs. No-locking fopen variants */
#else #else
lf.f = fopen(filename, "r"); lf.f = fopen(filename, "r"); /* default stdlib doesn't forcefully lock files here */
if (lf.f == NULL) return compat53_errfile(L, "open", fnameindex); if (lf.f == NULL) return compat53_errfile(L, "open", fnameindex);
#endif #endif
} }
@ -541,7 +579,7 @@ COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char
} }
if (c != EOF) if (c != EOF)
lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
status = lua_load(L, getF, &lf, lua_tostring(L, -1)); status = lua_load(L, &compat53_getF, &lf, lua_tostring(L, -1));
readstatus = ferror(lf.f); readstatus = ferror(lf.f);
if (filename) fclose(lf.f); /* close file (even in case of errors) */ if (filename) fclose(lf.f); /* close file (even in case of errors) */
if (readstatus) { if (readstatus) {