[#8815] Segfault in libc strlen, via rb_str_new2 — "Sean E. Russell" <ser@...>

Howdy,

12 messages 2006/09/09
[#8817] Re: Segfault in libc strlen, via rb_str_new2 — Eric Hodel <drbrain@...7.net> 2006/09/09

On Sep 8, 2006, at 10:10 PM, Sean E. Russell wrote:

constants (rcrs 178, 343,345)

From: Ondrej Bilka <neleai@...>
Date: 2006-09-12 10:55:54 UTC
List: ruby-core #8850
I looked at my rcr 343 and 345. Could you give me some feedback?

First question is what ways of constant resolution would you use at rcr.
Add to source get_const where is constant expected?
Change parser that it search constants from called method's class too?
Write AI that recognizes to what class constant belongs?
Have somebody other idea?

How abreviate constants(FIXNUM_PROCESS_RLIMIT_CPU -> CPU)?
I use conversion that you strip prefix and add as new constant while it makes sense. 
It is because you could specify what abbrevs do you want and what not. It producess enought spam warnings to encourage resolving abbrevation clashes. 
Resolution abrevs could be done when get_const is called but I don't see any advantage of this and don't know how convert ID to char *. 

This is my try. It shows way how modify other code(It will take long so
I would do it if it will me merged.)

Index: process.c
===================================================================
RCS file: /src/ruby/process.c,v
retrieving revision 1.156
diff -u -r1.156 process.c
--- process.c	4 Sep 2006 07:40:43 -0000	1.156
+++ process.c	12 Sep 2006 09:51:27 -0000
@@ -715,7 +715,7 @@
     else {
 	pid = NUM2INT(vpid);
 	if (argc == 2 && !NIL_P(vflags)) {
-	    flags = NUM2UINT(vflags);
+	    flags = NUM2UINT(rb_constantize(rb_mProcess,vflags));
 	}
     }
     if ((pid = rb_waitpid(pid, &status, flags)) < 0)
@@ -1852,7 +1852,7 @@
     int prio, iwhich, iwho;
 
     rb_secure(2);
-    iwhich = NUM2INT(which);
+    iwhich = NUM2INT(rb_get_const(rb_mProcess,which));
     iwho   = NUM2INT(who);
 
     errno = 0;
@@ -1884,7 +1884,7 @@
     int iwhich, iwho, iprio;
 
     rb_secure(2);
-    iwhich = NUM2INT(which);
+    iwhich = NUM2INT(rb_get_const(rb_mProcess,which));
     iwho   = NUM2INT(who);
     iprio  = NUM2INT(prio);
 
@@ -1934,7 +1934,7 @@
 
     rb_secure(2);
 
-    if (getrlimit(NUM2INT(resource), &rlim) < 0) {
+    if (getrlimit(NUM2INT(rb_get_const(rb_mProcess,resource)), &rlim) < 0) {
 	rb_sys_fail("getrlimit");
     }
     return rb_assoc_new(RLIM2NUM(rlim.rlim_cur), RLIM2NUM(rlim.rlim_max));
@@ -1996,7 +1996,7 @@
     rlim.rlim_cur = NUM2RLIM(rlim_cur);
     rlim.rlim_max = NUM2RLIM(rlim_max);
 
-    if (setrlimit(NUM2INT(resource), &rlim) < 0) {
+    if (setrlimit(NUM2INT(rb_get_const(rb_mProcess,resource)), &rlim) < 0) {
 	rb_sys_fail("setrlimit");
     }
     return Qnil;
@@ -3585,7 +3585,6 @@
 VALUE rb_mProcGID;
 VALUE rb_mProcID_Syscall;
 
-
 /*
  *  The <code>Process</code> module is a collection of methods used to
  *  manipulate processes.
Index: variable.c
===================================================================
RCS file: /src/ruby/variable.c,v
retrieving revision 1.140
diff -u -r1.140 variable.c
--- variable.c	4 Sep 2006 05:46:47 -0000	1.140
+++ variable.c	12 Sep 2006 09:51:19 -0000
@@ -1340,6 +1340,54 @@
     return rb_const_get_0(klass, id, Qfalse, Qtrue, fallback);
 }
 
+VALUE rb_get_const_at(VALUE klass,VALUE name){
+    ID id;
+    switch (TYPE(name)) {
+      case T_STRING:
+      case T_SYMBOL:
+        id = rb_to_id(name);
+        if (!rb_is_const_id(id)) { 
+          rb_name_error(id, "wrong constant name %s", rb_id2name(id));
+        }
+	return rb_const_get_at(klass, id);
+	break;
+      default:
+	return name;
+    }
+    return Qnil;/*dummy*/
+}
+VALUE rb_get_const(VALUE klass,VALUE name){
+    ID id;
+    switch (TYPE(name)) {
+      case T_STRING:
+      case T_SYMBOL:
+        id = rb_to_id(name);
+        if (!rb_is_const_id(id)) { 
+          rb_name_error(id, "wrong constant name %s", rb_id2name(id));
+        }
+	return rb_const_get(klass, id);
+	break;
+      default:
+	return name;
+    }
+    return Qnil;/*dummy*/
+}
+VALUE
+rb_constantize(VALUE klass, VALUE name)
+{
+    if (rb_obj_is_kind_of(name,rb_cArray)){
+      long i;
+      long res=0;
+      for (i=0;i<RARRAY_LEN(name);i++){
+        res|=NUM2LONG(rb_get_const(klass,RARRAY_PTR(name)[i]));
+      }
+      return LONG2NUM(res);
+    }
+    else 
+      return rb_get_const(klass,name);
+}
+
+
 /*
  *  call-seq:
  *     remove_const(sym)   => obj
@@ -1549,8 +1597,8 @@
     mod_av_set(klass, id, val, Qtrue);
 }
 
-void
-rb_define_const(VALUE klass, const char *name, VALUE val)
+static void
+rb_define_const_old(VALUE klass, const char *name, VALUE val)
 {
     ID id = rb_intern(name);
 
@@ -1563,6 +1611,20 @@
     rb_const_set(klass, id, val);
 }
 
+ /*same as rb_define const but strips only first strip prefixes*/
+void
+rb_define_const_n(VALUE klass, const char *name, VALUE val,int strip)
+{
+    int len=strlen(name),i;
+	rb_define_const_old(klass,name,val);	
+	for (i=0;i<len;i++){
+		if (!strip--)return;
+		if(name[i]=='_') rb_define_const_old(klass,name+i+1,val);
+	}
+}
+void rb_define_const(VALUE klass, const char *name, VALUE val)
+{rb_define_const_n(klass,name,val,-1);}
+
 void
 rb_define_global_const(const char *name, VALUE val)
 {
Index: file.c
===================================================================
RCS file: /src/ruby/file.c,v
retrieving revision 1.249
diff -u -r1.249 file.c
--- file.c	7 Sep 2006 14:27:20 -0000	1.249
+++ file.c	12 Sep 2006 09:51:36 -0000
@@ -4390,11 +4390,11 @@
     rb_define_singleton_method(rb_cFile, "join",   rb_file_s_join, -2);
 
 #ifdef DOSISH
-    rb_define_const(rb_cFile, "ALT_SEPARATOR", rb_obj_freeze(rb_str_new2("\\")));
+    rb_define_const_n(rb_cFile, "ALT_SEPARATOR", rb_obj_freeze(rb_str_new2("\\")),0);
 #else
-    rb_define_const(rb_cFile, "ALT_SEPARATOR", Qnil);
+    rb_define_const_n(rb_cFile, "ALT_SEPARATOR", Qnil,0);
 #endif
-    rb_define_const(rb_cFile, "PATH_SEPARATOR", rb_obj_freeze(rb_str_new2(PATH_SEP)));
+    rb_define_const_n(rb_cFile, "PATH_SEPARATOR", rb_obj_freeze(rb_str_new2(PATH_SEP)),0);
 
     rb_define_method(rb_cIO, "stat",  rb_io_stat, 0); /* this is IO's method */
     rb_define_method(rb_cFile, "lstat",  rb_file_lstat, 0);
Index: marshal.c
===================================================================
RCS file: /src/ruby/marshal.c,v
retrieving revision 1.129
diff -u -r1.129 marshal.c
--- marshal.c	2 Sep 2006 14:42:07 -0000	1.129
+++ marshal.c	12 Sep 2006 09:51:38 -0000
@@ -1412,8 +1412,8 @@
     rb_define_module_function(rb_mMarshal, "load", marshal_load, -1);
     rb_define_module_function(rb_mMarshal, "restore", marshal_load, -1);
 
-    rb_define_const(rb_mMarshal, "MAJOR_VERSION", INT2FIX(MARSHAL_MAJOR));
-    rb_define_const(rb_mMarshal, "MINOR_VERSION", INT2FIX(MARSHAL_MINOR));
+    rb_define_const_n(rb_mMarshal, "MAJOR_VERSION", INT2FIX(MARSHAL_MAJOR),0);
+    rb_define_const_n(rb_mMarshal, "MINOR_VERSION", INT2FIX(MARSHAL_MINOR),0);
 }
 
 VALUE
Index: object.c
===================================================================
RCS file: /src/ruby/object.c,v
retrieving revision 1.200
diff -u -r1.200 object.c
--- object.c	7 Sep 2006 16:11:28 -0000	1.200
+++ object.c	12 Sep 2006 09:51:15 -0000
@@ -1422,7 +1422,6 @@
 rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
 {
     VALUE name, recur;
-    ID id;
 
     if (argc == 1) {
 	name = argv[0];
@@ -1431,11 +1430,7 @@
     else {
 	rb_scan_args(argc, argv, "11", &name, &recur);
     }
-    id = rb_to_id(name);
-    if (!rb_is_const_id(id)) {
-	rb_name_error(id, "wrong constant name %s", rb_id2name(id));
-    }
-    return RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
+    return RTEST(recur) ? rb_get_const(mod, name) : rb_get_const_at(mod, name);
 }
 
 /*


In This Thread

Prev Next