diff options
author | Eric Wong <normalperson@yhbt.net> | 2011-02-22 15:32:40 -0800 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2011-02-22 15:39:42 -0800 |
commit | 5f1578c0d17b05f5158e467bd3abf18565fe1b4b (patch) | |
tree | b210653369612e3e5360a31eaa42501a8181de2c | |
parent | a5491670078b045ced2641649a6cfceee2cade10 (diff) | |
download | ruby_posix_mq-5f1578c0d17b05f5158e467bd3abf18565fe1b4b.tar.gz |
RSTRING_PTR may contain '\0' bytes which makes it unsuitable for mq_unlink() and mq_open()
-rw-r--r-- | ext/posix_mq/posix_mq.c | 11 | ||||
-rw-r--r-- | test/test_posix_mq.rb | 8 |
2 files changed, 10 insertions, 9 deletions
diff --git a/ext/posix_mq/posix_mq.c b/ext/posix_mq/posix_mq.c index c02d697..01b5dbd 100644 --- a/ext/posix_mq/posix_mq.c +++ b/ext/posix_mq/posix_mq.c @@ -346,9 +346,6 @@ static VALUE init(int argc, VALUE *argv, VALUE self) rb_scan_args(argc, argv, "13", &name, &oflags, &mode, &attr); - if (TYPE(name) != T_STRING) - rb_raise(rb_eArgError, "name must be a string"); - switch (TYPE(oflags)) { case T_NIL: x.oflags = O_RDONLY; @@ -375,7 +372,7 @@ static VALUE init(int argc, VALUE *argv, VALUE self) rb_raise(rb_eArgError, "flags must be an int, :r, :w, or :wr"); } - x.name = RSTRING_PTR(name); + x.name = StringValueCStr(name); x.argc = 2; switch (TYPE(mode)) { @@ -434,12 +431,8 @@ static VALUE init(int argc, VALUE *argv, VALUE self) */ static VALUE s_unlink(VALUE self, VALUE name) { - mqd_t rv; - - if (TYPE(name) != T_STRING) - rb_raise(rb_eArgError, "argument must be a string"); + mqd_t rv = mq_unlink(StringValueCStr(name)); - rv = mq_unlink(RSTRING_PTR(name)); if (rv == MQD_INVALID) rb_sys_fail("mq_unlink"); diff --git a/test/test_posix_mq.rb b/test/test_posix_mq.rb index 9587c3a..f7dec3c 100644 --- a/test/test_posix_mq.rb +++ b/test/test_posix_mq.rb @@ -25,6 +25,14 @@ class Test_POSIX_MQ < Test::Unit::TestCase assert @mq.closed? end + def test_open_with_null_byte + assert_raises(ArgumentError) { POSIX_MQ.open("/hello\0world", :rw) } + end + + def test_unlink_with_null_byte + assert_raises(ArgumentError) { POSIX_MQ.open("/hello\0world", :rw) } + end + def test_gc assert_nothing_raised do 2025.times { POSIX_MQ.new(@path, :rw) } |