about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2015-01-09 04:18:21 +0000
committerEric Wong <normalperson@yhbt.net>2015-01-09 07:24:09 +0000
commitf0f0cb23acc155e24a70a718801e807c80349e13 (patch)
treee990f82bf3ff989fb66d63f07faf58a304458c4b
parent6622d115c795d88b99d844ce8b74e979a2b55ae6 (diff)
downloadruby_posix_mq-f0f0cb23acc155e24a70a718801e807c80349e13.tar.gz
for_fd: delay assigning to mq->des until after mq_getattr
We do not want to assign to mq->des before verifying we
have a valid message queue, otherwise the GC may misclose
an invalid MQ descriptor (which may be a valid FD).
-rw-r--r--ext/posix_mq/posix_mq.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/ext/posix_mq/posix_mq.c b/ext/posix_mq/posix_mq.c
index 5e8122e..93e3913 100644
--- a/ext/posix_mq/posix_mq.c
+++ b/ext/posix_mq/posix_mq.c
@@ -384,13 +384,15 @@ static VALUE for_fd(VALUE klass, VALUE socket)
 {
         VALUE mqv = alloc(klass);
         struct posix_mq *mq = get(mqv, 0);
+        mqd_t mqd;
 
         mq->name = Qnil;
-        mq->des = FD_TO_MQD(NUM2INT(socket));
+        mqd = FD_TO_MQD(NUM2INT(socket));
 
-        if (mq_getattr(mq->des, &mq->attr) < 0)
+        if (mq_getattr(mqd, &mq->attr) < 0)
                 rb_sys_fail("provided file descriptor is not a POSIX MQ");
 
+        mq->des = mqd;
         return mqv;
 }
 #endif /* FD_TO_MQD */