From 5e6f3eaae9435b1ab7b36726e7b898d4994fcebf Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl@samba.org>
Date: Sat, 17 Jan 2009 12:18:29 +0100
Subject: [PATCH] Move initialization of the reply prs_struct to rpc_api_pipe

---
 source3/include/proto.h       |  2 +-
 source3/include/rpc_client.h  |  3 +--
 source3/librpc/rpc/dcerpc.c   |  3 ++-
 source3/rpc_client/cli_pipe.c | 23 ++++++++++-------------
 source3/rpc_client/ndr.c      |  4 +---
 5 files changed, 15 insertions(+), 20 deletions(-)

diff --git a/source3/include/proto.h b/source3/include/proto.h
index d644b09a6ac..9210a5cfe23 100644
--- a/source3/include/proto.h
+++ b/source3/include/proto.h
@@ -5197,7 +5197,7 @@ NTSTATUS rpccli_netlogon_set_trust_password(struct rpc_pipe_client *cli,
 
 /* The following definitions come from rpc_client/cli_pipe.c  */
 
-NTSTATUS rpc_api_pipe_req(struct rpc_pipe_client *cli,
+NTSTATUS rpc_api_pipe_req(TALLOC_CTX *mem_ctx, struct rpc_pipe_client *cli,
 			uint8 op_num,
 			prs_struct *in_data,
 			prs_struct *out_data);
diff --git a/source3/include/rpc_client.h b/source3/include/rpc_client.h
index 684044b8719..61b861c3b4e 100644
--- a/source3/include/rpc_client.h
+++ b/source3/include/rpc_client.h
@@ -49,9 +49,8 @@
 	if (!prs_init( &q_ps, RPC_MAX_PDU_FRAG_LEN, ctx, MARSHALL )) { \
 		return WERR_NOMEM;\
 	}\
-	prs_init_empty( &r_ps, ctx, UNMARSHALL );\
 	if ( q_io_fn("", &q_in, &q_ps, 0) ) {\
-		NTSTATUS _smb_pipe_stat_ = rpc_api_pipe_req(pcli, opnum, &q_ps, &r_ps); \
+		NTSTATUS _smb_pipe_stat_ = rpc_api_pipe_req(ctx, pcli, opnum, &q_ps, &r_ps); \
 		if (!NT_STATUS_IS_OK(_smb_pipe_stat_)) {\
 			prs_mem_free( &q_ps );\
 			prs_mem_free( &r_ps );\
diff --git a/source3/librpc/rpc/dcerpc.c b/source3/librpc/rpc/dcerpc.c
index 69bfc6f329a..21a2004422c 100644
--- a/source3/librpc/rpc/dcerpc.c
+++ b/source3/librpc/rpc/dcerpc.c
@@ -84,7 +84,8 @@ NTSTATUS dcerpc_ndr_request_recv(struct rpc_request *req)
 
 	prs_init_empty( &r_ps, req, UNMARSHALL );
 
-	status = rpc_api_pipe_req(req->pipe->rpc_cli, req->opnum, &req->q_ps, &r_ps); 
+	status = rpc_api_pipe_req(req, req->pipe->rpc_cli, req->opnum,
+				  &req->q_ps, &r_ps);
 
 	prs_mem_free( &req->q_ps );
 
diff --git a/source3/rpc_client/cli_pipe.c b/source3/rpc_client/cli_pipe.c
index d6f18846b1d..b93f6ed844d 100644
--- a/source3/rpc_client/cli_pipe.c
+++ b/source3/rpc_client/cli_pipe.c
@@ -1265,7 +1265,7 @@ static NTSTATUS cli_api_pipe(TALLOC_CTX *mem_ctx, struct rpc_pipe_client *cli,
 
  ****************************************************************************/
 
-static NTSTATUS rpc_api_pipe(struct rpc_pipe_client *cli,
+static NTSTATUS rpc_api_pipe(TALLOC_CTX *mem_ctx, struct rpc_pipe_client *cli,
 			prs_struct *data, /* Outgoing pdu fragment, already formatted for send. */
 			prs_struct *rbuf, /* Incoming reply - return as an NDR stream. */
 			uint8 expected_pkt_type)
@@ -1284,7 +1284,7 @@ static NTSTATUS rpc_api_pipe(struct rpc_pipe_client *cli,
 	}
 
 	/* Set up the current pdu parse struct. */
-	prs_init_empty(&current_pdu, prs_get_mem_context(rbuf), UNMARSHALL);
+	prs_init_empty(&current_pdu, talloc_tos(), UNMARSHALL);
 
 	DEBUG(5,("rpc_api_pipe: %s\n", rpccli_pipe_txt(debug_ctx(), cli)));
 
@@ -1316,9 +1316,8 @@ static NTSTATUS rpc_api_pipe(struct rpc_pipe_client *cli,
 	}
 	prs_give_memory(&current_pdu, (char *)rdata_copy, rdata_len, true);
 
-	/* Ensure we can mess with the return prs_struct. */
-	SMB_ASSERT(UNMARSHALLING(rbuf));
-	SMB_ASSERT(prs_data_size(rbuf) == 0);
+	/* Initialize the incoming PDU */
+	prs_init_empty(rbuf, mem_ctx, UNMARSHALL);
 
 	/* Make rbuf dynamic with no memory. */
 	prs_give_memory(rbuf, 0, 0, True);
@@ -1952,7 +1951,7 @@ static uint32 calculate_data_len_tosend(struct rpc_pipe_client *cli,
  and deals with signing/sealing details.
  ********************************************************************/
 
-NTSTATUS rpc_api_pipe_req(struct rpc_pipe_client *cli,
+NTSTATUS rpc_api_pipe_req(TALLOC_CTX *mem_ctx, struct rpc_pipe_client *cli,
 			uint8 op_num,
 			prs_struct *in_data,
 			prs_struct *out_data)
@@ -2055,7 +2054,8 @@ NTSTATUS rpc_api_pipe_req(struct rpc_pipe_client *cli,
 		/* Actually send the packet. */
 		if (flags & RPC_FLG_LAST) {
 			/* Last packet - send the data, get the reply and return. */
-			ret = rpc_api_pipe(cli, &outgoing_pdu, out_data, RPC_RESPONSE);
+			ret = rpc_api_pipe(mem_ctx, cli, &outgoing_pdu,
+					   out_data, RPC_RESPONSE);
 			prs_mem_free(&outgoing_pdu);
 
 			if ((DEBUGLEVEL >= 50)
@@ -2481,9 +2481,9 @@ static NTSTATUS rpc_finish_spnego_ntlmssp_bind(struct rpc_pipe_client *cli,
 
 	/* Initialize the returning data struct. */
 	prs_mem_free(rbuf);
-	prs_init_empty(rbuf, talloc_tos(), UNMARSHALL);
 
-	nt_status = rpc_api_pipe(cli, &rpc_out, rbuf, RPC_ALTCONTRESP);
+	nt_status = rpc_api_pipe(talloc_tos(), cli, &rpc_out, rbuf,
+				 RPC_ALTCONTRESP);
 	prs_mem_free(&rpc_out);
 	if (!NT_STATUS_IS_OK(nt_status)) {
 		return nt_status;
@@ -2559,11 +2559,8 @@ NTSTATUS rpc_pipe_bind(struct rpc_pipe_client *cli,
 		return status;
 	}
 
-	/* Initialize the incoming data struct. */
-	prs_init_empty(&rbuf, talloc_tos(), UNMARSHALL);
-
 	/* send data on \PIPE\.  receive a response */
-	status = rpc_api_pipe(cli, &rpc_out, &rbuf, RPC_BINDACK);
+	status = rpc_api_pipe(talloc_tos(), cli, &rpc_out, &rbuf, RPC_BINDACK);
 	prs_mem_free(&rpc_out);
 	if (!NT_STATUS_IS_OK(status)) {
 		return status;
diff --git a/source3/rpc_client/ndr.c b/source3/rpc_client/ndr.c
index 9ada47a3f5d..9c3205eca3e 100644
--- a/source3/rpc_client/ndr.c
+++ b/source3/rpc_client/ndr.c
@@ -59,9 +59,7 @@ NTSTATUS cli_do_rpc_ndr(struct rpc_pipe_client *cli,
 
 	talloc_free(push);
 
-	prs_init_empty( &r_ps, mem_ctx, UNMARSHALL );
-
-	status = rpc_api_pipe_req(cli, opnum, &q_ps, &r_ps); 
+	status = rpc_api_pipe_req(mem_ctx, cli, opnum, &q_ps, &r_ps);
 
 	prs_mem_free( &q_ps );