Commit c09794c
committed
libretro-common/file: NULL-check mallocs and unwind resources on OOM
Four OOM-deref sites across the file-I/O subsystem:
nbio_linux.c (nbio_linux_open):
handle = (struct nbio_linux_t*)malloc(sizeof(struct nbio_linux_t));
handle->fd = fd; /* NULL-deref on OOM */
handle->ctx = ctx;
handle->len = lseek(fd, 0, SEEK_END);
handle->ptr = malloc(handle->len);
handle->busy = false;
Two back-to-back mallocs with no NULL checks between the deref'ing
field writes. Also leaks the open()'d fd and the io_setup()'d
aio context on any OOM. The sibling nbio_stdio_open (same file
family, line 121) already does this correctly with fclose-on-OOM
and the nested malloc NULL-check; _linux_open was the lone holdout.
nbio_unixmmap.c (nbio_mmap_unix_open):
handle = malloc(sizeof(struct nbio_mmap_unix_t));
handle->fd = fd; /* NULL-deref on OOM */
handle->map_flags = map_flags[mode];
handle->len = _len;
handle->ptr = ptr;
Same unchecked-handle-malloc pattern; on OOM leaks the fd and the
mmap'd region.
nbio_windowsmmap.c (nbio_mmap_win32_open):
handle = (struct nbio_mmap_win32_t*)malloc(sizeof(struct nbio_mmap_win32_t));
handle->file = file; /* NULL-deref on OOM */
handle->is_write = is_write;
handle->len = len.QuadPart;
handle->ptr = ptr;
Same, Win32 variant; on OOM leaks the HANDLE and the MapViewOfFile
mapping.
archive_file_7z.c (sevenzip_file_read):
*buf = malloc((size_t)(outsize + 1));
((char*)(*buf))[outsize] = '\0'; /* NULL-deref on OOM */
memcpy(*buf, output + offset, outsize);
Only one site in 7z extraction; on OOM NULL-derefs on the
NUL-terminator write. Unlike the nbio sites, this one sits
inside a for-loop that already has an error label; added
res = SZ_ERROR_MEM / outsize = -1 / break so the existing
'!(file_found && res == SZ_OK)' cleanup branch runs.
Fixes in all four cases: NULL-check each malloc, unwind everything
we already acquired above it, and return NULL / -1 as the
respective function's error signal. free(NULL) / close(-1) are
defined as safe no-ops where relevant but nothing here relies on
that - each unwind is explicit about what it's releasing.
Thread-safety: unchanged. nbio handles are owned by whichever
thread opened them (typically a task queue thread); the 7z
extractor runs on the main thread during content scans.
Reachability: nbio_*_open is the entry point for every async
file load in the engine - most relevant for menu thumbnails,
where hundreds of handles can be in flight simultaneously on
memory-constrained handhelds. 7z extraction runs whenever a
7z-packed core / content is loaded.1 parent 8521da9 commit c09794c
4 files changed
Lines changed: 56 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
269 | 269 | | |
270 | 270 | | |
271 | 271 | | |
272 | | - | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
273 | 284 | | |
274 | 285 | | |
275 | 286 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
116 | 127 | | |
117 | 128 | | |
118 | 129 | | |
119 | 130 | | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
120 | 142 | | |
121 | 143 | | |
122 | 144 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
87 | 98 | | |
88 | 99 | | |
89 | 100 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
115 | 126 | | |
116 | 127 | | |
117 | 128 | | |
| |||
0 commit comments