[ruby-dev:24379] Re: File.basename dumps core

From: nobu@...
Date: 2004-09-30 15:15:06 UTC
List: ruby-dev #24379
なかだです。

At Thu, 30 Sep 2004 19:00:30 +0900,
Tanaka Akira wrote in [ruby-dev:24377]:
> 
> 次のようにすると core を吐きます。
> 
> % ./ruby -e '
> fname = Object.new
> ext = "x" * 0x1fff4
> class << fname; self end.send(:define_method, :to_str) {
>   ext.replace ""
>   "a.txt"
> }
> File.basename(fname, ext)'

fname.to_strでextに\0を追加したりしてもエラーにはなりませんが、
そこはまぁ愛敬ってことで。


Index: file.c
===================================================================
RCS file: /cvs/ruby/src/ruby/file.c,v
retrieving revision 1.186
diff -U2 -p -d -r1.186 file.c
--- file.c	23 Sep 2004 00:51:31 -0000	1.186
+++ file.c	30 Sep 2004 15:10:41 -0000
@@ -2655,9 +2655,9 @@ rb_file_s_basename(argc, argv)
 {
     VALUE fname, fext, basename;
-    char *name, *p, *ext = NULL;
+    char *name, *p;
     int f;
 
     if (rb_scan_args(argc, argv, "11", &fname, &fext) == 2) {
-	ext = StringValueCStr(fext);
+	StringValueCStr(fext);
     }
     StringValue(fname);
@@ -2675,5 +2675,5 @@ rb_file_s_basename(argc, argv)
     }
     else if (!(p = strrdirsep(name))) {
-	if (NIL_P(fext) || !(f = rmext(name, ext))) {
+	if (NIL_P(fext) || !(f = rmext(name, RSTRING(fext)->ptr))) {
 	    f = chompdirsep(name) - name;
 	    if (f == RSTRING(fname)->len) return fname;
@@ -2683,5 +2683,5 @@ rb_file_s_basename(argc, argv)
     else {
 	while (isdirsep(*p)) p++; /* skip last / */
-	if (NIL_P(fext) || !(f = rmext(p, ext))) {
+	if (NIL_P(fext) || !(f = rmext(p, RSTRING(fext)->ptr))) {
 	    f = chompdirsep(p) - p;
 	}


-- 
--- 僕の前にBugはない。
--- 僕の後ろにBugはできる。
    中田 伸悦

In This Thread

Prev Next