channel: Detect bad usage of libssh2_channel_process_startup
A common novice programmer error (at least among those using the
wrapping Perl module Net::SSH2), is to try to reuse channels.
This patchs detects that incorrect usage and fails with a
LIBSSH2_ERROR_BAD_USE error instead of hanging.
Signed-off-by: Salvador Fandino <sfandino-/E1597aS9LQAvxtiuMwx3w@public.gmane.org>
diff --git a/src/channel.c b/src/channel.c
index cb0d818..32d914d 100644
--- a/src/channel.c
+++ b/src/channel.c
@@ -1252,6 +1252,11 @@
{ SSH_MSG_CHANNEL_SUCCESS, SSH_MSG_CHANNEL_FAILURE, 0 };
int rc;
+ if (channel->process_state == libssh2_NB_state_end) {
+ return _libssh2_error(session, LIBSSH2_ERROR_BAD_USE,
+ "Channel can not be reused");
+ }
+
if (channel->process_state == libssh2_NB_state_idle) {
/* 10 = packet_type(1) + channel(4) + request_len(4) + want_reply(1) */
channel->process_packet_len = request_len + 10;
@@ -1298,7 +1303,7 @@
else if (rc) {
LIBSSH2_FREE(session, channel->process_packet);
channel->process_packet = NULL;
- channel->process_state = libssh2_NB_state_idle;
+ channel->process_state = libssh2_NB_state_end;
return _libssh2_error(session, rc,
"Unable to send channel request");
}
@@ -1320,14 +1325,14 @@
if (rc == LIBSSH2_ERROR_EAGAIN) {
return rc;
} else if (rc) {
- channel->process_state = libssh2_NB_state_idle;
+ channel->process_state = libssh2_NB_state_end;
return _libssh2_error(session, rc,
"Failed waiting for channel success");
}
code = data[0];
LIBSSH2_FREE(session, data);
- channel->process_state = libssh2_NB_state_idle;
+ channel->process_state = libssh2_NB_state_end;
if (code == SSH_MSG_CHANNEL_SUCCESS)
return 0;
diff --git a/src/libssh2_priv.h b/src/libssh2_priv.h
index 0ef6e1a..78d4ced 100644
--- a/src/libssh2_priv.h
+++ b/src/libssh2_priv.h
@@ -216,7 +216,8 @@
libssh2_NB_state_jump2,
libssh2_NB_state_jump3,
libssh2_NB_state_jump4,
- libssh2_NB_state_jump5
+ libssh2_NB_state_jump5,
+ libssh2_NB_state_end
} libssh2_nonblocking_states;
typedef struct packet_require_state_t