From 56066f332fe60c4b83a0cbb3430a723cbf5dd441 Mon Sep 17 00:00:00 2001 From: apio Date: Thu, 17 Aug 2023 11:20:32 +0200 Subject: [PATCH] wind: Handle ftruncate() and mmap() errors properly --- wind/IPC.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/wind/IPC.cpp b/wind/IPC.cpp index 3a4e5e59..d3ba6475 100644 --- a/wind/IPC.cpp +++ b/wind/IPC.cpp @@ -26,29 +26,32 @@ static Result create_shm_region(const char* path, int* outfd, ui::Rect rec int fd = shm_open(path, O_RDWR | O_CREAT | O_EXCL, 0600); if (fd < 0) { - os::eprintln("wind: could not create shared memory region: shm_open failed (%s) - %s", path, strerror(errno)); - return err(errno); + int olderr = errno; + os::eprintln("wind: could not create shared memory region: shm_open failed (%s) - %s", path, strerror(olderr)); + return err(olderr); } usize size = align_up(rect.width * rect.height * 4); // 4 bytes per pixel if (ftruncate(fd, size) < 0) { + int olderr = errno; os::eprintln("wind: could not create shared memory region: ftruncate failed (%d, %zu) - %s", fd, size, - strerror(errno)); + strerror(olderr)); shm_unlink(path); close(fd); - return 0; + return err(olderr); } void* p = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (p == MAP_FAILED) { + int olderr = errno; os::eprintln("wind: could not create shared memory region: mmap failed (%zu, %d) - %s", size, fd, - strerror(errno)); + strerror(olderr)); shm_unlink(path); close(fd); - return 0; + return err(olderr); } if (outfd) *outfd = fd;