Skip to content

Commit

Permalink
tests/unit/socket-helpers: Don't close(-1)
Browse files Browse the repository at this point in the history
In socket_check_afunix_support() we call socket(PF_UNIX, SOCK_STREAM, 0)
to see if it works, but we call close() on the result whether it
worked or not. Only close the fd if the socket() call succeeded.
Spotted by Coverity.

Resolves: Coverity CID 1497481

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Message-id: 20240312183810.557768-3-peter.maydell@linaro.org
  • Loading branch information
pm215 committed Mar 25, 2024
1 parent 80e9791 commit e921e00
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion tests/unit/socket-helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,15 @@ void socket_check_afunix_support(bool *has_afunix)
int fd;

fd = socket(PF_UNIX, SOCK_STREAM, 0);
close(fd);

#ifdef _WIN32
*has_afunix = (fd != (int)INVALID_SOCKET);
#else
*has_afunix = (fd >= 0);
#endif

if (*has_afunix) {
close(fd);
}
return;
}

0 comments on commit e921e00

Please sign in to comment.