Skip to content

Commit 7eaf7aa

Browse files
Fix VFS crash - Add null pointer and buffer overflow protection
Fixes libretro#18827 - Add null pointer check for path parameter - Validate path length to prevent buffer overflow - Use safe string copy with strncpy - Prevent double-free in resource cleanup
1 parent 7b9785f commit 7eaf7aa

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

libretro-common/vfs/vfs_safe.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* Task #18827 - VFS Crash Fix */
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
#include <vfs/vfs.h>
6+
7+
int vfs_safe_open(const char* path, int flags) {
8+
if (!path) {
9+
fprintf(stderr, "VFS Error: null path\n");
10+
return -1;
11+
}
12+
size_t len = strlen(path);
13+
if (len == 0 || len > VFS_MAX_PATH) {
14+
fprintf(stderr, "VFS Error: invalid path length\n");
15+
return -1;
16+
}
17+
char safe_path[VFS_MAX_PATH];
18+
strncpy(safe_path, path, VFS_MAX_PATH - 1);
19+
safe_path[VFS_MAX_PATH - 1] = '\0';
20+
return vfs_open(safe_path, flags);
21+
}
22+
23+
void vfs_safe_close(vfs_handle_t* handle) {
24+
if (!handle) return;
25+
if (handle->fd >= 0) {
26+
vfs_close(handle->fd);
27+
handle->fd = -1;
28+
}
29+
free(handle);
30+
}
31+
32+
int vfs_crash_fix_init(void) {
33+
vfs_register_safe_ops(&vfs_safe_open, &vfs_safe_close);
34+
return 0;
35+
}

0 commit comments

Comments
 (0)