diff options
author | Davide Grohmann <davide.grohmann@arm.com> | 2021-11-08 15:08:48 +0100 |
---|---|---|
committer | Davide Grohmann <davide.grohmann@arm.com> | 2021-11-15 09:49:52 +0100 |
commit | 2ba3c1d55f638d1a2ecc5abb37f45504489bc8f0 (patch) | |
tree | e208f47cee4cd6a03071cc0f445538ccbe4595e5 | |
parent | b2252bb68a8664db09b7f50d77f89e48d602dba2 (diff) | |
download | ethos-u-linux-driver-stack-2ba3c1d55f638d1a2ecc5abb37f45504489bc8f0.tar.gz |
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
-rw-r--r-- | driver_library/src/ethosu.cpp | 13 |
1 files 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<uint32_t>(dataCapacity)}; fd = device.ioctl(ETHOSU_IOCTL_BUFFER_CREATE, static_cast<void *>(&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<char *>(d); } @@ -291,7 +298,9 @@ Network::Network(Device &device, shared_ptr<Buffer> &buffer) : fd(-1), buffer(bu const tflite::Model *model = tflite::GetModel(reinterpret_cast<void *>(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 |