Subject: Re: proper buffer size for libssh2_sftp_read()

Re: proper buffer size for libssh2_sftp_read()

From: zl liu <xieepp_at_gmail.com>
Date: Fri, 17 Jun 2011 10:01:10 +0800

在 2011年6月17日 上午5:58,Daniel Stenberg <daniel_at_haxx.se>写道:

> On Thu, 16 Jun 2011, zl liu wrote:
>
> Can you see if you can figure out how that can happen?
>>>
>>
> -7 haven't return to the caller. just the internal errno.
>>
>
> I'm confused. So which line of code logged the -7 you showed originally?

That's my fault.
I mean before the first -7 error message is print, the function call stack
is:

 libssh2.dll!send_existing(_LIBSSH2_SESSION * session=0x017d0068, const
unsigned char * data=0x014f1e00, unsigned int data_len=9, long *
ret=0x00035410) 行605 C
  libssh2.dll!_libssh2_transport_send(_LIBSSH2_SESSION * session=0x017d0068,
const unsigned char * data=0x014f1e00, unsigned int data_len=9, const
unsigned char * data2=0x00000000, unsigned int data2_len=0) 行696 + 0x15 字节
C
  libssh2.dll!_libssh2_channel_receive_window_adjust(_LIBSSH2_CHANNEL *
channel=0x014f1c10, unsigned int adjustment=3932160, unsigned char force=0,
unsigned int * store=0x00000000) 行1581 + 0x1b 字节 C
  libssh2.dll!_libssh2_channel_read(_LIBSSH2_CHANNEL * channel=0x014f1c10,
int stream_id=0, char * buf=0x017dda08, unsigned int buflen=4) 行1882 + 0x12
字节 C
  libssh2.dll!sftp_packet_read(_LIBSSH2_SFTP * sftp=0x017dd9e8) 行193 + 0x25
字节 C
  libssh2.dll!sftp_packet_requirev(_LIBSSH2_SFTP * sftp=0x017dd9e8, int
num_valid_responses=2, const unsigned char * valid_responses=0x1002c03c,
unsigned int request_id=1, unsigned char * * data=0x000359ec, unsigned int *
data_len=0x000359e0) 行404 + 0x9 字节 C
  libssh2.dll!sftp_read(_LIBSSH2_SFTP_HANDLE * handle=0x017dd3c0, char *
buffer=0x00035e68, unsigned int buffer_size=1024000) 行1204 + 0x25 字节 C
  libssh2.dll!libssh2_sftp_read(_LIBSSH2_SFTP_HANDLE * hnd=0x017dd3c0, char
* buffer=0x00035e68, unsigned int buffer_maxlen=1024000) 行1311 + 0x11 字节 C

Now the breakpoint is in the Transport.c,line 605.
The variable memory is:
data:
0x01391E00 5d 00 00 00 00 00 3c 00 ].....<.
0x01391E08 00 00 00 00 0b 00 00 00 ........

p->odata:
0x01391E18 5e 00 00 00 00 00 00 00 ^.......
0x01391E20 1d 00 00 00 00 00 00 00 ........
As you see data != p->odata, so the function send_existing() will
return LIBSSH2_ERROR_BAD_USE
 to the function _libssh2_transport_send() line 696.
the function _libssh2_transport_send() then return LIBSSH2_ERROR_BAD_USE to
the function _libssh2_channel_receive_window_adjust() line 1581 as follow:
rc = _libssh2_transport_send(channel->session, channel->adjust_adjust, 9,
                                 NULL, 0);
    if (rc == LIBSSH2_ERROR_EAGAIN) {
        _libssh2_error(channel->session, rc,
                       "Would block sending window adjust");
        return rc;
    }
    else if (rc) {
        channel->adjust_queue = adjustment;
        return _libssh2_error(channel->session, LIBSSH2_ERROR_SOCKET_SEND,
                              "Unable to send transfer-window adjustment "
                              "packet, deferring");
    }
    else {
        channel->remote.window_size += adjustment;
    }

because rc != LIBSSH2_ERROR_EAGAIN and rc != 0, so the
function _libssh2_transport_send() :1589 will print error message -7
and return -7 to the caller _libssh2_channel_read():1882
{
{
rc = _libssh2_channel_receive_window_adjust(channel,

 (LIBSSH2_CHANNEL_WINDOW_DEFAULT*60), 0, NULL);
        if (rc == LIBSSH2_ERROR_EAGAIN)
            return rc;

        _libssh2_debug(session, LIBSSH2_TRACE_CONN,
                       "channel_read() filled %d adjusted %d",
                       bytes_read, buflen);
        /* continue in 'created' state to drain the already read packages
           first before starting to empty the socket further */
        channel->read_state = libssh2_NB_state_created;
    }

    return bytes_read;
}
the function _libssh2_channel_read() will return bytes_read but not -7,in my
test ,bytes_read is 4.
so -7 will not return to the user level.
After that any call to the function send_existing() will print an error
message -7 .

i haven't send anything, it is a internal mistake?
>>
>
> I don't understand. Are you saying this was the first call to
> _libssh2_transport_send() ? What do you mean with you haven't send anything?

 I was to download a file, i only invoked the function libssh2_sftp_read(),
so i have not send anything in the user level. but the error is
from send_existing() , so i say it's a internal bug. No offense.

>

-- 
>
thanks.
>
>  / daniel.haxx.se
> _______________________________________________
> libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
>
>

_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2011-06-17