From 71bcc1cac1a687d8e7222e7cca52b5628ea896ff Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 27 Feb 2011 07:05:54 +0000 Subject: more '\0' byte in C string avoidance Just covering all of our bases. --- magic.sample | 5 +++++ mahoro.c | 6 +++--- test.rb | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 magic.sample diff --git a/magic.sample b/magic.sample new file mode 100644 index 0000000..bd008c7 --- /dev/null +++ b/magic.sample @@ -0,0 +1,5 @@ +# Ruby scripts +0 search/1/b #!\ /usr/bin/ruby Ruby script text executable +0 search/1/b #!\ /usr/local/bin/ruby Ruby script text executable +0 search/1 #!/usr/bin/env\ ruby Ruby script text executable +0 search/1 #!\ /usr/bin/env\ ruby Ruby script text executable diff --git a/mahoro.c b/mahoro.c index 385234e..2a597ee 100644 --- a/mahoro.c +++ b/mahoro.c @@ -117,7 +117,7 @@ mahoro_check(argc, argv, self) switch(rb_scan_args(argc, argv, "01", &vpath)) { case 1: if(!NIL_P(vpath)) { - path = StringValuePtr(vpath); + path = StringValueCStr(vpath); } break; } @@ -135,7 +135,7 @@ mahoro_compile(klass, path) { magic_t cookie = magic_open(MAGIC_NONE); - if(magic_compile(cookie, StringValuePtr(path))) { + if(magic_compile(cookie, StringValueCStr(path))) { rb_raise(eMahoroError, "failed compile: %s", magic_error(cookie)); } @@ -150,7 +150,7 @@ mahoro_load(self, path) { magic_t cookie = (magic_t)DATA_PTR(self); - if(magic_load(cookie, StringValuePtr(path))) { + if(magic_load(cookie, StringValueCStr(path))) { rb_raise(eMahoroError, "failed load: %s", magic_error(cookie)); } diff --git a/test.rb b/test.rb index 52b3b80..574979d 100755 --- a/test.rb +++ b/test.rb @@ -55,6 +55,25 @@ class MahoroTestCase < Test::Unit::TestCase assert(@m.valid?, 'Default database was not valid.') end + def test_valid_with_null + assert_raises(ArgumentError) { @m.valid? "mahoro.c\0" } + end + + def test_compile + File.open(__FILE__) do |fp| + fp.flock File::LOCK_EX + assert Mahoro.compile("magic.sample") + assert_nothing_raised do + File.unlink("magic.sample.mgc") + end + end + end + + def test_compile_bad + assert_raises(ArgumentError) do + Mahoro.compile "magic.sample\0" + end + end end # arch-tag: test -- cgit v1.2.3-24-ge0c7