Permalink
Browse files

Merge pull request #7 from loganb/master

Do not trigger a BUG when reading zero bytes from a handle that's reached EOF
  • Loading branch information...
2 parents ab7ae7f + 6fb2bb7 commit 1ccca986b5778367c02ee9f4e8174fb5ad418151 @tarcieri committed Feb 13, 2012
Showing with 19 additions and 4 deletions.
  1. +8 −4 ext/iobuffer.c
  2. +11 −0 spec/buffer_spec.rb
View
@@ -353,6 +353,7 @@ static VALUE IO_Buffer_to_str(VALUE self) {
*/
static VALUE IO_Buffer_read_from(VALUE self, VALUE io) {
struct buffer *buf;
+ int ret;
#if HAVE_RB_IO_T
rb_io_t *fptr;
#else
@@ -362,8 +363,9 @@ static VALUE IO_Buffer_read_from(VALUE self, VALUE io) {
Data_Get_Struct(self, struct buffer, buf);
GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
rb_io_set_nonblock(fptr);
-
- return INT2NUM(buffer_read_from(buf, FPTR_TO_FD(fptr)));
+
+ ret = buffer_read_from(buf, FPTR_TO_FD(fptr));
+ return ret == -1 ? Qnil : INT2NUM(ret);
}
/**
@@ -690,10 +692,12 @@ static int buffer_read_from(struct buffer *buf, int fd)
nbytes = buf->node_size - buf->tail->end;
bytes_read = read(fd, buf->tail->data + buf->tail->end, nbytes);
- if(bytes_read < 1) {
+ if(bytes_read == 0) {
+ return -1; //When the file reaches EOF
+ } else if(bytes_read < 0) {
if(errno != EAGAIN)
rb_sys_fail("read");
-
+
return total_bytes_read;
}
View
@@ -142,6 +142,17 @@
str.should == "bar"
@buffer.size.should == 0
end
+
+ it "Returns nil when reading from a filehandle at EOF" do
+ (rp, wp) = File.pipe
+
+ wp.write("Foo")
+ wp.flush
+ @buffer.read_from(rp).should == 3
+ @buffer.read_from(rp).should == 0
+ wp.close
+ @buffer.read_from(rp).should == nil
+ end
#######
private

0 comments on commit 1ccca98

Please sign in to comment.