From c2375dbfa42d1eabb8e7dbd50c3ae4dfac491ba6 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 16 Sep 2009 23:46:30 +0200 Subject: [PATCH] libssh2_channel_read_ex() must return 0 when closed If the channel is already at EOF or even closed at the end of the libssh2_channel_read_ex() function and there's no data to return, we need to signal that back. We may have gotten that info while draining the incoming transport layer until EAGAIN so we must not be fooled by that return code. --- src/channel.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/channel.c b/src/channel.c index d584bacc..0a840da0 100644 --- a/src/channel.c +++ b/src/channel.c @@ -1828,8 +1828,15 @@ ssize_t _libssh2_channel_read(LIBSSH2_CHANNEL *channel, int stream_id, if (!bytes_read) { channel->read_state = libssh2_NB_state_idle; - /* if the transport layer said EAGAIN then we say so as well */ - return (rc == PACKET_EAGAIN)?rc:0; + /* If the channel is already at EOF or even closed, we need to signal + that back. We may have gotten that info while draining the incoming + transport layer until EAGAIN so we must not be fooled by that + return code. */ + if(channel->remote.eof || channel->remote.close) + return 0; + else + /* if the transport layer said EAGAIN then we say so as well */ + return (rc == PACKET_EAGAIN)?rc:0; } else /* make sure we remain in the created state to focus on emptying the