s3:pylibsmb: make use of protocol independent cli_read_send/recv in py_cli_read()
authorStefan Metzmacher <metze@samba.org>
Fri, 7 Dec 2018 13:28:04 +0000 (14:28 +0100)
committerStefan Metzmacher <metze@samba.org>
Thu, 13 Dec 2018 07:52:24 +0000 (08:52 +0100)
BUG: https://bugzilla.samba.org/show_bug.cgi?id=7113
BUG: https://bugzilla.samba.org/show_bug.cgi?id=11892
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13676

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Tim Beale <timbeale@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
source3/libsmb/pylibsmb.c

index 2dabacbff13a1d5959c73496fe9d1992f4e71f58..b6cff0bf1be81f8c4774a3b7da30d3a1b85d78ea 100644 (file)
@@ -769,8 +769,8 @@ static PyObject *py_cli_read(struct py_cli_state *self, PyObject *args,
        unsigned size;
        struct tevent_req *req;
        NTSTATUS status;
-       uint8_t *buf;
-       ssize_t buflen;
+       char *buf;
+       size_t received;
        PyObject *result;
 
        static const char *kwlist[] = {
@@ -782,20 +782,41 @@ static PyObject *py_cli_read(struct py_cli_state *self, PyObject *args,
                return NULL;
        }
 
-       req = cli_read_andx_send(NULL, self->ev, self->cli, fnum,
-                                offset, size);
+       result = PyBytes_FromStringAndSize(NULL, size);
+       if (result == NULL) {
+               return NULL;
+       }
+       buf = PyBytes_AS_STRING(result);
+
+       req = cli_read_send(NULL, self->ev, self->cli, fnum,
+                           buf, offset, size);
        if (!py_tevent_req_wait_exc(self, req)) {
+               Py_XDECREF(result);
                return NULL;
        }
-       status = cli_read_andx_recv(req, &buflen, &buf);
+       status = cli_read_recv(req, &received);
+       TALLOC_FREE(req);
 
        if (!NT_STATUS_IS_OK(status)) {
-               TALLOC_FREE(req);
+               Py_XDECREF(result);
                PyErr_SetNTSTATUS(status);
                return NULL;
        }
-       result = PyBytes_FromStringAndSize((const char *)buf, buflen);
-       TALLOC_FREE(req);
+
+       if (received > size) {
+               Py_XDECREF(result);
+               PyErr_Format(PyExc_IOError,
+                            "read invalid - got %zu requested %u",
+                            received, size);
+               return NULL;
+       }
+
+       if (received < size) {
+               if (_PyBytes_Resize(&result, received) < 0) {
+                       return NULL;
+               }
+       }
+
        return result;
 }