Commit 6300509
Fix use-after-free in UMS Filesystem name/mount_name (#18)
* Fix use-after-free in UMS Filesystem name/mount_name
Filesystem stored name/mount_name as std::string_view referencing
strings owned by UmsController::Device. libusbhsfs's populate
callback clears the devices vector on every event (partition
discovery, mount/unmount), invalidating those views. Subsequent
file access via mount_name.data() triggered a use-after-free and
crashed when accessing USB storage.
Make Filesystem own its strings (std::string). Also fix iterator
invalidation in ums_devices_changed_cb where std::erase ran on the
vector being range-iterated; switch to std::erase_if and only reset
cur_fs if it actually pointed to the removed device.
* Defer UMS device-list updates to the main thread
libusbhsfs's populate callback runs on its USB-event thread. The
previous code mutated context.filesystems / cur_fs directly from
that thread, racing with main-thread reads from UI code.
Stash the new device list under a mutex; have the main loops
(menu_loop, video_loop) drain and apply pending changes once per
frame so context.filesystems stays single-threaded for readers.
Also handle USB unplug during playback: when the filesystem
hosting the currently-playing file disappears, async-stop mpv
and break out of video_loop with EIO so the user falls back to
the menu cleanly. (Note: this does not eliminate crashes when
mpv's demuxer thread is mid-read at the moment of physical
removal -- that race is inside libusbhsfs and would require
upstream changes to fix.)
* Fix indentation
---------
Co-authored-by: averne <averne381@gmail.com>1 parent cc4a21f commit 6300509
2 files changed
Lines changed: 48 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
165 | 165 | | |
166 | 166 | | |
167 | 167 | | |
168 | | - | |
| 168 | + | |
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| 22 | + | |
| 23 | + | |
21 | 24 | | |
22 | 25 | | |
23 | 26 | | |
| |||
137 | 140 | | |
138 | 141 | | |
139 | 142 | | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
140 | 149 | | |
141 | | - | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
142 | 163 | | |
143 | 164 | | |
144 | | - | |
| 165 | + | |
| 166 | + | |
145 | 167 | | |
146 | | - | |
| 168 | + | |
147 | 169 | | |
148 | 170 | | |
149 | 171 | | |
150 | 172 | | |
151 | 173 | | |
152 | 174 | | |
153 | | - | |
154 | | - | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
155 | 178 | | |
156 | | - | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
157 | 184 | | |
158 | 185 | | |
159 | 186 | | |
| |||
181 | 208 | | |
182 | 209 | | |
183 | 210 | | |
| 211 | + | |
| 212 | + | |
184 | 213 | | |
185 | 214 | | |
186 | 215 | | |
| |||
277 | 306 | | |
278 | 307 | | |
279 | 308 | | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
280 | 321 | | |
281 | 322 | | |
282 | 323 | | |
| |||
0 commit comments