From 92d686fe197db4eedb574730df766a7d1f683dd1 Mon Sep 17 00:00:00 2001
From: Dan Fandrich <dan@coneharvesters.com>
Date: Tue, 18 Feb 2014 23:34:31 +0100
Subject: [PATCH] Fixed a few memory leaks in error paths

---
 src/packet.c  | 2 ++
 src/session.c | 8 ++++++++
 src/sftp.c    | 1 +
 3 files changed, 11 insertions(+)

diff --git a/src/packet.c b/src/packet.c
index 47bbf2b..74e26d7 100644
--- a/src/packet.c
+++ b/src/packet.c
@@ -408,6 +408,7 @@ packet_x11_open(LIBSSH2_SESSION * session, unsigned char *data,
  *
  * The input pointer 'data' is pointing to allocated data that this function
  * is asked to deal with so on failure OR success, it must be freed fine.
+ * The only exception is when the return code is LIBSSH2_ERROR_EAGAIN.
  *
  * This function will always be called with 'datalen' greater than zero.
  */
@@ -967,6 +968,7 @@ _libssh2_packet_add(LIBSSH2_SESSION * session, unsigned char *data,
         if (!packetp) {
             _libssh2_debug(session, LIBSSH2_ERROR_ALLOC,
                            "memory for packet");
+            LIBSSH2_FREE(session, data);
             session->packAdd_state = libssh2_NB_state_idle;
             return LIBSSH2_ERROR_ALLOC;
         }
diff --git a/src/session.c b/src/session.c
index 9838d2b..cbdf980 100644
--- a/src/session.c
+++ b/src/session.c
@@ -1016,6 +1016,14 @@ session_free(LIBSSH2_SESSION *session)
     if (session->scpSend_command) {
         LIBSSH2_FREE(session, session->scpSend_command);
     }
+    if (session->sftpInit_sftp) {
+        LIBSSH2_FREE(session, session->sftpInit_sftp);
+    }
+
+    /* Free payload buffer */
+    if (session->packet.total_num) {
+        LIBSSH2_FREE(session, session->packet.payload);
+    }
 
     /* Cleanup all remaining packets */
     while ((pkg = _libssh2_list_first(&session->packets))) {
diff --git a/src/sftp.c b/src/sftp.c
index c2337d5..a0892f3 100644
--- a/src/sftp.c
+++ b/src/sftp.c
@@ -844,6 +844,7 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
     if (data_len < 5) {
         _libssh2_error(session, LIBSSH2_ERROR_SFTP_PROTOCOL,
                        "Invalid SSH_FXP_VERSION response");
+        LIBSSH2_FREE(session, data);
         goto sftp_init_error;
     }