[前][次][番号順一覧][スレッド一覧]

ruby-changes:45264

From: nobu <ko1@a...>
Date: Mon, 16 Jan 2017 11:44:04 +0900 (JST)
Subject: [ruby-changes:45264] nobu:r57336 (trunk): file.c: refine message

nobu	2017-01-16 11:43:55 +0900 (Mon, 16 Jan 2017)

  New Revision: 57336

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57336

  Log:
    file.c: refine message
    
    * file.c (rb_get_path_check_convert): refine the error message
      when the path name contains null byte.

  Modified files:
    trunk/file.c
    trunk/internal.h
    trunk/string.c
Index: internal.h
===================================================================
--- internal.h	(revision 57335)
+++ internal.h	(revision 57336)
@@ -1475,6 +1475,7 @@ VALUE rb_enc_str_scrub(rb_encoding *enc, https://github.com/ruby/ruby/blob/trunk/internal.h#L1475
 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
Index: string.c
===================================================================
--- string.c	(revision 57335)
+++ string.c	(revision 57336)
@@ -2081,29 +2081,52 @@ rb_str_change_terminator_length(VALUE st https://github.com/ruby/ruby/blob/trunk/string.c#L2081
     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);
     }
     return s;
 }
+
+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)
Index: file.c
===================================================================
--- file.c	(revision 57335)
+++ file.c	(revision 57336)
@@ -195,7 +195,9 @@ rb_get_path_check_convert(VALUE obj, VAL https://github.com/ruby/ruby/blob/trunk/file.c#L195
     }
 
     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);
 }

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]