A weird little test which can expose buggy return value conditions in poll()
authorderaadt <deraadt@openbsd.org>
Tue, 23 Nov 2021 01:14:26 +0000 (01:14 +0000)
committerderaadt <deraadt@openbsd.org>
Tue, 23 Nov 2021 01:14:26 +0000 (01:14 +0000)
regress/sys/kern/poll/Makefile
regress/sys/kern/poll/pollretval.c [new file with mode: 0644]

index 3b87d79..e231059 100644 (file)
@@ -1,6 +1,6 @@
-#      $OpenBSD: Makefile,v 1.2 2021/11/21 06:21:01 visa Exp $
+#      $OpenBSD: Makefile,v 1.3 2021/11/23 01:14:26 deraadt Exp $
 
-PROGS=         poll_close pollnval
+PROGS=         poll_close pollnval pollretval
 LDADD=         -lpthread
 WARNINGS=      yes
 
diff --git a/regress/sys/kern/poll/pollretval.c b/regress/sys/kern/poll/pollretval.c
new file mode 100644 (file)
index 0000000..9d2d4ca
--- /dev/null
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <poll.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <err.h>
+
+int
+main()
+{
+        struct pollfd pfd[100];
+        int i, r, r2 = 0;
+
+        for (i = 0; i < 100; i++) {
+            pfd[i].fd = 0;
+            pfd[i].events = arc4random() % 0x177;
+        }
+
+       r = poll(pfd, 100, INFTIM);
+
+       if (r == -1)
+               errx(1, "poll failed unexpectedly");
+
+        for (i = 0; i < 100; i++)
+               if (pfd[i].revents)
+                       r2++;
+       if (r != r2)
+               errx(1, "poll return value %d miscounts .revents %d", r, r2);
+}