Avoid one malloc by putting the entire handle buffer in the handle struct

at once, use a define for the maximum size length of the handle instead of
'256' in the code.
This commit is contained in:
Daniel Stenberg 2008-12-17 10:45:20 +00:00
parent 2535c5c2ee
commit e26956be72
2 changed files with 7 additions and 13 deletions

View File

@ -532,12 +532,14 @@ struct _LIBSSH2_PUBLICKEY
unsigned long listFetch_data_len; unsigned long listFetch_data_len;
}; };
#define SFTP_HANDLE_MAXLEN 256 /* according to spec! */
struct _LIBSSH2_SFTP_HANDLE struct _LIBSSH2_SFTP_HANDLE
{ {
LIBSSH2_SFTP *sftp; LIBSSH2_SFTP *sftp;
LIBSSH2_SFTP_HANDLE *prev, *next; LIBSSH2_SFTP_HANDLE *prev, *next;
char *handle; char handle[SFTP_HANDLE_MAXLEN];
int handle_len; int handle_len;
char handle_type; char handle_type;

View File

@ -935,18 +935,11 @@ libssh2_sftp_open_ex(LIBSSH2_SFTP * sftp, const char *filename,
LIBSSH2_SFTP_HANDLE_DIR; LIBSSH2_SFTP_HANDLE_DIR;
fp->handle_len = libssh2_ntohu32(data + 5); fp->handle_len = libssh2_ntohu32(data + 5);
if (fp->handle_len > 256) { if (fp->handle_len > SFTP_HANDLE_MAXLEN) {
/* SFTP doesn't allow handles longer than 256 characters */ /* SFTP doesn't allow handles longer than 256 characters */
fp->handle_len = 256; fp->handle_len = SFTP_HANDLE_MAXLEN;
}
fp->handle = LIBSSH2_ALLOC(session, fp->handle_len);
if (!fp->handle) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate space for SFTP file/dir handle", 0);
LIBSSH2_FREE(session, data);
LIBSSH2_FREE(session, fp);
return NULL;
} }
memcpy(fp->handle, data + 9, fp->handle_len); memcpy(fp->handle, data + 9, fp->handle_len);
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
@ -994,7 +987,7 @@ libssh2_sftp_read(LIBSSH2_SFTP_HANDLE * handle, char *buffer,
packet = LIBSSH2_ALLOC(session, packet_len); packet = LIBSSH2_ALLOC(session, packet_len);
if (!packet) { if (!packet) {
libssh2_error(session, LIBSSH2_ERROR_ALLOC, libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for FXP_CLOSE packet", 0); "Unable to allocate memory for FXP_READ packet", 0);
return -1; return -1;
} }
sftp->read_state = libssh2_NB_state_allocated; sftp->read_state = libssh2_NB_state_allocated;
@ -1670,7 +1663,6 @@ libssh2_sftp_close_handle(LIBSSH2_SFTP_HANDLE * handle)
handle->close_state = libssh2_NB_state_idle; handle->close_state = libssh2_NB_state_idle;
LIBSSH2_FREE(session, handle->handle);
LIBSSH2_FREE(session, handle); LIBSSH2_FREE(session, handle);
return 0; return 0;