summaryrefslogtreecommitdiff
path: root/io.c
diff options
context:
space:
mode:
Diffstat (limited to 'io.c')
-rw-r--r--io.c8
1 files changed, 7 insertions, 1 deletions
diff --git a/io.c b/io.c
index 8d8bfe172b..12ea578dde 100644
--- a/io.c
+++ b/io.c
@@ -320,9 +320,15 @@ io_ungetc(VALUE str, rb_io_t *fptr)
fptr->rbuf_capa = 8192;
fptr->rbuf = ALLOC_N(char, fptr->rbuf_capa);
}
- if (fptr->rbuf_off < len) {
+ if (fptr->rbuf_capa < len + fptr->rbuf_len) {
rb_raise(rb_eIOError, "ungetc failed");
}
+ if (fptr->rbuf_off < len) {
+ MEMMOVE(fptr->rbuf+fptr->rbuf_capa-fptr->rbuf_len,
+ fptr->rbuf+fptr->rbuf_off,
+ char, fptr->rbuf_len);
+ fptr->rbuf_off = fptr->rbuf_capa-fptr->rbuf_len;
+ }
fptr->rbuf_off-=len;
fptr->rbuf_len+=len;
MEMMOVE(fptr->rbuf+fptr->rbuf_off, RSTRING_PTR(str), char, len);