diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-01-16 02:43:55 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-01-16 02:43:55 +0000 |
commit | 803621f6d7e8415452fec49292b2b045dc04fa37 (patch) | |
tree | c6079e9786e253faedf45839d3f2072c62b2c731 | |
parent | 992b0148cacc7abbd439691460b5d937930e53ca (diff) | |
download | ruby-803621f6d7e8415452fec49292b2b045dc04fa37.tar.gz |
file.c: refine message
* file.c (rb_get_path_check_convert): refine the error message
when the path name contains null byte.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57336 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | file.c | 4 | ||||
-rw-r--r-- | internal.h | 1 | ||||
-rw-r--r-- | string.c | 33 |
3 files changed, 32 insertions, 6 deletions
@@ -195,7 +195,9 @@ rb_get_path_check_convert(VALUE obj, VALUE tmp, int level) } check_path_encoding(tmp); - StringValueCStr(tmp); + if (!rb_str_to_cstr(tmp)) { + rb_raise(rb_eArgError, "path name contains null byte"); + } return rb_str_new4(tmp); } diff --git a/internal.h b/internal.h index c44d7bd99c..897c72b307 100644 --- a/internal.h +++ b/internal.h @@ -1475,6 +1475,7 @@ VALUE rb_enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl); size_t rb_str_memsize(VALUE); VALUE rb_sym_proc_call(ID mid, int argc, const VALUE *argv, VALUE passed_proc); VALUE rb_sym_to_proc(VALUE sym); +char *rb_str_to_cstr(VALUE str); /* symbol.c */ #ifdef RUBY_ENCODING_H @@ -2081,23 +2081,24 @@ rb_str_change_terminator_length(VALUE str, const int oldtermlen, const int terml return; } -char * -rb_string_value_cstr(volatile VALUE *ptr) +static char * +str_null_check(VALUE str, int *w) { - VALUE str = rb_string_value(ptr); char *s = RSTRING_PTR(str); long len = RSTRING_LEN(str); rb_encoding *enc = rb_enc_get(str); const int minlen = rb_enc_mbminlen(enc); if (minlen > 1) { + *w = 1; if (str_null_char(s, len, minlen, enc)) { - rb_raise(rb_eArgError, "string contains null char"); + return NULL; } return str_fill_term(str, s, len, minlen); } + *w = 0; if (!s || memchr(s, 0, len)) { - rb_raise(rb_eArgError, "string contains null byte"); + return NULL; } if (s[len]) { s = str_fill_term(str, s, len, minlen); @@ -2106,6 +2107,28 @@ rb_string_value_cstr(volatile VALUE *ptr) } char * +rb_str_to_cstr(VALUE str) +{ + int w; + return str_null_check(str, &w); +} + +char * +rb_string_value_cstr(volatile VALUE *ptr) +{ + VALUE str = rb_string_value(ptr); + int w; + char *s = str_null_check(str, &w); + if (!s) { + if (w) { + rb_raise(rb_eArgError, "string contains null char"); + } + rb_raise(rb_eArgError, "string contains null byte"); + } + return s; +} + +char * rb_str_fill_terminator(VALUE str, const int newminlen) { char *s = RSTRING_PTR(str); |