Revert "FileSystem: Allow ::openReadRaw() to break out of game directory"

This reverts commit d45a400227.
Causes memory corruption in its current state.
This commit is contained in:
Ancurio 2021-09-27 18:23:16 +02:00
parent ddde452544
commit 3d6b7d3b75
5 changed files with 39 additions and 32 deletions

View File

@ -33,6 +33,7 @@ fileIntFreeInstance(void *inst)
{ {
SDL_RWops *ops = static_cast<SDL_RWops*>(inst); SDL_RWops *ops = static_cast<SDL_RWops*>(inst);
SDL_RWclose(ops);
SDL_FreeRW(ops); SDL_FreeRW(ops);
} }
@ -41,14 +42,16 @@ DEF_TYPE_CUSTOMFREE(FileInt, fileIntFreeInstance);
static VALUE static VALUE
fileIntForPath(const char *path, bool rubyExc) fileIntForPath(const char *path, bool rubyExc)
{ {
SDL_RWops *ops; SDL_RWops *ops = SDL_AllocRW();
try try
{ {
ops = shState->fileSystem().openReadRaw(path); shState->fileSystem().openReadRaw(*ops, path);
} }
catch (const Exception &e) catch (const Exception &e)
{ {
SDL_FreeRW(ops);
if (rubyExc) if (rubyExc)
raiseRbExc(e); raiseRbExc(e);
else else

View File

@ -273,23 +273,23 @@ runRMXPScripts(mrb_state *mrb, mrbc_context *ctx)
/* We use a secondary util state to unmarshal the scripts */ /* We use a secondary util state to unmarshal the scripts */
mrb_state *scriptMrb = mrb_open(); mrb_state *scriptMrb = mrb_open();
SDL_RWops ops;
SDL_RWops *ops = shState->fileSystem().openReadRaw(scriptPack.c_str()); shState->fileSystem().openReadRaw(ops, scriptPack.c_str());
mrb_value scriptArray = mrb_nil_value(); mrb_value scriptArray = mrb_nil_value();
std::string readError; std::string readError;
try try
{ {
scriptArray = marshalLoadInt(scriptMrb, ops); scriptArray = marshalLoadInt(scriptMrb, &ops);
} }
catch (const Exception &e) catch (const Exception &e)
{ {
readError = std::string(": ") + e.msg; readError = std::string(": ") + e.msg;
} }
SDL_RWclose(ops); SDL_RWclose(&ops);
SDL_FreeRW(ops);
if (!mrb_array_p(scriptArray)) if (!mrb_array_p(scriptArray))
{ {

View File

@ -222,6 +222,15 @@ static int SDL_RWopsClose(SDL_RWops *ops)
return (result != 0) ? 0 : -1; return (result != 0) ? 0 : -1;
} }
static int SDL_RWopsCloseFree(SDL_RWops *ops)
{
int result = SDL_RWopsClose(ops);
SDL_FreeRW(ops);
return result;
}
/* Copies the first srcN characters from src into dst, /* Copies the first srcN characters from src into dst,
* or the full string if srcN == -1. Never writes more * or the full string if srcN == -1. Never writes more
* than dstMax, and guarantees dst to be null terminated. * than dstMax, and guarantees dst to be null terminated.
@ -263,12 +272,17 @@ findExt(const char *filename)
static void static void
initReadOps(PHYSFS_File *handle, initReadOps(PHYSFS_File *handle,
SDL_RWops &ops) SDL_RWops &ops,
bool freeOnClose)
{ {
ops.size = SDL_RWopsSize; ops.size = SDL_RWopsSize;
ops.seek = SDL_RWopsSeek; ops.seek = SDL_RWopsSeek;
ops.read = SDL_RWopsRead; ops.read = SDL_RWopsRead;
ops.write = SDL_RWopsWrite; ops.write = SDL_RWopsWrite;
if (freeOnClose)
ops.close = SDL_RWopsCloseFree;
else
ops.close = SDL_RWopsClose; ops.close = SDL_RWopsClose;
ops.type = SDL_RWOPS_PHYSFS; ops.type = SDL_RWOPS_PHYSFS;
@ -490,7 +504,7 @@ fontSetEnumCB (void *data, const char *dir, const char *fname)
return PHYSFS_ENUM_ERROR; return PHYSFS_ENUM_ERROR;
SDL_RWops ops; SDL_RWops ops;
initReadOps(handle, ops); initReadOps(handle, ops, false);
d->sfs->initFontSetCB(ops, filename); d->sfs->initFontSetCB(ops, filename);
@ -606,7 +620,7 @@ openReadEnumCB(void *d, const char *dirpath, const char *filename)
return PHYSFS_ENUM_ERROR; return PHYSFS_ENUM_ERROR;
} }
initReadOps(phys, data.ops); initReadOps(phys, data.ops, false);
const char *ext = findExt(filename); const char *ext = findExt(filename);
@ -670,27 +684,14 @@ void FileSystem::openRead(OpenHandler &handler, const char *filename)
throw Exception(Exception::NoFileError, "%s", filename); throw Exception(Exception::NoFileError, "%s", filename);
} }
SDL_RWops *FileSystem::openReadRaw(const char *filename) void FileSystem::openReadRaw(SDL_RWops &ops,
const char *filename,
bool freeOnClose)
{ {
SDL_RWops *ops;
PHYSFS_File *handle = PHYSFS_openRead(filename); PHYSFS_File *handle = PHYSFS_openRead(filename);
if (!handle) { assert(handle);
Debug() << "Couldn't open " << filename << " via PhysFS; trying normal FILE*";
ops = SDL_RWFromFile(filename, "rb");
if (!ops) { initReadOps(handle, ops, freeOnClose);
Debug() << "FILE* path failed too..";
throw Exception(Exception::NoFileError, "%s", filename);
}
}
else
{
ops = SDL_AllocRW();
initReadOps(handle, *ops);
}
return ops;
} }
bool FileSystem::exists(const char *filename) bool FileSystem::exists(const char *filename)

View File

@ -60,7 +60,9 @@ public:
const char *filename); const char *filename);
/* Circumvents extension supplementing */ /* Circumvents extension supplementing */
SDL_RWops *openReadRaw(const char *filename); void openReadRaw(SDL_RWops &ops,
const char *filename,
bool freeOnClose = false);
/* Does not perform extension supplementing */ /* Does not perform extension supplementing */
bool exists(const char *filename); bool exists(const char *filename);

View File

@ -167,13 +167,14 @@ _TTF_Font *SharedFontState::getFont(std::string family,
const char *path = !req.regular.empty() const char *path = !req.regular.empty()
? req.regular.c_str() : req.other.c_str(); ? req.regular.c_str() : req.other.c_str();
ops = shState->fileSystem().openReadRaw(path); ops = SDL_AllocRW();
shState->fileSystem().openReadRaw(*ops, path, true);
} }
// FIXME 0.9 is guesswork at this point // FIXME 0.9 is guesswork at this point
// float gamma = (96.0/45.0)*(5.0/14.0)*(size-5); // float gamma = (96.0/45.0)*(5.0/14.0)*(size-5);
// font = TTF_OpenFontRW(ops, 0, gamma /** .90*/); // font = TTF_OpenFontRW(ops, 1, gamma /** .90*/);
font = TTF_OpenFontRW(ops, 0, size* 0.90f); font = TTF_OpenFontRW(ops, 1, size* 0.90f);
if (!font) if (!font)
throw Exception(Exception::SDLError, "%s", SDL_GetError()); throw Exception(Exception::SDLError, "%s", SDL_GetError());