From 5f1578c0d17b05f5158e467bd3abf18565fe1b4b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 22 Feb 2011 15:32:40 -0800 Subject: use StringValueCStr for paths RSTRING_PTR may contain '\0' bytes which makes it unsuitable for mq_unlink() and mq_open() --- ext/posix_mq/posix_mq.c | 11 ++--------- 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) } -- cgit v1.2.3-24-ge0c7