VFS: Early return NULL if attempting to map 0 bytes from a file

This commit is contained in:
Vicki Pfau 2022-09-27 05:33:25 -07:00
parent 55c2efa3ea
commit 31f798748b
4 changed files with 18 additions and 1 deletions

View File

@ -112,6 +112,7 @@ Misc:
- Qt: Resume crashed game when loading a save state - Qt: Resume crashed game when loading a save state
- SDL: Support exposing an axis directly as the gyro value (closes mgba.io/i/2531) - SDL: Support exposing an axis directly as the gyro value (closes mgba.io/i/2531)
- Windows: Attach to console if present - Windows: Attach to console if present
- VFS: Early return NULL if attempting to map 0 bytes from a file
- Vita: Add bilinear filtering option (closes mgba.io/i/344) - Vita: Add bilinear filtering option (closes mgba.io/i/344)
0.9.3: (2021-12-17) 0.9.3: (2021-12-17)

View File

@ -137,6 +137,9 @@ ssize_t _vf3dWrite(struct VFile* vf, const void* buffer, size_t size) {
static void* _vf3dMap(struct VFile* vf, size_t size, int flags) { static void* _vf3dMap(struct VFile* vf, size_t size, int flags) {
struct VFile3DS* vf3d = (struct VFile3DS*) vf; struct VFile3DS* vf3d = (struct VFile3DS*) vf;
UNUSED(flags); UNUSED(flags);
if (!size) {
return NULL;
}
void* buffer = anonymousMemoryMap(size); void* buffer = anonymousMemoryMap(size);
if (buffer) { if (buffer) {
u32 sizeRead; u32 sizeRead;

View File

@ -113,6 +113,9 @@ ssize_t _vfsceWrite(struct VFile* vf, const void* buffer, size_t size) {
static void* _vfsceMap(struct VFile* vf, size_t size, int flags) { static void* _vfsceMap(struct VFile* vf, size_t size, int flags) {
struct VFileSce* vfsce = (struct VFileSce*) vf; struct VFileSce* vfsce = (struct VFileSce*) vf;
UNUSED(flags); UNUSED(flags);
if (!size) {
return NULL;
}
void* buffer = anonymousMemoryMap(size); void* buffer = anonymousMemoryMap(size);
if (buffer) { if (buffer) {
SceOff cur = sceIoLseek(vfsce->fd, 0, SEEK_CUR); SceOff cur = sceIoLseek(vfsce->fd, 0, SEEK_CUR);

View File

@ -134,6 +134,9 @@ ssize_t _vfdWrite(struct VFile* vf, const void* buffer, size_t size) {
#ifdef _POSIX_MAPPED_FILES #ifdef _POSIX_MAPPED_FILES
static void* _vfdMap(struct VFile* vf, size_t size, int flags) { static void* _vfdMap(struct VFile* vf, size_t size, int flags) {
struct VFileFD* vfd = (struct VFileFD*) vf; struct VFileFD* vfd = (struct VFileFD*) vf;
if (!size) {
return NULL;
}
int mmapFlags = MAP_PRIVATE; int mmapFlags = MAP_PRIVATE;
if (flags & MAP_WRITE) { if (flags & MAP_WRITE) {
mmapFlags = MAP_SHARED; mmapFlags = MAP_SHARED;
@ -153,6 +156,9 @@ static void _vfdUnmap(struct VFile* vf, void* memory, size_t size) {
#elif defined(_WIN32) #elif defined(_WIN32)
static void* _vfdMap(struct VFile* vf, size_t size, int flags) { static void* _vfdMap(struct VFile* vf, size_t size, int flags) {
struct VFileFD* vfd = (struct VFileFD*) vf; struct VFileFD* vfd = (struct VFileFD*) vf;
if (!size) {
return NULL;
}
int createFlags = PAGE_WRITECOPY; int createFlags = PAGE_WRITECOPY;
int mapFiles = FILE_MAP_COPY; int mapFiles = FILE_MAP_COPY;
if (flags & MAP_WRITE) { if (flags & MAP_WRITE) {
@ -192,12 +198,16 @@ static void _vfdUnmap(struct VFile* vf, void* memory, size_t size) {
#else #else
static void* _vfdMap(struct VFile* vf, size_t size, int flags) { static void* _vfdMap(struct VFile* vf, size_t size, int flags) {
struct VFileFD* vfd = (struct VFileFD*) vf; struct VFileFD* vfd = (struct VFileFD*) vf;
if (!size) {
vfd->writable = false;
return NULL;
}
if (flags & MAP_WRITE) { if (flags & MAP_WRITE) {
vfd->writable = true; vfd->writable = true;
} }
void* mem = anonymousMemoryMap(size); void* mem = anonymousMemoryMap(size);
if (!mem) { if (!mem) {
return 0; return NULL;
} }
off_t pos = lseek(vfd->fd, 0, SEEK_CUR); off_t pos = lseek(vfd->fd, 0, SEEK_CUR);