[libcamera-devel,2/4] test: ipc: unixsocket: Close open fds on error paths

Message ID 20200413104631.12276-3-email@uajain.com
State Accepted
Headers show
Series
  • Fix resource leaks pointed out by coverity scans
Related show

Commit Message

Umang Jain April 13, 2020, 10:46 a.m. UTC
Pointed out by Coverity DefectId=279052

Signed-off-by: Umang Jain <email@uajain.com>
---
 test/ipc/unixsocket.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Laurent Pinchart April 13, 2020, 12:31 p.m. UTC | #1
Hi Umang,

Thank you for the patch.

On Mon, Apr 13, 2020 at 10:46:53AM +0000, Umang Jain wrote:
> Pointed out by Coverity DefectId=279052
> 
> Signed-off-by: Umang Jain <email@uajain.com>
> ---
>  test/ipc/unixsocket.cpp | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/test/ipc/unixsocket.cpp b/test/ipc/unixsocket.cpp
> index 5348f35..4fc6606 100644
> --- a/test/ipc/unixsocket.cpp
> +++ b/test/ipc/unixsocket.cpp
> @@ -470,8 +470,10 @@ private:
>  		int size = 0;
>  		for (unsigned int i = 0; i < num; i++) {
>  			int clone = dup(fd);
> -			if (clone < 0)
> +			if (clone < 0) {
> +				close(fd);
>  				return clone;
> +			}

I think we should simplify all this by using the FileDescriptor class.
Looking at IPCUnixSocket::Payload, it seems that fds could be turned
into a vector of FileDescriptor. I haven't however investigated if the
API of the FileDescriptor class would need extensions, or if it's
suitable as-is, but at least here the manual dup() wouldn't be needed,
which already simplifies the code.

Usage of FileDescriptor would also fix a leak of fds in readyRead().

>  
>  			size += calculateLength(clone);
>  			message->fds.push_back(clone);

Patch

diff --git a/test/ipc/unixsocket.cpp b/test/ipc/unixsocket.cpp
index 5348f35..4fc6606 100644
--- a/test/ipc/unixsocket.cpp
+++ b/test/ipc/unixsocket.cpp
@@ -470,8 +470,10 @@  private:
 		int size = 0;
 		for (unsigned int i = 0; i < num; i++) {
 			int clone = dup(fd);
-			if (clone < 0)
+			if (clone < 0) {
+				close(fd);
 				return clone;
+			}
 
 			size += calculateLength(clone);
 			message->fds.push_back(clone);