diff options
author | Eric Wong <normalperson@yhbt.net> | 2011-02-27 07:05:54 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2011-02-27 07:05:54 +0000 |
commit | 71bcc1cac1a687d8e7222e7cca52b5628ea896ff (patch) | |
tree | 514e591ed0e68cecbde76e0836c22394c63fd872 | |
parent | 11846a17eed6350063c753a0d4b684d9c3c695d0 (diff) | |
download | mahoro-71bcc1cac1a687d8e7222e7cca52b5628ea896ff.tar.gz |
more '\0' byte in C string avoidance
Just covering all of our bases.
-rw-r--r-- | magic.sample | 5 | ||||
-rw-r--r-- | mahoro.c | 6 | ||||
-rwxr-xr-x | test.rb | 19 |
3 files changed, 27 insertions, 3 deletions
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 @@ -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)); } @@ -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 |