diff options
author | John Baldwin <jhb@FreeBSD.org> | 2007-04-02 19:15:47 +0000 |
---|---|---|
committer | John Baldwin <jhb@FreeBSD.org> | 2007-04-02 19:15:47 +0000 |
commit | 1ce2bc91878f97dd46980d21a2b9c9d283e05e13 (patch) | |
tree | b4ed199cdf22428e589ba76f6d3c83161e6890e7 /tools | |
parent | 0a55a034ba33bfba0de2444005a7f24b4bdb205f (diff) | |
download | src-1ce2bc91878f97dd46980d21a2b9c9d283e05e13.tar.gz src-1ce2bc91878f97dd46980d21a2b9c9d283e05e13.zip |
Fix a fd leak in socketpair():
- Close the new file objects created during socketpair() if the copyout of
the new file descriptors fails.
- Add a test to the socketpair regression test for this edge case.
Notes
Notes:
svn path=/head/; revision=168278
Diffstat (limited to 'tools')
-rw-r--r-- | tools/regression/sockets/socketpair/socketpair.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/tools/regression/sockets/socketpair/socketpair.c b/tools/regression/sockets/socketpair/socketpair.c index a1d0946c7b58..5f4647614f99 100644 --- a/tools/regression/sockets/socketpair/socketpair.c +++ b/tools/regression/sockets/socketpair/socketpair.c @@ -49,6 +49,7 @@ int main(int argc, char *argv[]) { + int fd1, fd2, fd3; int sv[2]; /* @@ -123,6 +124,38 @@ main(int argc, char *argv[]) fprintf(stderr, "FAIL\n"); } + /* + * Check for sequential fd allocation, and give up early if not. + */ + fd1 = dup(STDIN_FILENO); + fd2 = dup(STDIN_FILENO); + if (fd2 != fd1 + 1) { + fprintf(stderr, "Non-sequential fd allocation\n"); + fprintf(stderr, "FAIL\n"); + exit(-1); + } + + /* Allocate a socketpair using a bad destination address. */ + if (socketpair(PF_UNIX, SOCK_DGRAM, 0, NULL) == 0) { + fprintf(stderr, "socketpair(PF_UNIX, SOCK_DGRAM, NULL): opened\n"); + fprintf(stderr, "FAIL\n"); + exit(-1); + } + if (errno != EFAULT) { + fprintf(stderr, "socketpair(PF_UNIX, SOCK_DGRAM, NULL): %s\n", + strerror(errno)); + fprintf(stderr, "FAIL\n"); + exit(-1); + } + + /* Allocate a file descriptor and make sure it's fd2+1. */ + fd3 = dup(STDIN_FILENO); + if (fd3 != fd2 + 1) { + fprintf(stderr, "socketpair(..., NULL) allocated descriptors\n"); + fprintf(stderr, "FAIL\n"); + exit(-1); + } + fprintf(stderr, "PASS\n"); exit(0); } |