From 29b0c87d717266bf1dd4278b56f152e67ea22ab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Fri, 8 Aug 2025 09:53:13 +0200 Subject: [PATCH] spa: libcamera: source: allocBuffers(): more error checking First, check if the request pool is empty, and signal an error if it is not. Second, ensure that the number of allocated buffers matches. --- spa/plugins/libcamera/libcamera-source.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/spa/plugins/libcamera/libcamera-source.cpp b/spa/plugins/libcamera/libcamera-source.cpp index 5588f9828..ae3e9a9b3 100644 --- a/spa/plugins/libcamera/libcamera-source.cpp +++ b/spa/plugins/libcamera/libcamera-source.cpp @@ -307,10 +307,19 @@ int allocBuffers(struct impl *impl, struct port *port, unsigned int count) libcamera::Stream *stream = port->streamConfig.stream(); int res; + if (!impl->requestPool.empty()) + return -EBUSY; + if ((res = impl->allocator->allocate(stream)) < 0) return res; - for (unsigned int i = 0; i < count; i++) { + const auto& bufs = impl->allocator->buffers(stream); + if (bufs.empty() || bufs.size() != count) { + res = -ENOBUFS; + goto err; + } + + for (std::size_t i = 0; i < bufs.size(); i++) { std::unique_ptr request = impl->camera->createRequest(i); if (!request) { res = -ENOMEM; @@ -324,7 +333,7 @@ int allocBuffers(struct impl *impl, struct port *port, unsigned int count) * video frame has to be addressed using more than one memory. * address. Therefore, need calculate the number of discontiguous * memory and allocate the specified amount of memory */ - port->buffers_blocks = count_unique_fds(impl->allocator->buffers(stream).front()->planes()); + port->buffers_blocks = count_unique_fds(bufs.front()->planes()); if (port->buffers_blocks <= 0) { res = -ENOBUFS; goto err;