From 22f1926644a213a39cd8a3f83a80518e5c15797f Mon Sep 17 00:00:00 2001 From: matz Date: Wed, 5 Oct 2005 16:15:16 +0000 Subject: * range.c (rb_range_beg_len): should return Qfalse for non-range object. * pack.c (EXTEND16): [ruby-dev:27383] * eval.c (set_trace_func): add rb_secure(4) to prevent adding tracing function. * lib/delegate.rb: document update from James Edward Gray II . [ruby-core:05942] * process.c (proc_daemon): should restrict execution on levels higher than $SAFE=2. suggested by URABE Shyouhei . * lib/forwardable.rb: replaced by new implementation from . [ruby-core:05899] * file.c (path_check_0): disallow sticky world writable directory in PATH (and $LOAD_PATH). [ruby-dev:27226] * numeric.c (fix_idiv): 1.div(1.0) should return integer value. [ruby-dev:27235] * lib/yaml.rb: require 'yaml/constants'. [ruby-core:5776] * lib/xmlrpc/client.rb (XMLRPC::Client::do_rpc): add charset information to content-type header.[ruby-core:5127] * lib/xmlrpc/server.rb (CGIServer::serve): ditto. * lib/xmlrpc/server.rb (ModRubyServer::serve): ditto. * lib/xmlrpc/server.rb (WEBrickServlet::service): ditto. * test/dbm/test_dbm.rb (TestDBM::test_s_open_error): remove test_s_open_error test to detect duplicate open. [ruby-dev:27202] * eval.c (splat_value): use to_a to splat non Array object. * object.c (nil_to_a): remove nil.to_a. [experimental] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9349 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 72 +++++++++++++- defines.h | 4 + eval.c | 7 +- file.c | 25 +++-- gc.c | 24 ++++- lib/.document | 3 + lib/delegate.rb | 195 +++++++++++++++++++++++++++++++++----- lib/forwardable.rb | 206 +++++++++++++++++++++++++++++------------ lib/optparse.rb | 2 +- lib/xmlrpc/client.rb | 2 +- lib/xmlrpc/server.rb | 6 +- lib/yaml.rb | 1 + numeric.c | 50 +++++++--- object.c | 16 ---- pack.c | 4 +- process.c | 1 + range.c | 4 +- ruby.h | 4 +- sample/test.rb | 69 +------------- test/dbm/test_dbm.rb | 8 -- test/xmlrpc/webrick_testing.rb | 4 +- 21 files changed, 492 insertions(+), 215 deletions(-) diff --git a/ChangeLog b/ChangeLog index c6d0c771c3..0bbed2a78d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,13 +1,22 @@ -Wed Oct 05 04:42:38 2005 GOTOU Yuuzou +Thu Oct 6 01:02:19 2005 Yukihiro Matsumoto + + * range.c (rb_range_beg_len): should return Qfalse for non-range + object. + +Wed Oct 5 04:42:38 2005 GOTOU Yuuzou * lib/xmlrpc/server.rb (XMLRPC::Server#initialize): should mount the servlet on "/". -Wed Oct 05 04:06:49 2005 GOTOU Yuuzou +Wed Oct 5 04:06:49 2005 GOTOU Yuuzou * lib/xmlrpc/server.rb (XMLRPC::Server#serve): delete wrong call of "join". +Mon Oct 3 00:04:00 2005 Kazuhiro NISHIYAMA + + * pack.c (EXTEND16): [ruby-dev:27383] + Sat Oct 1 23:55:24 2005 NAKAMURA Usaku * win32/win32.c (do_select, rb_w32_select): brush up. @@ -73,6 +82,11 @@ Wed Sep 28 03:16:41 2005 NAKAMURA Usaku * rubysig.h: fixed build problem with --enable-pthread on platforms which don't have setitimer(). +Mon Sep 26 22:32:13 2005 Yukihiro Matsumoto + + * eval.c (set_trace_func): add rb_secure(4) to prevent adding + tracing function. + Mon Sep 26 20:59:28 2005 Hirokazu Yamamoto * parse.y: changed to ANSI function style. @@ -132,6 +146,7 @@ Sat Sep 24 11:44:28 2005 Minero Aoki * test/net/http/test_http.rb: new file. +>>>>>>> 1.4662 Sat Sep 24 08:54:05 2005 Minero Aoki * lib/fileutils.rb (cd): no longer accept :noop option, related @@ -193,6 +208,17 @@ Sat Sep 24 06:35:15 2005 Minero Aoki * ext/ripper/lib/ripper/sexp.rb: ditto. +Sat Sep 24 02:40:20 2005 Yukihiro Matsumoto + + * lib/delegate.rb: document update from James Edward Gray II + . [ruby-core:05942] + +Sat Sep 24 02:05:51 2005 Yukihiro Matsumoto + + * process.c (proc_daemon): should restrict execution on levels + higher than $SAFE=2. suggested by URABE Shyouhei + . + Fri Sep 23 20:10:35 2005 Nobuyoshi Nakada * ext/ripper/tools/generate.rb, ext/ripper/tools/preproc.rb: StringIO @@ -203,6 +229,11 @@ Fri Sep 23 17:36:48 2005 Masaki Suketa * ext/win32ole/win32ole.c: avoid core dump with WIN32OLE_EVENT. [ruby-dev:27133] +Fri Sep 23 16:27:39 2005 Yukihiro Matsumoto + + * lib/forwardable.rb: replaced by new implementation from + . [ruby-core:05899] + Fri Sep 23 07:07:47 2005 Minero Aoki * test/ripper/depend: use --output option instead of redirect; @@ -304,6 +335,16 @@ Wed Sep 21 07:43:58 2005 Nobuyoshi Nakada * lib/optparse.rb (default_argv, Arguable#options): defaults strings to be parsed to Arguable instance. +Wed Sep 21 02:44:09 2005 Yukihiro Matsumoto + + * file.c (path_check_0): disallow sticky world writable directory + in PATH (and $LOAD_PATH). [ruby-dev:27226] + +Wed Sep 21 00:32:22 2005 Yukihiro Matsumoto + + * numeric.c (fix_idiv): 1.div(1.0) should return integer value. + [ruby-dev:27235] + Tue Sep 20 22:25:43 2005 Nobuyoshi Nakada * ext/io/wait/lib/nonblock.rb: disable on platforms non-blocking flag @@ -315,6 +356,21 @@ Tue Sep 20 18:23:04 2005 Tanaka Akira * eval.c (thread_mark): mark th->last_status. [ruby-dev:27179] +Tue Sep 20 18:20:33 2005 Yukihiro Matsumoto + + * lib/yaml.rb: require 'yaml/constants'. [ruby-core:5776] + +Tue Sep 20 17:48:34 2005 Yukihiro Matsumoto + + * lib/xmlrpc/client.rb (XMLRPC::Client::do_rpc): add charset + information to content-type header.[ruby-core:5127] + + * lib/xmlrpc/server.rb (CGIServer::serve): ditto. + + * lib/xmlrpc/server.rb (ModRubyServer::serve): ditto. + + * lib/xmlrpc/server.rb (WEBrickServlet::service): ditto. + Tue Sep 20 17:26:42 2005 Hirokazu Yamamoto * test/webrick/test_cgi.rb: set ENV["PATH"] to CGIEnvPath on @@ -326,6 +382,12 @@ Tue Sep 20 17:14:10 2005 Hirokazu Yamamoto * io.c: PIPE_BUF is not defined in BeOS. use _POSIX_PIPE_BUF instead. [ruby-dev:27185] +Tue Sep 20 17:10:38 2005 Yukihiro Matsumoto + + * test/dbm/test_dbm.rb (TestDBM::test_s_open_error): remove + test_s_open_error test to detect duplicate open. + [ruby-dev:27202] + Tue Sep 20 15:39:40 2005 why the lucky stiff * ext/syck/emitter.c (syck_scan_scalar): prevent indicators from @@ -348,6 +410,12 @@ Tue Sep 20 05:48:26 2005 Hirokazu Yamamoto should not include 'webrick/https' unless 'use_ssl' because it fails where openssl is not installed. +Tue Sep 20 01:24:45 2005 Yukihiro Matsumoto + + * eval.c (splat_value): use to_a to splat non Array object. + + * object.c (nil_to_a): remove nil.to_a. [experimental] + Tue Sep 20 01:01:41 2005 Yukihiro Matsumoto * lib/mathn.rb (Fixnum): remove debug print. diff --git a/defines.h b/defines.h index 9b5154e580..48ca3aa8a1 100644 --- a/defines.h +++ b/defines.h @@ -42,13 +42,17 @@ #endif #define xmalloc ruby_xmalloc +#define xmalloc2 ruby_xmalloc2 #define xcalloc ruby_xcalloc #define xrealloc ruby_xrealloc +#define xrealloc2 ruby_xrealloc2 #define xfree ruby_xfree void *xmalloc(long); +void *xmalloc2(long,long); void *xcalloc(long,long); void *xrealloc(void*,long); +void *xrealloc2(void*,long,long); void xfree(void*); #if SIZEOF_LONG_LONG > 0 diff --git a/eval.c b/eval.c index 4b5e0eb696..54ca11cfa6 100644 --- a/eval.c +++ b/eval.c @@ -2464,6 +2464,7 @@ set_trace_func(VALUE obj, VALUE trace) { rb_event_hook_t *hook; + rb_secure(4); if (NIL_P(trace)) { trace_func = 0; rb_remove_event_hook(call_trace_func); @@ -2644,11 +2645,7 @@ avalue_splat(VALUE v) static VALUE splat_value(VALUE v) { - VALUE tmp = rb_check_array_type(v); - - if (NIL_P(tmp)) v = rb_ary_new3(1, v); - else v = tmp; - return rb_values_from_ary(v); + return rb_values_from_ary(rb_convert_type(v, T_ARRAY, "Array", "to_a")); } static VALUE diff --git a/file.c b/file.c index e769f09801..a69d8369d8 100644 --- a/file.c +++ b/file.c @@ -3840,7 +3840,7 @@ is_absolute_path(const char *path) #ifndef DOSISH static int -path_check_1(VALUE path) +path_check_0(VALUE path, int loadpath) { struct stat st; char *p0 = StringValueCStr(path); @@ -3855,7 +3855,7 @@ path_check_1(VALUE path) rb_str_cat2(newpath, "/"); rb_str_cat2(newpath, p0); - return path_check_1(newpath); + return path_check_0(newpath, loadpath); } for (;;) { #ifndef S_IWOTH @@ -3863,7 +3863,7 @@ path_check_1(VALUE path) #endif if (stat(p0, &st) == 0 && S_ISDIR(st.st_mode) && (st.st_mode & S_IWOTH) #ifdef S_ISVTX - && !(st.st_mode & S_ISVTX) + && (loadpath || !(st.st_mode & S_ISVTX)) #endif && !access(p0, W_OK)) { rb_warn("Insecure world writable dir %s, mode 0%o", p0, st.st_mode); @@ -3879,6 +3879,17 @@ path_check_1(VALUE path) } #endif +static int +fpath_check(path) + char *path; +{ +#ifndef DOSISH + return path_check_0(rb_str_new2(path), Qfalse); +#else + return 1; +#endif +} + int rb_path_check(const char *path) { @@ -3894,7 +3905,7 @@ rb_path_check(const char *path) if (!p) p = pend; for (;;) { - if (!path_check_1(rb_str_new(p0, p - p0))) { + if (!path_check_0(rb_str_new(p0, p - p0), Qtrue)) { return 0; /* not safe */ } p0 = p + 1; @@ -4001,7 +4012,7 @@ rb_find_file(VALUE path) #if defined(__MACOS__) || defined(riscos) if (is_macos_native_path(f)) { - if (rb_safe_level() >= 1 && !rb_path_check(f)) { + if (rb_safe_level() >= 1 && !fpath_check(f)) { rb_raise(rb_eSecurityError, "loading from unsafe file %s", f); } if (file_load_ok(f)) return path; @@ -4009,7 +4020,7 @@ rb_find_file(VALUE path) #endif if (is_absolute_path(f)) { - if (rb_safe_level() >= 1 && !rb_path_check(f)) { + if (rb_safe_level() >= 1 && !fpath_check(f)) { rb_raise(rb_eSecurityError, "loading from unsafe file %s", f); } if (file_load_ok(f)) return path; @@ -4050,7 +4061,7 @@ rb_find_file(VALUE path) return 0; /* no path, no load */ } f = dln_find_file(f, lpath); - if (rb_safe_level() >= 1 && !rb_path_check(f)) { + if (rb_safe_level() >= 1 && !fpath_check(f)) { rb_raise(rb_eSecurityError, "loading from unsafe file %s", f); } if (file_load_ok(f)) { diff --git a/gc.c b/gc.c index ed006e17d6..3bba989a10 100644 --- a/gc.c +++ b/gc.c @@ -133,12 +133,22 @@ ruby_xmalloc(long size) return mem; } +void * +ruby_xmalloc2(long n, long size) +{ + long len = size * n; + if (len < n || (n > 0 && len < size)) { + rb_raise(rb_eArgError, "malloc: possible integer overflow"); + } + return ruby_xmalloc(len); +} + void * ruby_xcalloc(long n, long size) { void *mem; - mem = xmalloc(n * size); + mem = ruby_xmalloc2(n, size); memset(mem, 0, n * size); return mem; @@ -152,7 +162,7 @@ ruby_xrealloc(void *ptr, long size) if (size < 0) { rb_raise(rb_eArgError, "negative re-allocation size"); } - if (!ptr) return xmalloc(size); + if (!ptr) return ruby_xmalloc(size); if (size == 0) size = 1; malloc_increase += size; RUBY_CRITICAL(mem = realloc(ptr, size)); @@ -168,6 +178,16 @@ ruby_xrealloc(void *ptr, long size) return mem; } +void * +ruby_xrealloc2(void *ptr, long n, long size) +{ + long len = size * n; + if (len < n || (n > 0 && len < size)) { + rb_raise(rb_eArgError, "realloc: possible integer overflow"); + } + return ruby_xrealloc(ptr, len); +} + void ruby_xfree(void *x) { diff --git a/lib/.document b/lib/.document index f917e02070..8abcd21d93 100644 --- a/lib/.document +++ b/lib/.document @@ -14,8 +14,11 @@ cgi cgi.rb complex.rb date.rb +delegate.rb +erb.rb fileutils.rb find.rb +forwardable.rb generator.rb logger.rb matrix.rb diff --git a/lib/delegate.rb b/lib/delegate.rb index 83aaf292a5..b54b57d7b7 100644 --- a/lib/delegate.rb +++ b/lib/delegate.rb @@ -1,21 +1,119 @@ -# Delegation class that delegates even methods defined in super class, -# which can not be covered with normal method_missing hack. -# -# Delegator is the abstract delegation class. Need to redefine -# `__getobj__' method in the subclass. SimpleDelegator is the -# concrete subclass for simple delegation. +# = delegate -- Support for the Delegation Pattern # -# Usage: -# foo = Object.new -# foo2 = SimpleDelegator.new(foo) -# foo.hash == foo2.hash # => false +# Documentation by James Edward Gray II and Gavin Sinclair # -# Foo = DelegateClass(Array) +# == Introduction # -# class ExtArraydelegate.rb provides full-class delegation via the +# DelegateClass() method. For single-method delegation via +# def_delegator(), see forwardable.rb. +# +# == Examples +# +# === SimpleDelegator +# +# Here's a simple example that takes advantage of the fact that +# SimpleDelegator's delegation object can be changed at any time. +# +# class Stats +# def initialize +# @source = SimpleDelegator.new([]) +# end +# +# def stats( records ) +# @source.__setobj__(records) +# +# "Elements: #{@source.size}\n" + +# " Non-Nil: #{@source.compact.size}\n" + +# " Unique: #{@source.uniq.size}\n" +# end +# end +# +# s = Stats.new +# puts s.stats(%w{James Edward Gray II}) +# puts +# puts s.stats([1, 2, 3, nil, 4, 5, 1, 2]) +# +# Prints: +# +# Elements: 4 +# Non-Nil: 4 +# Unique: 4 +# +# Elements: 8 +# Non-Nil: 7 +# Unique: 6 +# +# === DelegateClass() +# +# Here's a sample of use from tempfile.rb. +# +# A _Tempfile_ object is really just a _File_ object with a few special rules +# about storage location and/or when the File should be deleted. That makes for +# an almost textbook perfect example of how to use delegation. +# +# class Tempfile < DelegateClass(File) +# # constant and class member data initialization... +# +# def initialize(basename, tmpdir=Dir::tmpdir) +# # build up file path/name in var tmpname... +# +# @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL, 0600) +# +# # ... +# +# super(@tmpfile) +# +# # below this point, all methods of File are supported... +# end +# +# # ... +# end +# +# === Delegator +# +# SimpleDelegator's implementation serves as a nice example here. +# +# class SimpleDelegator < Delegator +# def initialize(obj) +# super # pass obj to Delegator constructor, required +# @_sd_obj = obj # store obj for future use +# end +# +# def __getobj__ +# @_sd_obj # return object we are delegating to, required +# end +# +# def __setobj__(obj) +# @_sd_obj = obj # change delegation object, a feature we're providing +# end +# +# # ... +# end +# +# Delegator is an abstract class used to build delegator pattern objects from +# subclasses. Subclasses should redefine \_\_getobj\_\_. For a concrete +# implementation, see SimpleDelegator. +# class Delegator preserved = ["__id__", "object_id", "__send__", "respond_to?"] instance_methods.each do |m| @@ -23,10 +121,15 @@ class Delegator undef_method m end + # + # Pass in the _obj_ to delegate method calls to. All methods supported by + # _obj_ will be delegated to. + # def initialize(obj) __setobj__(obj) end + # Handles the magic of delegation through \_\_getobj\_\_. def method_missing(m, *args) begin target = self.__getobj__ @@ -40,42 +143,79 @@ class Delegator end end + # + # Checks for a method provided by this the delegate object by fowarding the + # call through \_\_getobj\_\_. + # def respond_to?(m) return true if super return self.__getobj__.respond_to?(m) end + # + # This method must be overridden by subclasses and should return the object + # method calls are being delegated to. + # def __getobj__ raise NotImplementedError, "need to define `__getobj__'" end + # + # This method must be overridden by subclasses and change the object delegate + # to _obj_. + # def __setobj__(obj) raise NotImplementedError, "need to define `__setobj__'" end + # Serialization support for the object returned by \_\_getobj\_\_. def marshal_dump __getobj__ end + # Reinitializes delegation from a serialized object. def marshal_load(obj) __setobj__(obj) end end +# +# A concrete implementation of Delegator, this class provides the means to +# delegate all supported method calls to the object passed into the constructor +# and even to change the object being delegated to at a later time with +# \_\_setobj\_\_ . +# class SimpleDelegator Edward + # names.__setobj__(%w{Gavin Sinclair}) + # puts names[1] # => Sinclair + # def __setobj__(obj) raise ArgumentError, "cannot delegate to self" if self.equal?(obj) @_sd_obj = obj end + # Clone support for the object returned by \_\_getobj\_\_. def clone copy = super copy.__setobj__(__getobj__.clone) copy end + # Duplication support for the object returned by \_\_getobj\_\_. def dup copy = super copy.__setobj__(__getobj__.dup) @@ -83,10 +223,21 @@ class SimpleDelegatorforwardable.rb provides single-method delegation via the +# def_delegator() and def_delegators() methods. For full-class +# delegation via DelegateClass(), see delegate.rb. +# +# == Examples +# +# === Forwardable +# +# Forwardable makes building a new class based on existing work, with a proper +# interface, almost trivial. We want to rely on what has come before obviously, +# but with delegation we can take just the methods we need and even rename them +# as appropriate. In many cases this is preferable to inheritance, which gives +# us the entire old interface, even if much of it isn't needed. +# +# class Queue # extend Forwardable +# +# def initialize +# @q = [ ] # prepare delegate object +# end +# +# # setup prefered interface, enq() and deq()... +# def_delegator :@q, :push, :enq +# def_delegator :@q, :shift, :deq +# +# # support some general Array methods that fit Queues well +# def_delegators :@q, :clear, :first, :push, :shift, :size +# end +# +# q = Queue.new +# q.enq 1, 2, 3, 4, 5 +# q.push 6 +# +# q.shift # => 1 +# while q.size > 0 +# puts q.deq +# end +# +# q.enq "Ruby", "Perl", "Python" +# puts q.first +# q.clear +# puts q.first # -# def_delegators("@out", "printf", "print") -# def_delegators(:@in, :gets) -# def_delegator(:@contents, :[], "content_at") +# Prints: +# +# 2 +# 3 +# 4 +# 5 +# 6 +# Ruby +# nil +# +# Forwardable can be used to setup delegation at the object level as well. +# +# printer = String.new +# printer.extend Forwardable # prepare object for delegation +# printer.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts() +# printer.puts "Howdy!" +# +# Prints: +# +# Howdy! + +# +# The Forwardable module provides delegation of specified +# methods to a designated object, using the methods #def_delegator +# and #def_delegators. +# +# For example, say you have a class RecordCollection which +# contains an array @records. You could provide the lookup method +# #record_number(), which simply calls #[] on the @records +# array, like this: +# +# class RecordCollection +# extends Forwardable +# def_delegator :@records, :[], :record_number +# end +# +# Further, if you wish to provide the methods #size, #<<, and #map, +# all of which delegate to @records, this is how you can do it: +# +# class RecordCollection +# # extends Forwardable, but we did that above +# def_delegators :@records, :size, :<<, :map # end # f = Foo.new # f.printf ... # f.gets # f.content_at(1) # -# g = Goo.new -# g.extend SingleForwardable -# g.def_delegator("@out", :puts) -# g.puts ... -# -# +# Also see the example at forwardable.rb. module Forwardable - - @debug = nil - class< accessor + # delegate [method, method, ...] => accessor + # + def delegate(hash) + hash.each{ |methods, accessor| + methods = methods.to_s unless methods.respond_to?(:each) + methods.each{ |method| + def_instance_delegator(accessor, method) + } + } end + # + # Shortcut for defining multiple delegator methods, but with no + # provision for using a different name. The following two code + # samples have the same effect: + # + # def_delegators :@records, :size, :<<, :map + # + # def_delegator :@records, :size + # def_delegator :@records, :<< + # def_delegator :@records, :map + # def def_instance_delegators(accessor, *methods) - for method in methods + methods.delete("__send__") + methods.delete("__id__") + methods.each{ |method| def_instance_delegator(accessor, method) - end + } end + # + # Defines a method _method_ which delegates to _obj_ (i.e. it calls + # the method of the same name in _obj_). If _new_name_ is + # provided, it is used as the name for the delegate method. + # def def_instance_delegator(accessor, method, ali = method) - accessor = accessor.id2name if accessor.kind_of?(Integer) - method = method.id2name if method.kind_of?(Integer) - ali = ali.id2name if ali.kind_of?(Integer) - - module_eval(<<-EOS, "(__FORWARDABLE__)", 1) + str = %Q{ def #{ali}(*args, &block) - begin - #{accessor}.__send__(:#{method}, *args, &block) - rescue Exception - $@.delete_if{|s| /^\\(__FORWARDABLE__\\):/ =~ s} unless Forwardable::debug - Kernel::raise - end + #{accessor}.send(:#{method}, *args, &block) end - EOS - end - - alias def_delegators def_instance_delegators - alias def_delegator def_instance_delegator -end + } -module SingleForwardable - def def_singleton_delegators(accessor, *methods) - for method in methods - def_singleton_delegator(accessor, method) + # If it's not a class or module, it's an instance + begin + module_eval(str) + rescue + instance_eval(str) end end - def def_singleton_delegator(accessor, method, ali = method) - accessor = accessor.id2name if accessor.kind_of?(Integer) - method = method.id2name if method.kind_of?(Integer) - ali = ali.id2name if ali.kind_of?(Integer) - - instance_eval(<<-EOS, "(__FORWARDABLE__)", 1) - def #{ali}(*args, &block) - begin - #{accessor}.__send__(:#{method}, *args,&block) - rescue Exception - $@.delete_if{|s| /^\\(__FORWARDABLE__\\):/ =~ s} unless Forwardable::debug - Kernel::raise - end - end - EOS - end - - alias def_delegators def_singleton_delegators - alias def_delegator def_singleton_delegator + alias def_delegators def_instance_delegators + alias def_delegator def_instance_delegator end - - - +# compatibility +SingleForwardable = Forwardable diff --git a/lib/optparse.rb b/lib/optparse.rb index f1f207327d..c056ac8d25 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -352,7 +352,7 @@ class OptionParser # : (({block})) # (({yields})) at semi-error condition, instead of raises exception. # - def conv_arg(arg, val = nil) + def conv_arg(arg, val = []) if block if conv val = conv.call(*val) diff --git a/lib/xmlrpc/client.rb b/lib/xmlrpc/client.rb index 074f5014cf..dadd3279f2 100644 --- a/lib/xmlrpc/client.rb +++ b/lib/xmlrpc/client.rb @@ -500,7 +500,7 @@ module XMLRPC def do_rpc(request, async=false) header = { "User-Agent" => USER_AGENT, - "Content-Type" => "text/xml", + "Content-Type" => "text/xml; charset=utf-8", "Content-Length" => request.size.to_s, "Connection" => (async ? "close" : "keep-alive") } diff --git a/lib/xmlrpc/server.rb b/lib/xmlrpc/server.rb index 43041f343f..59a5869408 100644 --- a/lib/xmlrpc/server.rb +++ b/lib/xmlrpc/server.rb @@ -456,7 +456,7 @@ class CGIServer < BasicServer http_error(400, "Bad Request") if data.nil? or data.size != length - http_write(process(data), "Content-type" => "text/xml") + http_write(process(data), "Content-type" => "text/xml; charset=utf-8") } end @@ -531,7 +531,7 @@ class ModRubyServer < BasicServer http_error(400, "Bad Request") if data.nil? or data.size != length - http_write(process(data), 200, "Content-type" => "text/xml") + http_write(process(data), 200, "Content-type" => "text/xml; charset=utf-8") } end @@ -764,7 +764,7 @@ class WEBrickServlet < BasicServer response.status = 200 response['Content-Length'] = resp.size - response['Content-Type'] = "text/xml" + response['Content-Type'] = "text/xml; charset=utf-8" response.body = resp end end diff --git a/lib/yaml.rb b/lib/yaml.rb index 5d409cfae3..cdce7f7635 100644 --- a/lib/yaml.rb +++ b/lib/yaml.rb @@ -11,6 +11,7 @@ require 'yaml/error' require 'yaml/syck' require 'yaml/tag' require 'yaml/stream' +require 'yaml/constants' # == YAML # diff --git a/numeric.c b/numeric.c index 8ab8cd6623..3c8adac113 100644 --- a/numeric.c +++ b/numeric.c @@ -2039,18 +2039,8 @@ fix_quo(VALUE x, VALUE y) } } -/* - * call-seq: - * fix / numeric => numeric_result - * fix.div(numeric) => numeric_result - * - * Performs division: the class of the resulting object depends on - * the class of numeric and on the magnitude of the - * result. - */ - static VALUE -fix_div(VALUE x, VALUE y) +fix_divide(VALUE x, VALUE y, int flo) { if (FIXNUM_P(y)) { long div; @@ -2063,12 +2053,46 @@ fix_div(VALUE x, VALUE y) x = rb_int2big(FIX2LONG(x)); return rb_big_div(x, y); case T_FLOAT: - return rb_float_new((double)FIX2LONG(x) / RFLOAT(y)->value); + if (flo) { + return rb_float_new((double)FIX2LONG(x) / RFLOAT(y)->value); + } + else { + long div = (double)FIX2LONG(x) / RFLOAT(y)->value; + return LONG2NUM(div); + } default: return rb_num_coerce_bin(x, y); } } +/* + * call-seq: + * fix / numeric => numeric_result + * + * Performs division: the class of the resulting object depends on + * the class of numeric and on the magnitude of the + * result. + */ + +static VALUE +fix_div(VALUE x, VALUE y) +{ + return fix_divide(x, y, Qtrue); +} + +/* + * call-seq: + * fix.div(numeric) => numeric_result + * + * Performs integer division: returns integer value. + */ + +static VALUE +fix_idiv(VALUE x, VALUE y) +{ + return fix_divide(x, y, Qfalse); +} + /* * call-seq: * fix % other => Numeric @@ -2824,7 +2848,7 @@ Init_Numeric(void) rb_define_method(rb_cFixnum, "-", fix_minus, 1); rb_define_method(rb_cFixnum, "*", fix_mul, 1); rb_define_method(rb_cFixnum, "/", fix_div, 1); - rb_define_method(rb_cFixnum, "div", fix_div, 1); + rb_define_method(rb_cFixnum, "div", fix_idiv, 1); rb_define_method(rb_cFixnum, "%", fix_mod, 1); rb_define_method(rb_cFixnum, "modulo", fix_mod, 1); rb_define_method(rb_cFixnum, "divmod", fix_divmod, 1); diff --git a/object.c b/object.c index 0952837a0c..694fcf3159 100644 --- a/object.c +++ b/object.c @@ -727,21 +727,6 @@ nil_to_s(VALUE obj) return rb_str_new2(""); } -/* - * call-seq: - * nil.to_a => [] - * - * Always returns an empty array. - * - * nil.to_a #=> [] - */ - -static VALUE -nil_to_a(VALUE obj) -{ - return rb_ary_new2(0); -} - /* * call-seq: * nil.inspect => "nil" @@ -2448,7 +2433,6 @@ Init_Object(void) rb_define_method(rb_cNilClass, "to_i", nil_to_i, 0); rb_define_method(rb_cNilClass, "to_f", nil_to_f, 0); rb_define_method(rb_cNilClass, "to_s", nil_to_s, 0); - rb_define_method(rb_cNilClass, "to_a", nil_to_a, 0); rb_define_method(rb_cNilClass, "inspect", nil_inspect, 0); rb_define_method(rb_cNilClass, "&", false_and, 1); rb_define_method(rb_cNilClass, "|", false_or, 1); diff --git a/pack.c b/pack.c index 99faeca4d1..376d8ce1c9 100644 --- a/pack.c +++ b/pack.c @@ -349,12 +349,12 @@ num2i32(VALUE x) # define EXTEND32(x) #else /* invariant in modulo 1<<31 */ -# define EXTEND32(x) do {if (!natint) {(x) = (I32)(((1<<31)-1-(x))^~(~0<<31))}} while(0) +# define EXTEND32(x) do {if (!natint) {(x) = (I32)(((1<<31)-1-(x))^~(~0<<31));}} while(0) #endif #if SIZEOF_SHORT == SIZE16 # define EXTEND16(x) #else -# define EXTEND16(x) do { if (!natint) {(x) = (short)(((1<<15)-1-(x))^~(~0<<15))}} while(0) +# define EXTEND16(x) do { if (!natint) {(x) = (short)(((1<<15)-1-(x))^~(~0<<15));}} while(0) #endif #ifdef HAVE_LONG_LONG diff --git a/process.c b/process.c index 95e4dc4b46..cf5d824f90 100644 --- a/process.c +++ b/process.c @@ -2756,6 +2756,7 @@ proc_daemon(int argc, VALUE *argv) VALUE nochdir, noclose; int n; + rb_secure(2); rb_scan_args(argc, argv, "02", &nochdir, &noclose); #if defined(HAVE_DAEMON) diff --git a/range.c b/range.c index 866a993c15..4475608eee 100644 --- a/range.c +++ b/range.c @@ -449,9 +449,9 @@ rb_range_beg_len(VALUE range, long *begp, long *lenp, long len, int err) } else { b = rb_check_to_integer(range, "begin"); - if (NIL_P(b)) return Qnil; + if (NIL_P(b)) return Qfalse; e = rb_check_to_integer(range, "end"); - if (NIL_P(e)) return Qnil; + if (NIL_P(e)) return Qfalse; excl = RTEST(rb_funcall(range, rb_intern("exclude_end?"), 0)); } beg = NUM2LONG(b); diff --git a/ruby.h b/ruby.h index 5eb52c5bfa..52e9fdd28c 100644 --- a/ruby.h +++ b/ruby.h @@ -479,9 +479,9 @@ struct RBignum { #define OBJ_FROZEN(x) FL_TEST((x), FL_FREEZE) #define OBJ_FREEZE(x) FL_SET((x), FL_FREEZE) -#define ALLOC_N(type,n) (type*)xmalloc(sizeof(type)*(n)) +#define ALLOC_N(type,n) (type*)xmalloc2((n),sizeof(type)) #define ALLOC(type) (type*)xmalloc(sizeof(type)) -#define REALLOC_N(var,type,n) (var)=(type*)xrealloc((char*)(var),sizeof(type)*(n)) +#define REALLOC_N(var,type,n) (var)=(type*)xrealloc2((char*)(var),(n),sizeof(type)) #define ALLOCA_N(type,n) (type*)alloca(sizeof(type)*(n)) diff --git a/sample/test.rb b/sample/test.rb index 54ac3623b1..d2d7a15b59 100644 --- a/sample/test.rb +++ b/sample/test.rb @@ -54,8 +54,6 @@ a = [*[]]; test_ok(a == []) a = [*[1]]; test_ok(a == [1]) a = [*[1,2]]; test_ok(a == [1,2]) -a = *nil; test_ok(a == nil) -a = *1; test_ok(a == 1) a = *[]; test_ok(a == nil) a = *[1]; test_ok(a == 1) a = *[nil]; test_ok(a == nil) @@ -78,8 +76,6 @@ a, = [*[1]]; test_ok(a == 1) a, = *[1,2]; test_ok(a == 1) a, = [*[1,2]]; test_ok(a == 1) -a, = *nil; test_ok(a == nil) -a, = *1; test_ok(a == 1) a, = *[]; test_ok(a == nil) a, = *[1]; test_ok(a == 1) a, = *[nil]; test_ok(a == nil) @@ -100,8 +96,6 @@ a, = *[*[1,2]]; test_ok(a == 1) *a = [*[1]]; test_ok(a == [[1]]) *a = [*[1,2]]; test_ok(a == [[1,2]]) -*a = *nil; test_ok(a == [nil]) -*a = *1; test_ok(a == [1]) *a = *[]; test_ok(a == []) *a = *[1]; test_ok(a == [1]) *a = *[nil]; test_ok(a == [nil]) @@ -122,8 +116,6 @@ a,b,*c = [*[]]; test_ok([a,b,c] == [nil,nil,[]]) a,b,*c = [*[1]]; test_ok([a,b,c] == [1,nil,[]]) a,b,*c = [*[1,2]]; test_ok([a,b,c] == [1,2,[]]) -a,b,*c = *nil; test_ok([a,b,c] == [nil,nil,[]]) -a,b,*c = *1; test_ok([a,b,c] == [1,nil,[]]) a,b,*c = *[]; test_ok([a,b,c] == [nil,nil,[]]) a,b,*c = *[1]; test_ok([a,b,c] == [1,nil,[]]) a,b,*c = *[nil]; test_ok([a,b,c] == [nil,nil,[]]) @@ -143,8 +135,6 @@ def f; yield [*[]]; end; f {|a| test_ok(a == [])} def f; yield [*[1]]; end; f {|a| test_ok(a == [1])} def f; yield [*[1,2]]; end; f {|a| test_ok(a == [1,2])} -def f; yield *nil; end; f {|a| test_ok(a == nil)} -def f; yield *1; end; f {|a| test_ok(a == 1)} def f; yield *[1]; end; f {|a| test_ok(a == 1)} def f; yield *[nil]; end; f {|a| test_ok(a == nil)} def f; yield *[[]]; end; f {|a| test_ok(a == [])} @@ -161,8 +151,6 @@ def f; yield [*[]]; end; f {|a,| test_ok(a == nil)} def f; yield [*[1]]; end; f {|a,| test_ok(a == 1)} def f; yield [*[1,2]]; end; f {|a,| test_ok(a == 1)} -def f; yield *nil; end; f {|a,| test_ok(a == nil)} -def f; yield *1; end; f {|a,| test_ok(a == 1)} def f; yield *[]; end; f {|a,| test_ok(a == nil)} def f; yield *[1]; end; f {|a,| test_ok(a == 1)} def f; yield *[nil]; end; f {|a,| test_ok(a == nil)} @@ -183,8 +171,6 @@ def f; yield [*[]]; end; f {|*a| test_ok(a == [[]])} def f; yield [*[1]]; end; f {|*a| test_ok(a == [[1]])} def f; yield [*[1,2]]; end; f {|*a| test_ok(a == [[1,2]])} -def f; yield *nil; end; f {|*a| test_ok(a == [nil])} -def f; yield *1; end; f {|*a| test_ok(a == [1])} def f; yield *[]; end; f {|*a| test_ok(a == [])} def f; yield *[1]; end; f {|*a| test_ok(a == [1])} def f; yield *[nil]; end; f {|*a| test_ok(a == [nil])} @@ -204,8 +190,6 @@ def f; yield [*[]]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])} def f; yield [*[1]]; end; f {|a,b,*c| test_ok([a,b,c] == [1,nil,[]])} def f; yield [*[1,2]]; end; f {|a,b,*c| test_ok([a,b,c] == [1,2,[]])} -def f; yield *nil; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])} -def f; yield *1; end; f {|a,b,*c| test_ok([a,b,c] == [1,nil,[]])} def f; yield *[]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])} def f; yield *[1]; end; f {|a,b,*c| test_ok([a,b,c] == [1,nil,[]])} def f; yield *[nil]; end; f {|a,b,*c| test_ok([a,b,c] == [nil,nil,[]])} @@ -225,8 +209,6 @@ def r; return [*[]]; end; a = r(); test_ok(a == []) def r; return [*[1]]; end; a = r(); test_ok(a == [1]) def r; return [*[1,2]]; end; a = r(); test_ok(a == [1,2]) -def r; return *nil; end; a = r(); test_ok(a == nil) -def r; return *1; end; a = r(); test_ok(a == 1) def r; return *[]; end; a = r(); test_ok(a == nil) def r; return *[1]; end; a = r(); test_ok(a == 1) def r; return *[nil]; end; a = r(); test_ok(a == nil) @@ -235,14 +217,7 @@ def r; return *[*[]]; end; a = r(); test_ok(a == nil) def r; return *[*[1]]; end; a = r(); test_ok(a == 1) def r; return *[*[1,2]]; end; a = r(); test_ok(a == [1,2]) -def r; return *nil; end; a = *r(); test_ok(a == nil) -def r; return *1; end; a = *r(); test_ok(a == 1) -def r; return *[]; end; a = *r(); test_ok(a == nil) -def r; return *[1]; end; a = *r(); test_ok(a == 1) -def r; return *[nil]; end; a = *r(); test_ok(a == nil) def r; return *[[]]; end; a = *r(); test_ok(a == nil) -def r; return *[*[]]; end; a = *r(); test_ok(a == nil) -def r; return *[*[1]]; end; a = *r(); test_ok(a == 1) def r; return *[*[1,2]]; end; a = *r(); test_ok(a == [1,2]) def r; return; end; *a = r(); test_ok(a == [nil]) @@ -257,8 +232,6 @@ def r; return [*[]]; end; *a = r(); test_ok(a == [[]]) def r; return [*[1]]; end; *a = r(); test_ok(a == [[1]]) def r; return [*[1,2]]; end; *a = r(); test_ok(a == [[1,2]]) -def r; return *nil; end; *a = r(); test_ok(a == [nil]) -def r; return *1; end; *a = r(); test_ok(a == [1]) def r; return *[]; end; *a = r(); test_ok(a == [nil]) def r; return *[1]; end; *a = r(); test_ok(a == [1]) def r; return *[nil]; end; *a = r(); test_ok(a == [nil]) @@ -268,15 +241,8 @@ def r; return *[*[]]; end; *a = r(); test_ok(a == [nil]) def r; return *[*[1]]; end; *a = r(); test_ok(a == [1]) def r; return *[*[1,2]]; end; *a = r(); test_ok(a == [[1,2]]) -def r; return *nil; end; *a = *r(); test_ok(a == [nil]) -def r; return *1; end; *a = *r(); test_ok(a == [1]) -def r; return *[]; end; *a = *r(); test_ok(a == [nil]) -def r; return *[1]; end; *a = *r(); test_ok(a == [1]) -def r; return *[nil]; end; *a = *r(); test_ok(a == [nil]) def r; return *[[]]; end; *a = *r(); test_ok(a == []) def r; return *[1,2]; end; *a = *r(); test_ok(a == [1,2]) -def r; return *[*[]]; end; *a = *r(); test_ok(a == [nil]) -def r; return *[*[1]]; end; *a = *r(); test_ok(a == [1]) def r; return *[*[1,2]]; end; *a = *r(); test_ok(a == [1,2]) def r; return; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]]) @@ -291,8 +257,6 @@ def r; return [*[]]; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]]) def r; return [*[1]]; end; a,b,*c = r(); test_ok([a,b,c] == [1,nil,[]]) def r; return [*[1,2]]; end; a,b,*c = r(); test_ok([a,b,c] == [1,2,[]]) -def r; return *nil; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]]) -def r; return *1; end; a,b,*c = r(); test_ok([a,b,c] == [1,nil,[]]) def r; return *[]; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]]) def r; return *[1]; end; a,b,*c = r(); test_ok([a,b,c] == [1,nil,[]]) def r; return *[nil]; end; a,b,*c = r(); test_ok([a,b,c] == [nil,nil,[]]) @@ -352,8 +316,6 @@ a = loop do break [*[]]; end; test_ok(a == []) a = loop do break [*[1]]; end; test_ok(a == [1]) a = loop do break [*[1,2]]; end; test_ok(a == [1,2]) -a = loop do break *nil; end; test_ok(a == nil) -a = loop do break *1; end; test_ok(a == 1) a = loop do break *[]; end; test_ok(a == nil) a = loop do break *[1]; end; test_ok(a == 1) a = loop do break *[nil]; end; test_ok(a == nil) @@ -374,8 +336,6 @@ a = loop do break *[*[1,2]]; end; test_ok(a == [1,2]) *a = loop do break [*[1]]; end; test_ok(a == [[1]]) *a = loop do break [*[1,2]]; end; test_ok(a == [[1,2]]) -*a = loop do break *nil; end; test_ok(a == [nil]) -*a = loop do break *1; end; test_ok(a == [1]) *a = loop do break *[]; end; test_ok(a == [nil]) *a = loop do break *[1]; end; test_ok(a == [1]) *a = loop do break *[nil]; end; test_ok(a == [nil]) @@ -385,15 +345,8 @@ a = loop do break *[*[1,2]]; end; test_ok(a == [1,2]) *a = loop do break *[*[1]]; end; test_ok(a == [1]) *a = loop do break *[*[1,2]]; end; test_ok(a == [[1,2]]) -*a = *loop do break *nil; end; test_ok(a == [nil]) -*a = *loop do break *1; end; test_ok(a == [1]) -*a = *loop do break *[]; end; test_ok(a == [nil]) -*a = *loop do break *[1]; end; test_ok(a == [1]) -*a = *loop do break *[nil]; end; test_ok(a == [nil]) *a = *loop do break *[[]]; end; test_ok(a == []) *a = *loop do break *[1,2]; end; test_ok(a == [1,2]) -*a = *loop do break *[*[]]; end; test_ok(a == [nil]) -*a = *loop do break *[*[1]]; end; test_ok(a == [1]) *a = *loop do break *[*[1,2]]; end; test_ok(a == [1,2]) a,b,*c = loop do break; end; test_ok([a,b,c] == [nil,nil,[]]) @@ -408,8 +361,6 @@ a,b,*c = loop do break [*[]]; end; test_ok([a,b,c] == [nil,nil,[]]) a,b,*c = loop do break [*[1]]; end; test_ok([a,b,c] == [1,nil,[]]) a,b,*c = loop do break [*[1,2]]; end; test_ok([a,b,c] == [1,2,[]]) -a,b,*c = loop do break *nil; end; test_ok([a,b,c] == [nil,nil,[]]) -a,b,*c = loop do break *1; end; test_ok([a,b,c] == [1,nil,[]]) a,b,*c = loop do break *[]; end; test_ok([a,b,c] == [nil,nil,[]]) a,b,*c = loop do break *[1]; end; test_ok([a,b,c] == [1,nil,[]]) a,b,*c = loop do break *[nil]; end; test_ok([a,b,c] == [nil,nil,[]]) @@ -431,8 +382,6 @@ r([]){next [*[]]} r([1]){next [*[1]]} r([1,2]){next [*[1,2]]} -r(nil){next *nil} -r(1){next *1} r(nil){next *[]} r(1){next *[1]} r(nil){next *[nil]} @@ -455,15 +404,8 @@ r([[1]]){next [*[1]]} r([[1,2]]){next [*[1,2]]} def r(val); *a = *yield(); test_ok(a == val, 2); end -r([nil]){next *nil} -r([1]){next *1} -r([nil]){next *[]} -r([1]){next *[1]} -r([nil]){next *[nil]} r([]){next *[[]]} r([1,2]){next *[1,2]} -r([nil]){next *[*[]]} -r([1]){next *[*[1]]} r([1,2]){next *[*[1,2]]} def r(val); a,b,*c = yield(); test_ok([a,b,c] == val, 2); end @@ -480,15 +422,8 @@ r([1,nil,[]]){next [*[1]]} r([1,2,[]]){next [*[1,2]]} def r(val); a,b,*c = *yield(); test_ok([a,b,c] == val, 2); end -r([nil,nil,[]]){next *nil} -r([1,nil,[]]){next *1} -r([nil,nil,[]]){next *[]} -r([1,nil,[]]){next *[1]} -r([nil,nil,[]]){next *[nil]} r([nil,nil,[]]){next *[[]]} r([1,2,[]]){next *[1,2]} -r([nil,nil,[]]){next *[*[]]} -r([1,nil,[]]){next *[*[1]]} r([1,2,[]]){next *[*[1,2]]} test_check "condition" @@ -1025,11 +960,11 @@ test_ok(IterTest.new(nil).method(:f).to_proc.call([m]) == [m]) IterTest.new([0]).each0 {|x| test_ok(x == 0)} IterTest.new([1]).each1 {|x| test_ok(x == 1)} IterTest.new([2]).each2 {|x| test_ok(x == [2])} -IterTest.new([3]).each3 {|x| test_ok(x == 3)} +#IterTest.new([3]).each3 {|x| test_ok(x == 3)} IterTest.new([4]).each4 {|x| test_ok(x == 4)} IterTest.new([5]).each5 {|x| test_ok(x == 5)} IterTest.new([6]).each6 {|x| test_ok(x == [6])} -IterTest.new([7]).each7 {|x| test_ok(x == 7)} +#IterTest.new([7]).each7 {|x| test_ok(x == 7)} IterTest.new([8]).each8 {|x| test_ok(x == 8)} IterTest.new([[0]]).each0 {|x| test_ok(x == [0])} diff --git a/test/dbm/test_dbm.rb b/test/dbm/test_dbm.rb index 0a604e50e5..cec2b20f91 100644 --- a/test/dbm/test_dbm.rb +++ b/test/dbm/test_dbm.rb @@ -86,14 +86,6 @@ if defined? DBM assert_equal(DBM.open("tmptest_dbm") { :foo }, :foo) end - def test_s_open_error - assert_instance_of(DBM, dbm = DBM.open("tmptest_dbm", 0)) - assert_raise(Errno::EACCES, "NEVER MIND IF YOU USE Berkeley DB3") { - DBM.open("tmptest_dbm", 0) - } - dbm.close - end - def test_close assert_instance_of(DBM, dbm = DBM.open("tmptest_dbm")) assert_nil(dbm.close) diff --git a/test/xmlrpc/webrick_testing.rb b/test/xmlrpc/webrick_testing.rb index a6830cece1..eb054a77e7 100644 --- a/test/xmlrpc/webrick_testing.rb +++ b/test/xmlrpc/webrick_testing.rb @@ -23,14 +23,14 @@ module WEBrick_Testing } Timeout.timeout(5) { - nil until @__started # wait until the server is ready + Thread.pass until @__started # wait until the server is ready } end def stop_server Timeout.timeout(5) { @__server.shutdown - nil while @__started # wait until the server is down + Thread.pass while @__started # wait until the server is down } @__server = nil end -- cgit v1.2.3