From f11630ab68b95fc64c0d3e7d1af9518ead22c853 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Mon, 27 Apr 2009 11:46:41 +0000 Subject: Improve sftp_reply_names_add. git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@625 7dcaeef0-15fb-0310-b436-a5af3365683c --- include/libssh/sftp.h | 4 ++-- libssh/sftpserver.c | 49 ++++++++++++++++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/include/libssh/sftp.h b/include/libssh/sftp.h index a54ec495..486165d8 100644 --- a/include/libssh/sftp.h +++ b/include/libssh/sftp.h @@ -654,8 +654,8 @@ STRING *sftp_handle_alloc(SFTP_SESSION *sftp, void *info); int sftp_reply_attr(SFTP_CLIENT_MESSAGE *msg, SFTP_ATTRIBUTES *attr); void *sftp_handle(SFTP_SESSION *sftp, STRING *handle); int sftp_reply_status(SFTP_CLIENT_MESSAGE *msg, u32 status, char *message); -int sftp_reply_names_add(SFTP_CLIENT_MESSAGE *msg, char *file, char *longname, - SFTP_ATTRIBUTES *attr); +int sftp_reply_names_add(SFTP_CLIENT_MESSAGE *msg, const char *file, + const char *longname, SFTP_ATTRIBUTES *attr); int sftp_reply_names(SFTP_CLIENT_MESSAGE *msg); int sftp_reply_data(SFTP_CLIENT_MESSAGE *msg, void *data, int len); void sftp_handle_remove(SFTP_SESSION *sftp, void *handle); diff --git a/libssh/sftpserver.c b/libssh/sftpserver.c index 08b214c3..4e1cebff 100644 --- a/libssh/sftpserver.c +++ b/libssh/sftpserver.c @@ -294,19 +294,42 @@ int sftp_reply_attr(SFTP_CLIENT_MESSAGE *msg, SFTP_ATTRIBUTES *attr) { return 0; } -int sftp_reply_names_add(SFTP_CLIENT_MESSAGE *msg, char *file, char *longname, - SFTP_ATTRIBUTES *attr){ - STRING *name=string_from_char(file); - if(!msg->attrbuf) - msg->attrbuf=buffer_new(); - buffer_add_ssh_string(msg->attrbuf,name); - free(name); - name=string_from_char(longname); - buffer_add_ssh_string(msg->attrbuf,name); - free(name); - buffer_add_attributes(msg->attrbuf,attr); - msg->attr_num++; - return 0; +int sftp_reply_names_add(SFTP_CLIENT_MESSAGE *msg, const char *file, + const char *longname, SFTP_ATTRIBUTES *attr) { + STRING *name; + + name = string_from_char(file); + if (name == NULL) { + return -1; + } + + if (msg->attrbuf == NULL) { + msg->attrbuf = buffer_new(); + if (msg->attrbuf == NULL) { + string_free(name); + return -1; + } + } + + if (buffer_add_ssh_string(msg->attrbuf, name) < 0) { + string_free(name); + return -1; + } + + string_free(name); + name = string_from_char(longname); + if (name == NULL) { + return -1; + } + if (buffer_add_ssh_string(msg->attrbuf,name) < 0 || + buffer_add_attributes(msg->attrbuf,attr) < 0) { + string_free(name); + return -1; + } + string_free(name); + msg->attr_num++; + + return 0; } int sftp_reply_names(SFTP_CLIENT_MESSAGE *msg){ -- cgit v1.2.3