From 9c14d0c61ae5c9ed16604d03d70dc3c4765563d0 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 27 Feb 2011 01:10:33 +0000 Subject: disallow '\0' in C strings It's a bug in user code if we try to open files with '\0' in it. --- mahoro.c | 4 ++-- test.rb | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/mahoro.c b/mahoro.c index 49e3e45..3fd1e61 100644 --- a/mahoro.c +++ b/mahoro.c @@ -46,7 +46,7 @@ mahoro_initialize(argc, argv, self) switch(rb_scan_args(argc, argv, "02", &vflags, &vpath)) { case 2: if(!NIL_P(vpath)) { - path = StringValuePtr(vpath); + path = StringValueCStr(vpath); } /* fallthrough */ case 1: @@ -77,7 +77,7 @@ mahoro_file(self, path) const char *msg; magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie; - if(!(msg = magic_file(cookie, StringValuePtr(path)))) { + if(!(msg = magic_file(cookie, StringValueCStr(path)))) { rb_raise(eMahoroError, "failed lookup: %s", magic_error(cookie)); } diff --git a/test.rb b/test.rb index b7bcb69..1ffb570 100755 --- a/test.rb +++ b/test.rb @@ -22,6 +22,10 @@ class MahoroTestCase < Test::Unit::TestCase }.include?(@m.file('mahoro.c'))) end + def test_null_byte_in_path + assert_raises(ArgumentError) { @m.file("mahoro.c\0foo") } + end + def test_buffer @m.flags = Mahoro::NONE assert_equal('ASCII C program text', -- cgit v1.2.3-24-ge0c7