mirror of
				https://github.com/libssh2/libssh2.git
				synced 2025-11-03 22:13:11 +03:00 
			
		
		
		
	fix several return -1 to return a proper error code
On many places in the code there have been laziness return -1 statements lying around that should be fixed to return sensible error codes. Here's a take at fixing a few offenders.
This commit is contained in:
		@@ -1738,7 +1738,7 @@ static ssize_t channel_read(LIBSSH2_CHANNEL *channel, int stream_id,
 | 
				
			|||||||
        rc = _libssh2_transport_read(session);
 | 
					        rc = _libssh2_transport_read(session);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if ((rc < 0) && (rc != PACKET_EAGAIN))
 | 
					    if ((rc < 0) && (rc != PACKET_EAGAIN))
 | 
				
			||||||
        return -1;
 | 
					        return rc;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /*
 | 
					    /*
 | 
				
			||||||
     * =============================== NOTE ===============================
 | 
					     * =============================== NOTE ===============================
 | 
				
			||||||
@@ -1950,6 +1950,8 @@ _libssh2_channel_packet_data_len(LIBSSH2_CHANNEL * channel, int stream_id)
 | 
				
			|||||||
 * Send data to a channel. Note that if this returns EAGAIN or simply didn't
 | 
					 * Send data to a channel. Note that if this returns EAGAIN or simply didn't
 | 
				
			||||||
 * send the entire packet, the caller must call this function again with the
 | 
					 * send the entire packet, the caller must call this function again with the
 | 
				
			||||||
 * SAME input arguments.
 | 
					 * SAME input arguments.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * If it returns a negative number, that is the error code!
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
ssize_t
 | 
					ssize_t
 | 
				
			||||||
_libssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id,
 | 
					_libssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id,
 | 
				
			||||||
@@ -1970,7 +1972,7 @@ _libssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id,
 | 
				
			|||||||
        if (channel->local.close) {
 | 
					        if (channel->local.close) {
 | 
				
			||||||
            libssh2_error(session, LIBSSH2_ERROR_CHANNEL_CLOSED,
 | 
					            libssh2_error(session, LIBSSH2_ERROR_CHANNEL_CLOSED,
 | 
				
			||||||
                          "We've already closed this channel", 0);
 | 
					                          "We've already closed this channel", 0);
 | 
				
			||||||
            return -1;
 | 
					            return LIBSSH2_ERROR_CHANNEL_CLOSED;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (channel->local.eof) {
 | 
					        if (channel->local.eof) {
 | 
				
			||||||
@@ -1988,7 +1990,7 @@ _libssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id,
 | 
				
			|||||||
            libssh2_error(session, LIBSSH2_ERROR_ALLOC,
 | 
					            libssh2_error(session, LIBSSH2_ERROR_ALLOC,
 | 
				
			||||||
                          "Unable to allocte space for data transmission packet",
 | 
					                          "Unable to allocte space for data transmission packet",
 | 
				
			||||||
                          0);
 | 
					                          0);
 | 
				
			||||||
            return -1;
 | 
					            return LIBSSH2_ERROR_ALLOC;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        channel->write_state = libssh2_NB_state_allocated;
 | 
					        channel->write_state = libssh2_NB_state_allocated;
 | 
				
			||||||
@@ -2074,7 +2076,7 @@ _libssh2_channel_write(LIBSSH2_CHANNEL *channel, int stream_id,
 | 
				
			|||||||
                LIBSSH2_FREE(session, channel->write_packet);
 | 
					                LIBSSH2_FREE(session, channel->write_packet);
 | 
				
			||||||
                channel->write_packet = NULL;
 | 
					                channel->write_packet = NULL;
 | 
				
			||||||
                channel->write_state = libssh2_NB_state_idle;
 | 
					                channel->write_state = libssh2_NB_state_idle;
 | 
				
			||||||
                return -1;
 | 
					                return LIBSSH2_ERROR_SOCKET_SEND;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            /* Shrink local window size */
 | 
					            /* Shrink local window size */
 | 
				
			||||||
            channel->local.window_size -= channel->write_bufwrite;
 | 
					            channel->local.window_size -= channel->write_bufwrite;
 | 
				
			||||||
@@ -2134,7 +2136,7 @@ static int channel_send_eof(LIBSSH2_CHANNEL *channel)
 | 
				
			|||||||
    else if (rc) {
 | 
					    else if (rc) {
 | 
				
			||||||
        libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
 | 
					        libssh2_error(session, LIBSSH2_ERROR_SOCKET_SEND,
 | 
				
			||||||
                      "Unable to send EOF on channel", 0);
 | 
					                      "Unable to send EOF on channel", 0);
 | 
				
			||||||
        return -1;
 | 
					        return LIBSSH2_ERROR_SOCKET_SEND;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    channel->local.eof = 1;
 | 
					    channel->local.eof = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										17
									
								
								src/sftp.c
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								src/sftp.c
									
									
									
									
									
								
							@@ -1403,7 +1403,8 @@ libssh2_sftp_readdir_ex(LIBSSH2_SFTP_HANDLE *hnd, char *buffer,
 | 
				
			|||||||
/*
 | 
					/*
 | 
				
			||||||
 * sftp_write
 | 
					 * sftp_write
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * Write data to an SFTP handle
 | 
					 * Write data to an SFTP handle. Returns the number of bytes written, or
 | 
				
			||||||
 | 
					 * a negative error code.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
 | 
					static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
 | 
				
			||||||
                          size_t count)
 | 
					                          size_t count)
 | 
				
			||||||
@@ -1434,7 +1435,7 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
 | 
				
			|||||||
        if (!sftp->write_packet) {
 | 
					        if (!sftp->write_packet) {
 | 
				
			||||||
            libssh2_error(session, LIBSSH2_ERROR_ALLOC,
 | 
					            libssh2_error(session, LIBSSH2_ERROR_ALLOC,
 | 
				
			||||||
                          "Unable to allocate memory for FXP_WRITE", 0);
 | 
					                          "Unable to allocate memory for FXP_WRITE", 0);
 | 
				
			||||||
            return -1;
 | 
					            return LIBSSH2_ERROR_ALLOC;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        _libssh2_htonu32(s, packet_len - 4);
 | 
					        _libssh2_htonu32(s, packet_len - 4);
 | 
				
			||||||
        s += 4;
 | 
					        s += 4;
 | 
				
			||||||
@@ -1467,9 +1468,8 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
 | 
				
			|||||||
            return rc;
 | 
					            return rc;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        else if(0 == rc) {
 | 
					        else if(0 == rc) {
 | 
				
			||||||
            /* an actual error */
 | 
					            /* nothing sent is an error */
 | 
				
			||||||
            fprintf(stderr, "WEIRDNESS\n");
 | 
					            return LIBSSH2_ERROR_SOCKET_SEND;
 | 
				
			||||||
            return -1;
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        else if (packet_len != rc) {
 | 
					        else if (packet_len != rc) {
 | 
				
			||||||
            return rc;
 | 
					            return rc;
 | 
				
			||||||
@@ -1483,11 +1483,12 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
 | 
				
			|||||||
                             sftp->write_request_id, &data, &data_len);
 | 
					                             sftp->write_request_id, &data, &data_len);
 | 
				
			||||||
    if (rc == PACKET_EAGAIN) {
 | 
					    if (rc == PACKET_EAGAIN) {
 | 
				
			||||||
        return PACKET_EAGAIN;
 | 
					        return PACKET_EAGAIN;
 | 
				
			||||||
    } else if (rc) {
 | 
					    }
 | 
				
			||||||
 | 
					    else if (rc) {
 | 
				
			||||||
        libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
 | 
					        libssh2_error(session, LIBSSH2_ERROR_SOCKET_TIMEOUT,
 | 
				
			||||||
                      "Timeout waiting for status message", 0);
 | 
					                      "Timeout waiting for status message", 0);
 | 
				
			||||||
        sftp->write_state = libssh2_NB_state_idle;
 | 
					        sftp->write_state = libssh2_NB_state_idle;
 | 
				
			||||||
        return -1;
 | 
					        return rc;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    sftp->write_state = libssh2_NB_state_idle;
 | 
					    sftp->write_state = libssh2_NB_state_idle;
 | 
				
			||||||
@@ -1503,7 +1504,7 @@ static ssize_t sftp_write(LIBSSH2_SFTP_HANDLE *handle, const char *buffer,
 | 
				
			|||||||
                  0);
 | 
					                  0);
 | 
				
			||||||
    sftp->last_errno = retcode;
 | 
					    sftp->last_errno = retcode;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return -1;
 | 
					    return LIBSSH2_ERROR_SFTP_PROTOCOL;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* libssh2_sftp_write
 | 
					/* libssh2_sftp_write
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user