From ad54de2acac70ba2f889892df950508edbc972b7 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 16 Oct 2012 01:53:32 +0000 Subject: file.c: check_path_encoding * file.c (check_path_encoding): new function to ensure path name encoding to be ASCII-compatible. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37206 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ file.c | 25 +++++++++++++------------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index af89d2e169..40f8006c3c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Tue Oct 16 10:53:29 2012 Nobuyoshi Nakada + + * file.c (check_path_encoding): new function to ensure path name + encoding to be ASCII-compatible. + Tue Oct 16 09:40:04 2012 NAKAMURA Usaku * test/ruby/test_regexp.rb diff --git a/file.c b/file.c index 38cc83b124..22df4420be 100644 --- a/file.c +++ b/file.c @@ -156,12 +156,22 @@ file_path_convert(VALUE name) return name; } +static rb_encoding * +check_path_encoding(VALUE str) +{ + rb_encoding *enc = rb_enc_get(str); + if (!rb_enc_asciicompat(enc)) { + rb_raise(rb_eEncCompatError, "path name must be ASCII-compatible (%s): %"PRIsVALUE, + rb_enc_name(enc), rb_str_inspect(str)); + } + return enc; +} + static VALUE rb_get_path_check(VALUE obj, int level) { VALUE tmp; ID to_path; - rb_encoding *enc; if (insecure_obj_p(obj, level)) { rb_insecure_operation(); @@ -178,13 +188,8 @@ rb_get_path_check(VALUE obj, int level) if (obj != tmp && insecure_obj_p(tmp, level)) { rb_insecure_operation(); } - enc = rb_enc_get(tmp); - if (!rb_enc_asciicompat(enc)) { - tmp = rb_str_inspect(tmp); - rb_raise(rb_eEncCompatError, "path name must be ASCII-compatible (%s): %s", - rb_enc_name(enc), RSTRING_PTR(tmp)); - } + check_path_encoding(tmp); StringValueCStr(tmp); return rb_str_new4(tmp); @@ -3669,11 +3674,7 @@ rb_file_s_basename(int argc, VALUE *argv) if (rb_scan_args(argc, argv, "11", &fname, &fext) == 2) { StringValue(fext); - enc = rb_enc_get(fext); - if (!rb_enc_asciicompat(enc)) { - rb_raise(rb_eEncCompatError, "ascii incompatible character encodings: %s", - rb_enc_name(enc)); - } + enc = check_path_encoding(fext); } FilePathStringValue(fname); if (NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) { -- cgit v1.2.3