librelist archives

« back to archive

[PATCH] epoll: enforce maxevents > 0 before memory allocation

[PATCH] epoll: enforce maxevents > 0 before memory allocation

From:
Eric Wong
Date:
2013-04-21 @ 03:51
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.
---
 ext/sleepy_penguin/epoll.c | 6 ++++++
 1 file changed, 6 insertions(+)

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;
 
-- 
Eric Wong