summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorjhb <jhb@FreeBSD.org>2007-04-02 19:15:47 +0000
committerjhb <jhb@FreeBSD.org>2007-04-02 19:15:47 +0000
commitb9a3a5afc72f64c5b84111d7076a3e50d67b2529 (patch)
treeb4ed199cdf22428e589ba76f6d3c83161e6890e7 /tools
parent67ec796ffc0def827d19857a83e77651a34b9f96 (diff)
downloadFreeBSD-src-b9a3a5afc72f64c5b84111d7076a3e50d67b2529.zip
FreeBSD-src-b9a3a5afc72f64c5b84111d7076a3e50d67b2529.tar.gz
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.
Diffstat (limited to 'tools')
-rw-r--r--tools/regression/sockets/socketpair/socketpair.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/tools/regression/sockets/socketpair/socketpair.c b/tools/regression/sockets/socketpair/socketpair.c
index a1d0946..5f46476 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);
}
OpenPOWER on IntegriCloud