screen-cast/src: Handle failing to allocate shm buffers

Don't leak the file descriptors, and don't fall over when trying to
clean up buffers that failed to allocate.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1697>
This commit is contained in:
Jonas Ådahl 2021-02-01 09:30:55 +01:00 committed by Marge Bot
parent 70d9a7b4bf
commit 08ad107df5

View File

@ -781,6 +781,8 @@ on_stream_add_buffer (void *data,
if (ftruncate (spa_data[0].fd, spa_data[0].maxsize) < 0) if (ftruncate (spa_data[0].fd, spa_data[0].maxsize) < 0)
{ {
close (spa_data[0].fd);
spa_data[0].fd = -1;
g_critical ("Can't truncate to %d: %m", spa_data[0].maxsize); g_critical ("Can't truncate to %d: %m", spa_data[0].maxsize);
return; return;
} }
@ -797,6 +799,8 @@ on_stream_add_buffer (void *data,
spa_data[0].mapoffset); spa_data[0].mapoffset);
if (spa_data[0].data == MAP_FAILED) if (spa_data[0].data == MAP_FAILED)
{ {
close (spa_data[0].fd);
spa_data[0].fd = -1;
g_critical ("Failed to mmap memory: %m"); g_critical ("Failed to mmap memory: %m");
return; return;
} }
@ -819,11 +823,16 @@ on_stream_remove_buffer (void *data,
g_critical ("Failed to remove non-exported DMA buffer"); g_critical ("Failed to remove non-exported DMA buffer");
} }
else if (spa_data[0].type == SPA_DATA_MemFd) else if (spa_data[0].type == SPA_DATA_MemFd)
{
g_warn_if_fail (spa_data[0].fd > 0 || !spa_data[0].data);
if (spa_data[0].fd > 0)
{ {
munmap (spa_data[0].data, spa_data[0].maxsize); munmap (spa_data[0].data, spa_data[0].maxsize);
close (spa_data[0].fd); close (spa_data[0].fd);
} }
} }
}
static const struct pw_stream_events stream_events = { static const struct pw_stream_events stream_events = {
PW_VERSION_STREAM_EVENTS, PW_VERSION_STREAM_EVENTS,