From 2ba3c1d55f638d1a2ecc5abb37f45504489bc8f0 Mon Sep 17 00:00:00 2001 From: Davide Grohmann Date: Mon, 8 Nov 2021 15:08:48 +0100 Subject: Fix: do not leak file descriptors on errors When bailing out on errors in the constructors of Buffers or Networks, the open file descriptor should be closed since the descructor will never be called. Change-Id: I8e1954e9efd65b594cc9544e18d0bfbe0730f156 --- driver_library/src/ethosu.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/driver_library/src/ethosu.cpp b/driver_library/src/ethosu.cpp index 45e8525..4fc431b 100644 --- a/driver_library/src/ethosu.cpp +++ b/driver_library/src/ethosu.cpp @@ -233,7 +233,14 @@ Buffer::Buffer(Device &device, const size_t capacity) : fd(-1), dataPtr(nullptr) ethosu_uapi_buffer_create uapi = {static_cast(dataCapacity)}; fd = device.ioctl(ETHOSU_IOCTL_BUFFER_CREATE, static_cast(&uapi)); - void *d = emmap(nullptr, dataCapacity, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + void *d; + try { + d = emmap(nullptr, dataCapacity, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + } catch (std::exception &e) { + try { + eclose(fd); + } catch (...) { std::throw_with_nested(e); } + } dataPtr = reinterpret_cast(d); } @@ -291,7 +298,9 @@ Network::Network(Device &device, shared_ptr &buffer) : fd(-1), buffer(bu const tflite::Model *model = tflite::GetModel(reinterpret_cast(buffer->data())); if (model->subgraphs() == nullptr) { - throw EthosU::Exception("Failed to get subgraphs: nullptr"); + try { + eclose(fd); + } catch (...) { std::throw_with_nested(EthosU::Exception("Failed to get subgraphs: nullptr")); } } // Get input dimensions for first subgraph -- cgit v1.2.1