* string.c (rb_str_clear): avoid revealing NULL pointer.

[ruby-dev:24766]

* string.c (str_gsub): add paranoid check. [ruby-dev:24827]

* string.c (str_mod_check): check frozen status as well.
  [ruby-dev:24801]

* lib/mathn.rb (Integer::gcd2): faster implementation by
  <erlercw@siu.edu>. [ruby-talk:120232]

* ext/gdbm/gdbm.c (fgdbm_store): StringValue() may alter string
  pointer.  [ruby-dev:24783]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7265 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2004-11-14 15:41:40 +00:00
Родитель 8f1c92af4f
Коммит 0f36ecebe0
8 изменённых файлов: 82 добавлений и 47 удалений

Просмотреть файл

@ -1,3 +1,13 @@
Mon Nov 15 00:33:40 2004 Yukihiro Matsumoto <matz@ruby-lang.org>
* string.c (rb_str_clear): avoid revealing NULL pointer.
[ruby-dev:24766]
* string.c (str_gsub): add paranoid check. [ruby-dev:24827]
* string.c (str_mod_check): check frozen status as well.
[ruby-dev:24801]
Sun Nov 14 18:59:03 2004 Tanaka Akira <akr@m17n.org> Sun Nov 14 18:59:03 2004 Tanaka Akira <akr@m17n.org>
* process.c (proc_getrlimit): new function for Process.getrlimit. * process.c (proc_getrlimit): new function for Process.getrlimit.
@ -13,6 +23,11 @@ Sun Nov 14 13:27:03 2004 Nobuyoshi Nakada <nobu@ruby-lang.org>
* lib/pp.rb (PP#object_address_group): remove odd number of 'f' * lib/pp.rb (PP#object_address_group): remove odd number of 'f'
prefixed to negative address. prefixed to negative address.
Sun Nov 14 10:48:21 2004 Yukihiro Matsumoto <matz@ruby-lang.org>
* lib/mathn.rb (Integer::gcd2): faster implementation by
<erlercw@siu.edu>. [ruby-talk:120232]
Sun Nov 14 08:46:33 2004 NAKAMURA, Hiroshi <nakahiro@sarion.co.jp> Sun Nov 14 08:46:33 2004 NAKAMURA, Hiroshi <nakahiro@sarion.co.jp>
* test/logger/test_logger.rb: Logger just expects * test/logger/test_logger.rb: Logger just expects
@ -38,6 +53,11 @@ Fri Nov 12 14:08:01 2004 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp>
* ext/tk/lib/tk.rb: ditto * ext/tk/lib/tk.rb: ditto
Fri Nov 12 00:31:05 2004 Yukihiro Matsumoto <matz@ruby-lang.org>
* ext/gdbm/gdbm.c (fgdbm_store): StringValue() may alter string
pointer. [ruby-dev:24783]
Thu Nov 11 17:58:19 2004 Yukihiro Matsumoto <matz@ruby-lang.org> Thu Nov 11 17:58:19 2004 Yukihiro Matsumoto <matz@ruby-lang.org>
* dir.c (rb_glob): should have called rb_glob_caller(). * dir.c (rb_glob): should have called rb_glob_caller().

4
eval.c
Просмотреть файл

@ -4686,6 +4686,10 @@ rb_yield_0(val, self, klass, flags, avalue)
POP_TAG(); POP_TAG();
if (state) goto pop_state; if (state) goto pop_state;
} }
else if (lambda && RARRAY(val)->len != 0) {
rb_raise(rb_eArgError, "wrong number of arguments (%ld for 0)",
RARRAY(val)->len);
}
if (!node) { if (!node) {
state = 0; state = 0;
goto pop_state; goto pop_state;

Просмотреть файл

@ -462,11 +462,11 @@ fdbm_store(obj, keystr, valstr)
fdbm_modify(obj); fdbm_modify(obj);
keystr = rb_obj_as_string(keystr); keystr = rb_obj_as_string(keystr);
valstr = rb_obj_as_string(valstr);
key.dptr = RSTRING(keystr)->ptr; key.dptr = RSTRING(keystr)->ptr;
key.dsize = RSTRING(keystr)->len; key.dsize = RSTRING(keystr)->len;
valstr = rb_obj_as_string(valstr);
val.dptr = RSTRING(valstr)->ptr; val.dptr = RSTRING(valstr)->ptr;
val.dsize = RSTRING(valstr)->len; val.dsize = RSTRING(valstr)->len;

Просмотреть файл

@ -566,10 +566,11 @@ fgdbm_store(obj, keystr, valstr)
rb_gdbm_modify(obj); rb_gdbm_modify(obj);
StringValue(keystr); StringValue(keystr);
StringValue(valstr);
key.dptr = RSTRING(keystr)->ptr; key.dptr = RSTRING(keystr)->ptr;
key.dsize = RSTRING(keystr)->len; key.dsize = RSTRING(keystr)->len;
StringValue(valstr);
val.dptr = RSTRING(valstr)->ptr; val.dptr = RSTRING(valstr)->ptr;
val.dsize = RSTRING(valstr)->len; val.dsize = RSTRING(valstr)->len;

Просмотреть файл

@ -445,11 +445,11 @@ fsdbm_store(obj, keystr, valstr)
fdbm_modify(obj); fdbm_modify(obj);
StringValue(keystr); StringValue(keystr);
StringValue(valstr);
key.dptr = RSTRING(keystr)->ptr; key.dptr = RSTRING(keystr)->ptr;
key.dsize = RSTRING(keystr)->len; key.dsize = RSTRING(keystr)->len;
StringValue(valstr);
val.dptr = RSTRING(valstr)->ptr; val.dptr = RSTRING(valstr)->ptr;
val.dsize = RSTRING(valstr)->len; val.dsize = RSTRING(valstr)->len;

Просмотреть файл

@ -17,24 +17,13 @@ require "matrix.rb"
class Integer class Integer
remove_method(:gcd2) remove_method(:gcd2)
def gcd2(int) def gcd2(other)
a = self.abs min = self.abs
b = int.abs max = other.abs
a, b = b, a if a < b min, max = max % min, min while min > 0
max
pd_a = a.prime_division
pd_b = b.prime_division
gcd = 1
for pair in pd_a
as = pd_b.assoc(pair[0])
if as
gcd *= as[0] ** [as[1], pair[1]].min
end
end
return gcd
end end
def Integer.from_prime_division(pd) def Integer.from_prime_division(pd)
value = 1 value = 1
for prime, index in pd for prime, index in pd
@ -68,34 +57,54 @@ end
class Prime class Prime
include Enumerable include Enumerable
# These are included as class variables to cache them for later uses. If memory
# usage is a problem, they can be put in Prime#initialize as instance variables.
def initialize # There must be no primes between @@primes[-1] and @@next_to_check.
@seed = 1 @@primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
@primes = [] # @@next_to_check % 6 must be 1.
@counts = [] @@next_to_check = 103 # @@primes[-1] - @@primes[-1] % 6 + 7
@@ulticheck_index = 3 # @@primes.index(@@primes.reverse.find {|n|
# n < Math.sqrt(@@next_to_check) })
@@ulticheck_next_squared = 121 # @@primes[@@ulticheck_index + 1] ** 2
class << self
# Return the prime cache.
def cache
return @@primes
end
alias primes cache
alias primes_so_far cache
end end
def initialize
@index = -1
end
# Return primes given by this instance so far.
def primes
return @@primes[0, @index + 1]
end
alias primes_so_far primes
def succ def succ
i = -1 @index += 1
size = @primes.size while @index >= @@primes.length
while i < size # Only check for prime factors up to the square root of the potential primes,
if i == -1 # but without the performance hit of an actual square root calculation.
@seed += 1 if @@next_to_check + 4 > @@ulticheck_next_squared
i += 1 @@ulticheck_index += 1
else @@ulticheck_next_squared = @@primes.at(@@ulticheck_index + 1) ** 2
while @seed > @counts[i]
@counts[i] += @primes[i]
end
if @seed != @counts[i]
i += 1
else
i = -1
end
end end
# Only check numbers congruent to one and five, modulo six. All others
# are divisible by two or three. This also allows us to skip checking against
# two and three.
@@primes.push @@next_to_check if @@primes[2..@@ulticheck_index].find {|prime| @@next_to_check % prime == 0 }.nil?
@@next_to_check += 4
@@primes.push @@next_to_check if @@primes[2..@@ulticheck_index].find {|prime| @@next_to_check % prime == 0 }.nil?
@@next_to_check += 2
end end
@primes.push @seed return @@primes[@index]
@counts.push @seed + @seed
return @seed
end end
alias next succ alias next succ

2
pack.c
Просмотреть файл

@ -253,11 +253,11 @@ endian()
#undef ntohl #undef ntohl
#undef htons #undef htons
#undef htonl #undef htonl
#endif
#define ntohs(x) swaps(x) #define ntohs(x) swaps(x)
#define ntohl(x) swapl(x) #define ntohl(x) swapl(x)
#define htons(x) swaps(x) #define htons(x) swaps(x)
#define htonl(x) swapl(x) #define htonl(x) swapl(x)
#endif
#define ntohf(x) swapf(x) #define ntohf(x) swapf(x)
#define ntohd(x) swapd(x) #define ntohd(x) swapd(x)
#define htonf(x) swapf(x) #define htonf(x) swapf(x)

Просмотреть файл

@ -45,7 +45,7 @@ str_mod_check(s, p, len)
char *p; char *p;
long len; long len;
{ {
if (RSTRING(s)->ptr != p || RSTRING(s)->len != len) { if (RSTRING(s)->ptr != p || RSTRING(s)->len != len || OBJ_FROZEN(s)) {
rb_raise(rb_eRuntimeError, "string modified"); rb_raise(rb_eRuntimeError, "string modified");
} }
} }
@ -1962,9 +1962,7 @@ rb_str_sub_bang(argc, argv, str)
rb_match_busy(match); rb_match_busy(match);
repl = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match))); repl = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match)));
if (RSTRING(str)->ptr != p || RSTRING(str)->len != len) { str_mod_check(str, p, len);
rb_raise(rb_eRuntimeError, "string modified");
}
rb_backref_set(match); rb_backref_set(match);
} }
else { else {
@ -2084,6 +2082,9 @@ str_gsub(argc, argv, str, bang)
rb_match_busy(match); rb_match_busy(match);
val = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match))); val = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match)));
str_mod_check(str, sp, slen); str_mod_check(str, sp, slen);
if (val == dest) { /* paranoid chack [ruby-dev:24827] */
rb_raise(rb_eRuntimeError, "block should not cheat");
}
rb_backref_set(match); rb_backref_set(match);
} }
else { else {