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;
} }
@ -820,8 +824,13 @@ on_stream_remove_buffer (void *data,
} }
else if (spa_data[0].type == SPA_DATA_MemFd) else if (spa_data[0].type == SPA_DATA_MemFd)
{ {
munmap (spa_data[0].data, spa_data[0].maxsize); g_warn_if_fail (spa_data[0].fd > 0 || !spa_data[0].data);
close (spa_data[0].fd);
if (spa_data[0].fd > 0)
{
munmap (spa_data[0].data, spa_data[0].maxsize);
close (spa_data[0].fd);
}
} }
} }