Fix Red Hat bugzilla bug : https://bugzilla.redhat.com/show_bug.cgi?id=516165
[metze/samba/wip.git] / source3 / libsmb / clireadwrite.c
index 1d2f5f79ec62cbb37bfe06e0f52f2759efab318d..0d1f9e515ef778f5dd9670ffc3a5ee5f3b8ddfa5 100644 (file)
@@ -82,7 +82,7 @@ static void cli_read_andx_done(struct tevent_req *subreq);
 
 struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
                                        struct event_context *ev,
-                                       struct cli_state *cli, int fnum,
+                                       struct cli_state *cli, uint16_t fnum,
                                        off_t offset, size_t size,
                                        struct tevent_req **psmbreq)
 {
@@ -135,17 +135,23 @@ struct tevent_req *cli_read_andx_create(TALLOC_CTX *mem_ctx,
 
 struct tevent_req *cli_read_andx_send(TALLOC_CTX *mem_ctx,
                                      struct event_context *ev,
-                                     struct cli_state *cli, int fnum,
+                                     struct cli_state *cli, uint16_t fnum,
                                      off_t offset, size_t size)
 {
        struct tevent_req *req, *subreq;
+       NTSTATUS status;
 
        req = cli_read_andx_create(mem_ctx, ev, cli, fnum, offset, size,
                                   &subreq);
-       if ((req == NULL) || !cli_smb_req_send(subreq)) {
-               TALLOC_FREE(req);
+       if (req == NULL) {
                return NULL;
        }
+
+       status = cli_smb_req_send(subreq);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return tevent_req_post(req, ev);
+       }
        return req;
 }
 
@@ -194,7 +200,7 @@ static void cli_read_andx_done(struct tevent_req *subreq)
        state->buf = (uint8_t *)smb_base(inbuf) + SVAL(vwv+6, 0);
 
        if (trans_oob(smb_len(inbuf), SVAL(vwv+6, 0), state->received)
-           || (state->buf < bytes)) {
+           || (state->received && (state->buf < bytes))) {
                DEBUG(5, ("server returned invalid read&x data offset\n"));
                tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
                return;
@@ -554,7 +560,7 @@ static NTSTATUS cli_read_sink(char *buf, size_t n, void *priv)
        return NT_STATUS_OK;
 }
 
-ssize_t cli_read(struct cli_state *cli, int fnum, char *buf,
+ssize_t cli_read(struct cli_state *cli, uint16_t fnum, char *buf,
                 off_t offset, size_t size)
 {
        NTSTATUS status;
@@ -574,7 +580,7 @@ ssize_t cli_read(struct cli_state *cli, int fnum, char *buf,
 ****************************************************************************/
 
 static bool cli_issue_write(struct cli_state *cli,
-                               int fnum,
+                               uint16_t fnum,
                                off_t offset,
                                uint16 mode,
                                const char *buf,
@@ -674,7 +680,7 @@ static bool cli_issue_write(struct cli_state *cli,
 ****************************************************************************/
 
 ssize_t cli_write(struct cli_state *cli,
-                int fnum, uint16 write_mode,
+                uint16_t fnum, uint16 write_mode,
                 const char *buf, off_t offset, size_t size)
 {
        ssize_t bwritten = 0;
@@ -735,7 +741,7 @@ ssize_t cli_write(struct cli_state *cli,
 ****************************************************************************/
 
 ssize_t cli_smbwrite(struct cli_state *cli,
-                    int fnum, char *buf, off_t offset, size_t size1)
+                    uint16_t fnum, char *buf, off_t offset, size_t size1)
 {
        char *p;
        ssize_t total = 0;
@@ -848,9 +854,9 @@ struct tevent_req *cli_write_andx_create(TALLOC_CTX *mem_ctx,
        }
 
        state->pad = 0;
-       state->iov[0].iov_base = &state->pad;
+       state->iov[0].iov_base = (void *)&state->pad;
        state->iov[0].iov_len = 1;
-       state->iov[1].iov_base = CONST_DISCARD(uint8_t *, buf);
+       state->iov[1].iov_base = CONST_DISCARD(void *, buf);
        state->iov[1].iov_len = size;
 
        subreq = cli_smb_req_create(state, ev, cli, SMBwriteX, 0, wct, vwv,
@@ -870,13 +876,19 @@ struct tevent_req *cli_write_andx_send(TALLOC_CTX *mem_ctx,
                                       off_t offset, size_t size)
 {
        struct tevent_req *req, *subreq;
+       NTSTATUS status;
 
        req = cli_write_andx_create(mem_ctx, ev, cli, fnum, mode, buf, offset,
                                    size, NULL, 0, &subreq);
-       if ((req == NULL) || !cli_smb_req_send(subreq)) {
-               TALLOC_FREE(req);
+       if (req == NULL) {
                return NULL;
        }
+
+       status = cli_smb_req_send(subreq);
+       if (!NT_STATUS_IS_OK(status)) {
+               tevent_req_nterror(req, status);
+               return tevent_req_post(req, ev);
+       }
        return req;
 }