Re: [Safer] rb_class_path

From: Michal Rokos <m.rokos@...>
Date: 2002-08-28 14:26:16 UTC
List: ruby-core #381
Hello,

On Wed, Aug 28, 2002 at 06:29:41PM +0900, nobu.nokada@softhome.net wrote:
> > >   +	RSTRING(str)->len = strlen(RSTRING(str)->ptr);

	If you agree, I will commit this version. (It's based on my 1st
	version of this patch and your fix with 2 * SIZEOF_LONG. I think
	that it is also to have +1 in the end of rb_str_new (not +1+1))

		Michal

Index: signal.c
===================================================================
RCS file: /src/ruby/signal.c,v
retrieving revision 1.36
diff -u -p -r1.36 signal.c
--- signal.c	2002/08/16 07:23:04	1.36
+++ signal.c	2002/08/28 08:22:05
@@ -36,9 +36,7 @@ static struct signals {
 #ifdef SIGHUP
     {"HUP", SIGHUP},
 #endif
-#ifdef SIGINT
     {"INT", SIGINT},
-#endif
 #ifdef SIGQUIT
     {"QUIT", SIGQUIT},
 #endif
Index: variable.c
===================================================================
RCS file: /src/ruby/variable.c,v
retrieving revision 1.65
diff -u -p -r1.65 variable.c
--- variable.c	2002/05/14 06:22:26	1.65
+++ variable.c	2002/08/28 08:22:07
@@ -188,17 +188,22 @@ rb_class_path(klass)
 
     if (path) return path;
     else {
-	char buf[256];
+	VALUE str;
 	char *s = "Class";
 
 	if (TYPE(klass) == T_MODULE) {
-	    if (rb_obj_class(klass) == rb_cModule)
+	    if (rb_obj_class(klass) == rb_cModule) {
 		s = "Module";
-	    else
+	    }
+	    else {
 		s = rb_class2name(RBASIC(klass)->klass);
+	    }
 	}
-	sprintf(buf, "#<%s:0x%lx>", s, klass);
-	return rb_str_new2(buf);
+	str = rb_str_new(0, 2 + strlen(s) + 3 + 2 * SIZEOF_LONG + 1);
+	sprintf(RSTRING(str)->ptr, "#<%s:0x%lx>", s, klass);
+	RSTRING(str)->len = strlen(RSTRING(str)->ptr);
+
+	return str;
     }
 }
 
-- 
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Michal Rokos                         Czech Technical University, Prague
E-mail:m.rokos@sh.cvut.cz      ICQ:36118339      Jabber:majkl@jabber.cz
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

In This Thread