* io.c (io_ungetc): reallocate internal buffer if pushing data

excess capacity.  [ruby-dev:31650]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13512 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2007-09-25 12:45:03 +00:00
parent a50ef07e16
commit 2f6f2bfb02
3 changed files with 15 additions and 9 deletions

View File

@ -1,3 +1,8 @@
Tue Sep 25 15:11:32 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* io.c (io_ungetc): reallocate internal buffer if pushing data
excess capacity. [ruby-dev:31650]
Tue Sep 25 13:43:03 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* eval_method.ci (remove_method): should not remove undef place

View File

@ -6,11 +6,15 @@
assert_equal 'ok', %q{
begin
r, w = IO.pipe
w.write "foo"
w.close
# assert_raise(IOError, "[ruby-dev:31650]") { 20000.times { r.ungetc "a" } }
r.getc
20000.times { r.ungetc "a" }
rescue IOError
:ok
data = r.read
if data.size == 20002 && data[-5..-1] == "aaaoo"
:ok
end
ensure
r.close
end

11
io.c
View File

@ -296,20 +296,17 @@ io_ungetc(VALUE str, rb_io_t *fptr)
fptr->rbuf_capa = 8192;
fptr->rbuf = ALLOC_N(char, fptr->rbuf_capa);
}
if (fptr->rbuf_off == 0) {
if (fptr->rbuf_len) {
MEMMOVE(fptr->rbuf+len, fptr->rbuf, char, fptr->rbuf_len);
}
fptr->rbuf_off = len;
}
else if (fptr->rbuf_off < len) {
if (fptr->rbuf_off < len) {
int capa = fptr->rbuf_len + len;
char *buf = ALLOC_N(char, capa);
if (fptr->rbuf_len) {
MEMMOVE(buf+len, fptr->rbuf+fptr->rbuf_off, char, fptr->rbuf_len);
}
fptr->rbuf_capa = capa;
fptr->rbuf_off = len;
free(fptr->rbuf);
fptr->rbuf = buf;
}
fptr->rbuf_off-=len;
fptr->rbuf_len+=len;