Merge branch '22-exec-files-only' into 'public_next'

Fix exec crashing with out of memory if opening a directory

See merge request STJr/SRB2Internal!634
This commit is contained in:
Eidolon 2022-12-06 08:42:04 +00:00
commit 9a7d15761d
3 changed files with 38 additions and 2 deletions

View file

@ -104,6 +104,8 @@
#include <io.h>
#endif
FILE *fopenfile(const char*, const char*);
//#define NOMD5
// Uncheck this to compile debugging code

View file

@ -39,6 +39,7 @@
#define SUFFIX "*"
#define SLASH "\\"
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#ifndef INVALID_FILE_ATTRIBUTES
@ -307,6 +308,39 @@ closedir (DIR * dirp)
}
#endif
// fopen but it REALLY only works on regular files
// Turns out, on linux, anyway, you can fopen directories
// in read mode. (It's supposed to fail in write mode
// though!!)
FILE *fopenfile(const char *path, const char *mode)
{
FILE *h = fopen(path, mode);
if (h != NULL)
{
struct stat st;
int eno;
if (fstat(fileno(h), &st) == -1)
{
eno = errno;
}
else if (!S_ISREG(st.st_mode))
{
eno = EACCES; // set some kinda error
}
else
{
return h; // ok
}
fclose(h);
errno = eno;
}
return NULL;
}
static CV_PossibleValue_t addons_cons_t[] = {{0, "Default"},
#if 1
{1, "HOME"}, {2, "SRB2"},

View file

@ -273,8 +273,8 @@ size_t FIL_ReadFileTag(char const *name, UINT8 **buffer, INT32 tag)
size_t count, length;
UINT8 *buf;
if (FIL_ReadFileOK(name))
handle = fopen(name, "rb");
//if (FIL_ReadFileOK(name))
handle = fopenfile(name, "rb");
if (!handle)
return 0;