diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-03-24 09:38:37 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-03-24 09:38:37 +0000 |
commit | ece972717b2e752532b222f03500f23b03280327 (patch) | |
tree | 14f984d7ad39dd888fbc20837e8fd6cfd236b20e | |
parent | ab7dac4027c063222ad5e4ea68012020f97c047d (diff) | |
download | ruby-ece972717b2e752532b222f03500f23b03280327.tar.gz |
security enhancement of dl library (need test).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3609 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | eval.c | 9 | ||||
-rw-r--r-- | ext/dl/dl.c | 97 | ||||
-rw-r--r-- | ext/dl/handle.c | 29 | ||||
-rw-r--r-- | ext/dl/ptr.c | 152 | ||||
-rw-r--r-- | ext/dl/sym.c | 20 | ||||
-rw-r--r-- | lib/jcode.rb | 6 | ||||
-rw-r--r-- | object.c | 6 | ||||
-rw-r--r-- | ruby.h | 6 | ||||
-rw-r--r-- | version.h | 8 |
9 files changed, 175 insertions, 158 deletions
@@ -139,7 +139,7 @@ rb_secure(level) } void -rb_check_safe_str(x) +rb_check_safe_obj(x) VALUE x; { if (ruby_safe_level > 0 && OBJ_TAINTED(x)){ @@ -152,6 +152,13 @@ rb_check_safe_str(x) } } rb_secure(4); +} + +void +rb_check_safe_str(x) + VALUE x; +{ + rb_check_safe_obj(x); if (TYPE(x)!= T_STRING) { rb_raise(rb_eTypeError, "wrong argument type %s (expected String)", rb_obj_classname(x)); diff --git a/ext/dl/dl.c b/ext/dl/dl.c index bfdf5e81a6..95bc780e07 100644 --- a/ext/dl/dl.c +++ b/ext/dl/dl.c @@ -24,8 +24,8 @@ rb_dl_scan_callback_args(long stack[], const char *proto, VALUE val; sp = stack; - for( i=1; proto[i]; i++ ){ - switch( proto[i] ){ + for (i=1; proto[i]; i++) { + switch (proto[i]) { case 'C': { char v; @@ -162,11 +162,11 @@ dlsizeof(const char *cstr) len = strlen(cstr); size = 0; - for( i=0; i<len; i++ ){ + for (i=0; i<len; i++) { n = 1; - if( isdigit(cstr[i+1]) ){ + if (isdigit(cstr[i+1])) { dlen = 1; - while( isdigit(cstr[i+dlen]) ){ dlen ++; }; + while (isdigit(cstr[i+dlen])) { dlen ++; }; dlen --; d = ALLOCA_N(char, dlen + 1); strncpy(d, cstr + i + 1, dlen); @@ -177,7 +177,7 @@ dlsizeof(const char *cstr) dlen = 0; } - switch( cstr[i] ){ + switch (cstr[i]) { case 'I': DLALIGN(0,size,INT_ALIGN); case 'i': @@ -234,9 +234,9 @@ c_farray(VALUE v, long *size) len = RARRAY(v)->len; *size = sizeof(float) * len; ary = dlmalloc(*size); - for( i=0; i < len; i++ ){ + for (i=0; i < len; i++) { e = rb_ary_entry(v, i); - switch( TYPE(e) ){ + switch (TYPE(e)) { case T_FLOAT: ary[i] = (float)(RFLOAT(e)->value); break; @@ -262,9 +262,9 @@ c_darray(VALUE v, long *size) len = RARRAY(v)->len; *size = sizeof(double) * len; ary = dlmalloc(*size); - for( i=0; i < len; i++ ){ + for (i=0; i < len; i++) { e = rb_ary_entry(v, i); - switch( TYPE(e) ){ + switch (TYPE(e)) { case T_FLOAT: ary[i] = (double)(RFLOAT(e)->value); break; @@ -290,9 +290,9 @@ c_larray(VALUE v, long *size) len = RARRAY(v)->len; *size = sizeof(long) * len; ary = dlmalloc(*size); - for( i=0; i < len; i++ ){ + for (i=0; i < len; i++) { e = rb_ary_entry(v, i); - switch( TYPE(e) ){ + switch (TYPE(e)) { case T_FIXNUM: case T_BIGNUM: ary[i] = (long)(NUM2INT(e)); @@ -319,9 +319,9 @@ c_iarray(VALUE v, long *size) len = RARRAY(v)->len; *size = sizeof(int) * len; ary = dlmalloc(*size); - for( i=0; i < len; i++ ){ + for (i=0; i < len; i++) { e = rb_ary_entry(v, i); - switch( TYPE(e) ){ + switch (TYPE(e)) { case T_FIXNUM: case T_BIGNUM: ary[i] = (int)(NUM2INT(e)); @@ -348,9 +348,9 @@ c_harray(VALUE v, long *size) len = RARRAY(v)->len; *size = sizeof(short) * len; ary = dlmalloc(*size); - for( i=0; i < len; i++ ){ + for (i=0; i < len; i++) { e = rb_ary_entry(v, i); - switch( TYPE(e) ){ + switch (TYPE(e)) { case T_FIXNUM: case T_BIGNUM: ary[i] = (short)(NUM2INT(e)); @@ -377,9 +377,9 @@ c_carray(VALUE v, long *size) len = RARRAY(v)->len; *size = sizeof(char) * len; ary = dlmalloc(*size); - for( i=0; i < len; i++ ){ + for (i=0; i < len; i++) { e = rb_ary_entry(v, i); - switch( TYPE(e) ){ + switch (TYPE(e)) { case T_FIXNUM: case T_BIGNUM: ary[i] = (char)(NUM2INT(e)); @@ -401,15 +401,23 @@ c_parray(VALUE v, long *size) { int i, len; void **ary; - VALUE e; + VALUE e, tmp; len = RARRAY(v)->len; *size = sizeof(void*) * len; ary = dlmalloc(*size); - for( i=0; i < len; i++ ){ + for (i=0; i < len; i++) { e = rb_ary_entry(v, i); - switch( TYPE(e) ){ + switch (TYPE(e)) { + default: + tmp = rb_check_string_type(e); + if (NIL_P(tmp)) { + rb_raise(rb_eDLTypeError, "unexpected type of the element #%d", i); + } + e = tmp; + /* fall through */ case T_STRING: + rb_check_safe_str(e); { char *str, *src; src = RSTRING(e)->ptr; @@ -421,7 +429,7 @@ c_parray(VALUE v, long *size) ary[i] = NULL; break; case T_DATA: - if( rb_obj_is_kind_of(e, rb_cDLPtrData) ){ + if (rb_obj_is_kind_of(e, rb_cDLPtrData)) { struct ptr_data *pdata; Data_Get_Struct(e, struct ptr_data, pdata); ary[i] = (void*)(pdata->ptr); @@ -430,9 +438,6 @@ c_parray(VALUE v, long *size) rb_raise(rb_eDLTypeError, "unexpected type of the element #%d", i); } break; - default: - rb_raise(rb_eDLTypeError, "unexpected type of the element #%d", i); - break; } } @@ -445,24 +450,26 @@ rb_ary2cary(char t, VALUE v, long *size) int len; VALUE val0; - if( TYPE(v) != T_ARRAY ){ + val0 = rb_check_array_type(v); + if(NIL_P(TYPE(val0))) { rb_raise(rb_eDLTypeError, "an array is expected."); } + v = val0; len = RARRAY(v)->len; - if( len == 0 ){ + if (len == 0) { return NULL; } - if( !size ){ + if (!size) { size = ALLOCA_N(long,1); } val0 = rb_ary_entry(v,0); - switch( TYPE(val0) ){ + switch (TYPE(val0)) { case T_FIXNUM: case T_BIGNUM: - switch( t ){ + switch (t) { case 'C': case 'c': return (void*)c_carray(v,size); case 'H': case 'h': @@ -477,7 +484,7 @@ rb_ary2cary(char t, VALUE v, long *size) case T_STRING: return (void*)c_parray(v,size); case T_FLOAT: - switch( t ){ + switch (t) { case 'F': case 'f': return (void*)c_farray(v,size); case 'D': case 'd': case 0: @@ -485,7 +492,7 @@ rb_ary2cary(char t, VALUE v, long *size) } rb_raise(rb_eDLTypeError, "type mismatch"); case T_DATA: - if( rb_obj_is_kind_of(val0, rb_cDLPtrData) ){ + if (rb_obj_is_kind_of(val0, rb_cDLPtrData)) { return (void*)c_parray(v,size); } rb_raise(rb_eDLTypeError, "type mismatch"); @@ -516,7 +523,7 @@ rb_ary_to_ptr(int argc, VALUE argv[], VALUE self) VALUE t; long size; - switch( rb_scan_args(argc, argv, "01", &t) ){ + switch (rb_scan_args(argc, argv, "01", &t)) { case 1: ptr = rb_ary2cary(StringValuePtr(t)[0], self, &size); break; @@ -556,15 +563,13 @@ rb_dl_malloc(VALUE self, VALUE size) VALUE rb_dl_strdup(VALUE self, VALUE str) { - rb_secure(4); - str = rb_String(str); + SafeStringValue(str); return rb_dlptr_new(strdup(RSTRING(str)->ptr), RSTRING(str)->len, dlfree); } static VALUE rb_dl_sizeof(VALUE self, VALUE str) { - rb_secure(4); return INT2NUM(dlsizeof(StringValuePtr(str))); } @@ -577,9 +582,9 @@ rb_dl_callback(int argc, VALUE argv[], VALUE self) rb_secure(4); proc = Qnil; - switch( rb_scan_args(argc, argv, "11", &type, &proc) ){ + switch (rb_scan_args(argc, argv, "11", &type, &proc)) { case 1: - if( rb_block_given_p() ){ + if (rb_block_given_p()) { proc = rb_f_lambda(); } else{ @@ -589,8 +594,8 @@ rb_dl_callback(int argc, VALUE argv[], VALUE self) break; } - Check_Type(type, T_STRING); - switch( RSTRING(type)->ptr[0] ){ + StringValue(type); + switch (RSTRING(type)->ptr[0]) { case '0': rettype = 0x00; break; @@ -620,13 +625,13 @@ rb_dl_callback(int argc, VALUE argv[], VALUE self) } entry = -1; - for( i=0; i < MAX_CALLBACK; i++ ){ - if( rb_hash_aref(DLFuncTable, rb_assoc_new(INT2NUM(rettype), INT2NUM(i))) == Qnil ){ + for (i=0; i < MAX_CALLBACK; i++) { + if (rb_hash_aref(DLFuncTable, rb_assoc_new(INT2NUM(rettype), INT2NUM(i))) == Qnil) { entry = i; break; } } - if( entry < 0 ){ + if (entry < 0) { rb_raise(rb_eDLError, "too many callbacks are defined."); } @@ -646,9 +651,9 @@ rb_dl_remove_callback(VALUE mod, VALUE sym) rb_secure(4); f = rb_dlsym2csym(sym); - for( i=0; i < CALLBACK_TYPES; i++ ){ - for( j=0; j < MAX_CALLBACK; j++ ){ - if( rb_dl_callback_table[i][j] == f ){ + for (i=0; i < CALLBACK_TYPES; i++) { + for (j=0; j < MAX_CALLBACK; j++) { + if (rb_dl_callback_table[i][j] == f) { rb_hash_aset(DLFuncTable, rb_assoc_new(INT2NUM(i),INT2NUM(j)),Qnil); break; } diff --git a/ext/dl/handle.c b/ext/dl/handle.c index 23ad5ef066..17e8e5221d 100644 --- a/ext/dl/handle.c +++ b/ext/dl/handle.c @@ -10,7 +10,7 @@ VALUE rb_cDLHandle; void dlhandle_free(struct dl_handle *dlhandle) { - if( dlhandle->ptr && dlhandle->open && dlhandle->enable_close ){ + if (dlhandle->ptr && dlhandle->open && dlhandle->enable_close) { dlclose(dlhandle->ptr); } } @@ -50,7 +50,7 @@ rb_dlhandle_initialize(int argc, VALUE argv[], VALUE self) int cflag; const char *err; - switch( rb_scan_args(argc, argv, "11", &lib, &flag) ){ + switch (rb_scan_args(argc, argv, "11", &lib, &flag)) { case 1: clib = StringValuePtr(lib); cflag = RTLD_LAZY | RTLD_GLOBAL; @@ -65,24 +65,24 @@ rb_dlhandle_initialize(int argc, VALUE argv[], VALUE self) ptr = dlopen(clib, cflag); #if defined(HAVE_DLERROR) - if( !ptr && (err = dlerror()) ){ + if (!ptr && (err = dlerror())) { rb_raise(rb_eRuntimeError, err); } #else - if( !ptr ){ + if (!ptr) { err = dlerror(); rb_raise(rb_eRuntimeError, err); } #endif Data_Get_Struct(self, struct dl_handle, dlhandle); - if( dlhandle->ptr && dlhandle->open && dlhandle->enable_close ){ + if (dlhandle->ptr && dlhandle->open && dlhandle->enable_close) { dlclose(dlhandle->ptr); } dlhandle->ptr = ptr; dlhandle->open = 1; dlhandle->enable_close = 0; - if( rb_block_given_p() ){ + if (rb_block_given_p()) { rb_ensure(rb_yield, self, rb_dlhandle_close, self); } @@ -138,14 +138,16 @@ rb_dlhandle_sym(int argc, VALUE argv[], VALUE self) const char *name, *stype; const char *err; - if( rb_scan_args(argc, argv, "11", &sym, &type) == 2 ){ + rb_secure(4); + if (rb_scan_args(argc, argv, "11", &sym, &type) == 2) { + SafeStringValue(type); stype = StringValuePtr(type); } else{ stype = NULL; } - if( sym == Qnil ){ + if (sym == Qnil) { #if defined(RTLD_NEXT) name = RTLD_NEXT; #else @@ -153,21 +155,22 @@ rb_dlhandle_sym(int argc, VALUE argv[], VALUE self) #endif } else{ + SafeStringValue(sym); name = StringValuePtr(sym); } Data_Get_Struct(self, struct dl_handle, dlhandle); - if( ! dlhandle->open ){ + if (! dlhandle->open) { rb_raise(rb_eRuntimeError, "Closed handle."); } handle = dlhandle->ptr; func = dlsym(handle, name); #if defined(HAVE_DLERROR) - if( !func && (err = dlerror()) ) + if (!func && (err = dlerror())) #else - if( !func ) + if (!func) #endif { #if defined(__CYGWIN__) || defined(WIN32) || defined(__MINGW32__) @@ -180,9 +183,9 @@ rb_dlhandle_sym(int argc, VALUE argv[], VALUE self) func = dlsym(handle, name_a); dlfree(name_a); #if defined(HAVE_DLERROR) - if( !func && (err = dlerror()) ) + if (!func && (err = dlerror())) #else - if( !func ) + if (!func) #endif { rb_raise(rb_eRuntimeError, "Unknown symbol \"%sA\".", name); diff --git a/ext/dl/ptr.c b/ext/dl/ptr.c index a7a9aebbd6..1eb0b82730 100644 --- a/ext/dl/ptr.c +++ b/ext/dl/ptr.c @@ -33,7 +33,7 @@ rb_dlmem_delete(void *ptr) static void rb_dlmem_aset(void *ptr, VALUE obj) { - if( obj == Qnil ){ + if (obj == Qnil) { rb_dlmem_delete(ptr); } else{ @@ -53,22 +53,22 @@ rb_dlmem_aref(void *ptr) void dlptr_free(struct ptr_data *data) { - if( data->ptr ){ + if (data->ptr) { DEBUG_CODE({ printf("dlptr_free(): removing the pointer `0x%x' from the MemorySpace\n", data->ptr); }); rb_dlmem_delete(data->ptr); - if( data->free ){ + if (data->free) { DEBUG_CODE({ printf("dlptr_free(): 0x%x(data->ptr:0x%x)\n",data->free,data->ptr); }); (*(data->free))(data->ptr); } } - if( data->stype ) dlfree(data->stype); - if( data->ssize ) dlfree(data->ssize); - if( data->ids ) dlfree(data->ids); + if (data->stype) dlfree(data->stype); + if (data->ssize) dlfree(data->ssize); + if (data->ids) dlfree(data->ids); } void @@ -82,6 +82,7 @@ dlptr_init(VALUE val) data->ptr); }); rb_dlmem_aset(data->ptr, val); + OBJ_TAINT(val); } VALUE @@ -91,9 +92,9 @@ rb_dlptr_new2(VALUE klass, void *ptr, long size, freefunc_t func) VALUE val; rb_secure(4); - if( ptr ){ + if (ptr) { val = rb_dlmem_aref(ptr); - if( val == Qnil ){ + if (val == Qnil) { val = Data_Make_Struct(klass, struct ptr_data, 0, dlptr_free, data); data->ptr = ptr; @@ -108,7 +109,7 @@ rb_dlptr_new2(VALUE klass, void *ptr, long size, freefunc_t func) dlptr_init(val); } else{ - if( func ){ + if (func) { Data_Get_Struct(val, struct ptr_data, data); data->free = func; } @@ -144,11 +145,11 @@ rb_dlptr2cptr(VALUE val) struct ptr_data *data; void *ptr; - if( rb_obj_is_kind_of(val, rb_cDLPtrData) ){ + if (rb_obj_is_kind_of(val, rb_cDLPtrData)) { Data_Get_Struct(val, struct ptr_data, data); ptr = data->ptr; } - else if( val == Qnil ){ + else if (val == Qnil) { ptr = NULL; } else{ @@ -188,7 +189,7 @@ rb_dlptr_initialize(int argc, VALUE argv[], VALUE self) freefunc_t f = NULL; long s = 0; - switch( rb_scan_args(argc, argv, "12", &ptr, &size, &sym) ){ + switch (rb_scan_args(argc, argv, "12", &ptr, &size, &sym)) { case 1: p = (void*)(DLNUM2LONG(rb_Integer(ptr))); break; @@ -205,9 +206,9 @@ rb_dlptr_initialize(int argc, VALUE argv[], VALUE self) rb_bug("rb_dlptr_initialize"); } - if( p ){ + if (p) { Data_Get_Struct(self, struct ptr_data, data); - if( data->ptr && data->free ){ + if (data->ptr && data->free) { /* Free previous memory. Use of inappropriate initialize may cause SEGV. */ (*(data->free))(data->ptr); } @@ -226,7 +227,7 @@ rb_dlptr_s_malloc(int argc, VALUE argv[], VALUE klass) int s; freefunc_t f = NULL; - switch( rb_scan_args(argc, argv, "11", &size, &sym) ){ + switch (rb_scan_args(argc, argv, "11", &size, &sym)) { case 1: s = NUM2INT(size); break; @@ -313,14 +314,14 @@ rb_dlptr_to_array(int argc, VALUE argv[], VALUE self) Data_Get_Struct(self, struct ptr_data, data); - switch( rb_scan_args(argc, argv, "11", &type, &size) ){ + switch (rb_scan_args(argc, argv, "11", &type, &size)) { case 2: t = StringValuePtr(type)[0]; n = NUM2INT(size); break; case 1: t = StringValuePtr(type)[0]; - switch( t ){ + switch (t) { case 'C': n = data->size; break; @@ -343,9 +344,9 @@ rb_dlptr_to_array(int argc, VALUE argv[], VALUE self) n = data->size / sizeof(void*); break; default: - if( t == 'p' || t == 's' ){ + if (t == 'p' || t == 's') { int i; - for( i=0; ((void**)(data->ptr))[i]; i++ ){}; + for (i=0; ((void**)(data->ptr))[i]; i++) {}; n = i; } else{ @@ -359,8 +360,8 @@ rb_dlptr_to_array(int argc, VALUE argv[], VALUE self) ary = rb_ary_new(); - for( i=0; i < n; i++ ){ - switch( t ){ + for (i=0; i < n; i++) { + switch (t) { case 'C': rb_ary_push(ary, INT2NUM(((char*)(data->ptr))[i])); break; @@ -381,7 +382,7 @@ rb_dlptr_to_array(int argc, VALUE argv[], VALUE self) case 'S': { char *str = ((char**)(data->ptr))[i]; - if( str ){ + if (str) { rb_ary_push(ary, rb_tainted_str_new2(str)); } else{ @@ -392,7 +393,7 @@ rb_dlptr_to_array(int argc, VALUE argv[], VALUE self) case 's': { char *str = ((char**)(data->ptr))[i]; - if( str ){ + if (str) { rb_ary_push(ary, rb_tainted_str_new2(str)); xfree(str); } @@ -423,7 +424,7 @@ rb_dlptr_to_s(int argc, VALUE argv[], VALUE self) int len; Data_Get_Struct(self, struct ptr_data, data); - switch( rb_scan_args(argc, argv, "01", &arg1) ){ + switch (rb_scan_args(argc, argv, "01", &arg1)) { case 0: val = rb_tainted_str_new2((char*)(data->ptr)); break; @@ -446,7 +447,7 @@ rb_dlptr_to_str(int argc, VALUE argv[], VALUE self) int len; Data_Get_Struct(self, struct ptr_data, data); - switch( rb_scan_args(argc, argv, "01", &arg1) ){ + switch (rb_scan_args(argc, argv, "01", &arg1)) { case 0: val = rb_tainted_str_new((char*)(data->ptr),data->size); break; @@ -527,16 +528,16 @@ rb_dlptr_define_data_type(int argc, VALUE argv[], VALUE self) rb_scan_args(argc, argv, "11*", &data_type, &type, &rest); Data_Get_Struct(self, struct ptr_data, data); - if( argc == 1 || (argc == 2 && type == Qnil) ){ - if( NUM2INT(data_type) == DLPTR_CTYPE_UNKNOWN ){ + if (argc == 1 || (argc == 2 && type == Qnil)) { + if (NUM2INT(data_type) == DLPTR_CTYPE_UNKNOWN) { data->ctype = DLPTR_CTYPE_UNKNOWN; data->slen = 0; data->ids_num = 0; - if( data->stype ){ + if (data->stype) { dlfree(data->stype); data->stype = NULL; } - if( data->ids ){ + if (data->ids) { dlfree(data->ids); data->ids = NULL; } @@ -551,32 +552,29 @@ rb_dlptr_define_data_type(int argc, VALUE argv[], VALUE self) StringValue(type); Check_Type(rest, T_ARRAY); num = RARRAY(rest)->len; - for( i=0; i<num; i++ ){ - vid = rb_ary_entry(rest,i); - if( !(TYPE(vid)==T_STRING || TYPE(vid)==T_SYMBOL) ){ - rb_raise(rb_eTypeError, "#%d must be a string or symbol", i + 2); - } + for (i=0; i<num; i++) { + rb_to_id(rb_ary_entry(rest,i)); } data->ctype = t; data->slen = num; data->ids_num = num; - if( data->stype ) dlfree(data->stype); + if (data->stype) dlfree(data->stype); data->stype = (char*)dlmalloc(sizeof(char) * num); - if( data->ssize ) dlfree(data->ssize); + if (data->ssize) dlfree(data->ssize); data->ssize = (int*)dlmalloc(sizeof(int) * num); - if( data->ids ) dlfree(data->ids); + if (data->ids) dlfree(data->ids); data->ids = (ID*)dlmalloc(sizeof(ID) * data->ids_num); ctype = StringValuePtr(type); - for( i=0; i<num; i++ ){ + for (i=0; i<num; i++) { vid = rb_ary_entry(rest,i); data->ids[i] = rb_to_id(vid); data->stype[i] = *ctype; ctype ++; - if( isdigit(*ctype) ){ + if (isdigit(*ctype)) { char *p, *d; - for( p=ctype; isdigit(*p); p++ ) ; + for (p=ctype; isdigit(*p); p++) ; d = ALLOCA_N(char, p - ctype + 1); strncpy(d, ctype, p - ctype); d[p - ctype] = '\0'; @@ -588,11 +586,11 @@ rb_dlptr_define_data_type(int argc, VALUE argv[], VALUE self) } } - if( *ctype ){ + if (*ctype) { rb_raise(rb_eArgError, "too few/many arguments"); } - if( !data->size ) + if (!data->size) data->size = dlsizeof(RSTRING(type)->ptr); return Qnil; @@ -607,7 +605,7 @@ rb_dlptr_define_struct(int argc, VALUE argv[], VALUE self) pass_argc = argc + 1; pass_argv = ALLOCA_N(VALUE, pass_argc); pass_argv[0] = INT2FIX(DLPTR_CTYPE_STRUCT); - for( i=1; i<pass_argc; i++ ){ + for (i=1; i<pass_argc; i++) { pass_argv[i] = argv[i-1]; } return rb_dlptr_define_data_type(pass_argc, pass_argv, self); @@ -622,7 +620,7 @@ rb_dlptr_define_union(int argc, VALUE argv[], VALUE self) pass_argc = argc + 1; pass_argv = ALLOCA_N(VALUE, pass_argc); pass_argv[0] = INT2FIX(DLPTR_CTYPE_UNION); - for( i=1; i<pass_argc; i++ ){ + for (i=1; i<pass_argc; i++) { pass_argv[i] = argv[i-1]; } return rb_dlptr_define_data_type(pass_argc, pass_argv, self); @@ -634,7 +632,7 @@ rb_dlptr_get_data_type(VALUE self) struct ptr_data *data; Data_Get_Struct(self, struct ptr_data, data); - if( data->stype ) + if (data->stype) return rb_assoc_new(INT2FIX(data->ctype), rb_tainted_str_new(data->stype, data->slen)); else @@ -648,11 +646,11 @@ cary2ary(void *ptr, char t, int len) VALUE elem; int i; - if( len < 1 ) + if (len < 1) return Qnil; - if( len == 1 ){ - switch( t ){ + if (len == 1) { + switch (t) { case 'I': elem = INT2NUM(*((int*)ptr)); ptr = (char *)ptr + sizeof(int); @@ -689,8 +687,8 @@ cary2ary(void *ptr, char t, int len) } ary = rb_ary_new(); - for( i=0; i < len; i++ ){ - switch( t ){ + for (i=0; i < len; i++) { + switch (t) { case 'I': elem = INT2NUM(*((int*)ptr)); ptr = (char *)ptr + sizeof(int); @@ -738,28 +736,28 @@ rb_dlptr_aref(int argc, VALUE argv[], VALUE self) int i; int offset; - if( rb_scan_args(argc, argv, "11", &key, &num) == 1 ){ + if (rb_scan_args(argc, argv, "11", &key, &num) == 1) { num = INT2NUM(0); } - if( TYPE(key) == T_FIXNUM || TYPE(key) == T_BIGNUM ){ + if (TYPE(key) == T_FIXNUM || TYPE(key) == T_BIGNUM) { VALUE pass[1]; pass[0] = num; return rb_dlptr_to_str(1, pass, rb_dlptr_plus(self, key)); } - - if( ! (TYPE(key) == T_STRING || TYPE(key) == T_SYMBOL ) ){ + rb_to_id(key); + if (! (TYPE(key) == T_STRING || TYPE(key) == T_SYMBOL)) { rb_raise(rb_eTypeError, "the key must be a string or symbol"); } id = rb_to_id(key); Data_Get_Struct(self, struct ptr_data, data); offset = 0; - switch( data->ctype ){ + switch (data->ctype) { case DLPTR_CTYPE_STRUCT: - for( i=0; i < data->ids_num; i++ ){ - if( data->ids[i] == id ){ - switch( data->stype[i] ){ + for (i=0; i < data->ids_num; i++) { + if (data->ids[i] == id) { + switch (data->stype[i]) { case 'I': DLALIGN(data->ptr,offset,INT_ALIGN); break; @@ -786,7 +784,7 @@ rb_dlptr_aref(int argc, VALUE argv[], VALUE self) } return cary2ary((char *)data->ptr + offset, data->stype[i], data->ssize[i]); } - switch( data->stype[i] ){ + switch (data->stype[i]) { case 'I': offset += sizeof(int) * data->ssize[i]; break; @@ -815,8 +813,8 @@ rb_dlptr_aref(int argc, VALUE argv[], VALUE self) } break; case DLPTR_CTYPE_UNION: - for( i=0; i < data->ids_num; i++ ){ - if( data->ids[i] == id ){ + for (i=0; i < data->ids_num; i++) { + if (data->ids[i] == id) { return cary2ary((char *)data->ptr + offset, data->stype[i], data->ssize[i]); } } @@ -834,7 +832,7 @@ ary2cary(char t, VALUE val, long *size) { void *ptr; - if( TYPE(val) == T_ARRAY ){ + if (TYPE(val) == T_ARRAY) { ptr = rb_ary2cary(t, val, size); } else{ @@ -855,14 +853,14 @@ rb_dlptr_aset(int argc, VALUE argv[], VALUE self) void *memimg; rb_secure(4); - switch( rb_scan_args(argc, argv, "21", &key, &num, &val) ){ + switch (rb_scan_args(argc, argv, "21", &key, &num, &val)) { case 2: val = num; num = Qnil; break; } - if( TYPE(key) == T_FIXNUM || TYPE(key) == T_BIGNUM ){ + if (TYPE(key) == T_FIXNUM || TYPE(key) == T_BIGNUM) { void *dst, *src; long len; @@ -871,29 +869,25 @@ rb_dlptr_aset(int argc, VALUE argv[], VALUE self) dst = (void*)((long)(data->ptr) + DLNUM2LONG(key)); src = RSTRING(val)->ptr; len = RSTRING(val)->len; - if( num == Qnil ){ + if (num == Qnil) { memcpy(dst, src, len); } else{ long n = NUM2INT(num); memcpy(dst, src, n < len ? n : len); - if( n > len ) MEMZERO((char*)dst + len, char, n - len); + if (n > len) MEMZERO((char*)dst + len, char, n - len); } return val; } - if( ! (TYPE(key) == T_STRING || TYPE(key) == T_SYMBOL ) ){ - rb_raise(rb_eTypeError, "the key must be a string or symbol"); - } - id = rb_to_id(key); Data_Get_Struct(self, struct ptr_data, data); - switch( data->ctype ){ + switch (data->ctype) { case DLPTR_CTYPE_STRUCT: offset = 0; - for( i=0; i < data->ids_num; i++ ){ - if( data->ids[i] == id ){ - switch( data->stype[i] ){ + for (i=0; i < data->ids_num; i++) { + if (data->ids[i] == id) { + switch (data->stype[i]) { case 'I': DLALIGN(data->ptr,offset,INT_ALIGN); break; @@ -922,7 +916,7 @@ rb_dlptr_aset(int argc, VALUE argv[], VALUE self) memcpy((char *)data->ptr + offset, memimg, memsize); return val; } - switch( data->stype[i] ){ + switch (data->stype[i]) { case 'I': case 'i': offset += sizeof(int) * data->ssize[i]; @@ -960,9 +954,9 @@ rb_dlptr_aset(int argc, VALUE argv[], VALUE self) return val; /* break; */ case DLPTR_CTYPE_UNION: - for( i=0; i < data->ids_num; i++ ){ - if( data->ids[i] == id ){ - switch( data->stype[i] ){ + for (i=0; i < data->ids_num; i++) { + if (data->ids[i] == id) { + switch (data->stype[i]) { case 'I': case 'i': memsize = sizeof(int) * data->ssize[i]; break; @@ -1007,7 +1001,7 @@ rb_dlptr_size(int argc, VALUE argv[], VALUE self) { VALUE size; - if( rb_scan_args(argc, argv, "01", &size) == 0){ + if (rb_scan_args(argc, argv, "01", &size) == 0){ return DLLONG2NUM(RDLPTR(self)->size); } else{ diff --git a/ext/dl/sym.c b/ext/dl/sym.c index 7e18e61b5e..2034c0a7ff 100644 --- a/ext/dl/sym.c +++ b/ext/dl/sym.c @@ -330,7 +330,7 @@ rb_dlsym_call(int argc, VALUE argv[], VALUE self) long ftype; void *func; - rb_secure(4); + rb_secure(2); Data_Get_Struct(self, struct sym_data, sym); DEBUG_CODE({ printf("rb_dlsym_call(): type = '%s', func = 0x%x\n", sym->type, sym->func); @@ -457,20 +457,20 @@ rb_dlsym_call(int argc, VALUE argv[], VALUE self) ANY2S(args[i]) = DLSTR(0); } else{ - if( TYPE(argv[i]) != T_STRING ){ - rb_raise(rb_eDLError, "#%d must be a string",i); - } - ANY2S(args[i]) = DLSTR(RSTRING(argv[i])->ptr); + VALUE str = argv[i]; + SafeStringValue(str); + ANY2S(args[i]) = DLSTR(RSTRING(str)->ptr); } PUSH_P(ftype); break; case 's': - if( TYPE(argv[i]) != T_STRING ){ - rb_raise(rb_eDLError, "#%d must be a string",i); + { + VALUE str = argv[i]; + SafeStringValue(str); + ANY2S(args[i]) = DLSTR(dlmalloc(RSTRING(str)->len + 1)); + memcpy((char*)(ANY2S(args[i])), RSTRING(str)->ptr, RSTRING(str)->len + 1); + dtypes[i] = 's'; } - ANY2S(args[i]) = DLSTR(dlmalloc(RSTRING(argv[i])->len + 1)); - memcpy((char*)(ANY2S(args[i])), RSTRING(argv[i])->ptr, RSTRING(argv[i])->len + 1); - dtypes[i] = 's'; PUSH_P(ftype); break; default: diff --git a/lib/jcode.rb b/lib/jcode.rb index ab6ab60701..8064518017 100644 --- a/lib/jcode.rb +++ b/lib/jcode.rb @@ -19,9 +19,9 @@ class String PATTERN_EUC = '[\xa1-\xfe][\xa1-\xfe]' PATTERN_UTF8 = '[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf][\x80-\xbf]' - RE_SJIS = Regexp.new(PATTERN_SJIS, 'n') - RE_EUC = Regexp.new(PATTERN_EUC, 'n') - RE_UTF8 = Regexp.new(PATTERN_UTF8, 'n') + RE_SJIS = Regexp.new(PATTERN_SJIS, 0, 'n') + RE_EUC = Regexp.new(PATTERN_EUC, 0, 'n') + RE_UTF8 = Regexp.new(PATTERN_UTF8, 0, 'n') SUCC = {} SUCC['s'] = Hash.new(1) @@ -773,12 +773,14 @@ ID rb_to_id(name) VALUE name; { + VALUE tmp; ID id; switch (TYPE(name)) { case T_STRING: return rb_intern(RSTRING(name)->ptr); case T_FIXNUM: + rb_warn("do not use Fixnums as Symbols"); id = FIX2LONG(name); if (!rb_id2name(id)) { rb_raise(rb_eArgError, "%ld is not a symbol", id); @@ -788,6 +790,10 @@ rb_to_id(name) id = SYM2ID(name); break; default: + tmp = rb_check_string_type(name); + if (!NIL_P(tmp)) { + return rb_intern(RSTRING(tmp)->ptr); + } rb_raise(rb_eTypeError, "%s is not a symbol", RSTRING(rb_inspect(name))->ptr); } return id; @@ -215,12 +215,14 @@ VALUE rb_string_value _((volatile VALUE*)); char *rb_string_value_ptr _((volatile VALUE*)); #define StringValue(v) if (TYPE(v) != T_STRING) rb_string_value(&(v)) +#define StringValuePtr(v) rb_string_value_ptr(&(v)) + +void rb_check_safe_obj _((VALUE)); void rb_check_safe_str _((VALUE)); #define SafeStringValue(v) do {\ StringValue(v);\ - rb_check_safe_str(v);\ + rb_check_safe_obj(v);\ } while (0) -#define StringValuePtr(v) rb_string_value_ptr(&(v)) /* obsolete macro - use SafeStringValue(v) */ #define Check_SafeStr(v) rb_check_safe_str((VALUE)(v)) @@ -1,11 +1,11 @@ #define RUBY_VERSION "1.8.0" -#define RUBY_RELEASE_DATE "2003-03-23" +#define RUBY_RELEASE_DATE "2003-03-24" #define RUBY_VERSION_CODE 180 -#define RUBY_RELEASE_CODE 20030323 +#define RUBY_RELEASE_CODE 20030324 #define RUBY_VERSION_MAJOR 1 #define RUBY_VERSION_MINOR 8 #define RUBY_VERSION_TEENY 0 #define RUBY_RELEASE_YEAR 2003 -#define RUBY_RELEASE_MONTH 03 -#define RUBY_RELEASE_DAY 23 +#define RUBY_RELEASE_MONTH 3 +#define RUBY_RELEASE_DAY 24 |