about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2013-04-21 03:48:27 +0000
committerEric Wong <normalperson@yhbt.net>2013-04-21 03:50:28 +0000
commit7181b7e4da11894b59caebc1c864deca06a780b2 (patch)
treefc2ff2e3a973eee33fefcb20a444edfa88f33a8a
parent623c6797bcf1f8077c39cdcffb83b9aa25793932 (diff)
downloadsleepy_penguin-7181b7e4da11894b59caebc1c864deca06a780b2.tar.gz
epoll: enforce maxevents > 0 before memory allocation
This prevents overflow and excessive memory usage/OOM error.
Note: the kernel enforces this and returns EINVAL anyways,
we just do it to prevent OOM here.
-rw-r--r--ext/sleepy_penguin/epoll.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/ext/sleepy_penguin/epoll.c b/ext/sleepy_penguin/epoll.c
index 8e49171..a6f86f8 100644
--- a/ext/sleepy_penguin/epoll.c
+++ b/ext/sleepy_penguin/epoll.c
@@ -65,6 +65,12 @@ static struct ep_per_thread *ept_get(VALUE self, int maxevents)
         int err;
         void *ptr;
 
+        /* error check here to prevent OOM from posix_memalign */
+        if (maxevents <= 0) {
+                errno = EINVAL;
+                rb_sys_fail("epoll_wait maxevents <= 0");
+        }
+
         if (ept && ept->capa >= maxevents)
                 goto out;