Fix error handling in sftp (the sftp_recv return value was being
[sgt/putty] / sftp.c
diff --git a/sftp.c b/sftp.c
index 99d50a7..9ef69c2 100644 (file)
--- a/sftp.c
+++ b/sftp.c
@@ -7,13 +7,10 @@
 #include <string.h>
 #include <assert.h>
 
+#include "misc.h"
 #include "int64.h"
 #include "sftp.h"
 
-#define smalloc malloc
-#define srealloc realloc
-#define sfree free
-
 #define GET_32BIT(cp) \
     (((unsigned long)(unsigned char)(cp)[0] << 24) | \
     ((unsigned long)(unsigned char)(cp)[1] << 16) | \
@@ -373,6 +370,10 @@ char *fxp_realpath(char *path)
     sftp_pkt_addstring_str(pktout, path);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return NULL;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x123) {
        fxp_internal_error("request ID mismatch\n");
@@ -417,6 +418,10 @@ struct fxp_handle *fxp_open(char *path, int type)
     sftp_pkt_adduint32(pktout, 0);     /* (FIXME) empty ATTRS structure */
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return NULL;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x567) {
        fxp_internal_error("request ID mismatch\n");
@@ -456,6 +461,10 @@ struct fxp_handle *fxp_opendir(char *path)
     sftp_pkt_addstring(pktout, path);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return NULL;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x456) {
        fxp_internal_error("request ID mismatch\n");
@@ -496,6 +505,10 @@ void fxp_close(struct fxp_handle *handle)
     sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x789) {
        fxp_internal_error("request ID mismatch\n");
@@ -517,6 +530,10 @@ int fxp_mkdir(char *path)
     sftp_pkt_adduint32(pktout, 0);     /* (FIXME) empty ATTRS structure */
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return 0;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x234) {
        fxp_internal_error("request ID mismatch\n");
@@ -539,6 +556,10 @@ int fxp_rmdir(char *path)
     sftp_pkt_addstring(pktout, path);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return 0;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x345) {
        fxp_internal_error("request ID mismatch\n");
@@ -561,6 +582,10 @@ int fxp_remove(char *fname)
     sftp_pkt_addstring(pktout, fname);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return 0;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x678) {
        fxp_internal_error("request ID mismatch\n");
@@ -584,6 +609,10 @@ int fxp_rename(char *srcfname, char *dstfname)
     sftp_pkt_addstring(pktout, dstfname);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return 0;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x678) {
        fxp_internal_error("request ID mismatch\n");
@@ -610,6 +639,10 @@ int fxp_stat(char *fname, struct fxp_attrs *attrs)
     sftp_pkt_addstring(pktout, fname);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return 0;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x678) {
        fxp_internal_error("request ID mismatch\n");
@@ -636,6 +669,10 @@ int fxp_fstat(struct fxp_handle *handle, struct fxp_attrs *attrs)
     sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return 0;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x678) {
        fxp_internal_error("request ID mismatch\n");
@@ -665,6 +702,10 @@ int fxp_setstat(char *fname, struct fxp_attrs attrs)
     sftp_pkt_addattrs(pktout, attrs);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return 0;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x678) {
        fxp_internal_error("request ID mismatch\n");
@@ -688,6 +729,10 @@ int fxp_fsetstat(struct fxp_handle *handle, struct fxp_attrs attrs)
     sftp_pkt_addattrs(pktout, attrs);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return 0;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0x678) {
        fxp_internal_error("request ID mismatch\n");
@@ -720,6 +765,10 @@ int fxp_read(struct fxp_handle *handle, char *buffer, uint64 offset,
     sftp_pkt_adduint32(pktout, len);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return -1;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0xBCD) {
        fxp_internal_error("request ID mismatch");
@@ -759,6 +808,10 @@ struct fxp_names *fxp_readdir(struct fxp_handle *handle)
     sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return NULL;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0xABC) {
        fxp_internal_error("request ID mismatch\n");
@@ -804,6 +857,10 @@ int fxp_write(struct fxp_handle *handle, char *buffer, uint64 offset,
     sftp_pkt_addstring_data(pktout, buffer, len);
     sftp_send(pktout);
     pktin = sftp_recv();
+    if (!pktin) {
+       fxp_internal_error("did not receive a valid SFTP packet\n");
+       return 0;
+    }
     id = sftp_pkt_getuint32(pktin);
     if (id != 0xDCB) {
        fxp_internal_error("request ID mismatch\n");