From 2dc1e2a5d346c550468d91414550d8c5258258fc 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. (cherry picked from commit 29b0c87d717266bf1dd4278b56f152e67ea22ab3) --- 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 df4c8f181..e8357b00e 100644 --- a/spa/plugins/libcamera/libcamera-source.cpp +++ b/spa/plugins/libcamera/libcamera-source.cpp @@ -306,10 +306,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; @@ -323,7 +332,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;