Commit 30b534c9 authored by Shuhei Matsumoto's avatar Shuhei Matsumoto Committed by Tomasz Zawadzki
Browse files

jsonrpc: Put null byte to the end of response object in req->send_buf



If we print a JSON RPC response object in the req->send_buf simply,
garbage will be contained because JSON RPC object is not required to be
terminated by a null byte. To exclude garbage from print, put a null
byte to the end of the JSON RPC response object before sending.

The null byte was not considered for the req->send_buf. Add one byte
when allocating or reallocating the req->send_buf for the null byte.

Signed-off-by: default avatarShuhei Matsumoto <smatsumoto@nvidia.com>
Change-Id: I1fa0b61db4ffba989da672b0d69662e3813aab8e
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/18828


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Mellanox Build Bot
Reviewed-by: default avatarAleksey Marchuk <alexeymar@nvidia.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
parent b3bec079
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -98,7 +98,8 @@ jsonrpc_server_write_cb(void *cb_ctx, const void *data, size_t size)
	if (new_size != request->send_buf_size) {
		uint8_t *new_buf;

		new_buf = realloc(request->send_buf, new_size);
		/* Add extra byte for the null terminator. */
		new_buf = realloc(request->send_buf, new_size + 1);
		if (new_buf == NULL) {
			SPDK_ERRLOG("Resizing send_buf failed (current size %zu, new size %zu)\n",
				    request->send_buf_size, new_size);
@@ -165,7 +166,8 @@ jsonrpc_parse_request(struct spdk_jsonrpc_server_conn *conn, const void *json, s
	request->send_offset = 0;
	request->send_len = 0;
	request->send_buf_size = SPDK_JSONRPC_SEND_BUF_SIZE_INIT;
	request->send_buf = malloc(request->send_buf_size);
	/* Add extra byte for the null terminator. */
	request->send_buf = malloc(request->send_buf_size + 1);
	if (request->send_buf == NULL) {
		SPDK_ERRLOG("Failed to allocate send_buf (%zu bytes)\n", request->send_buf_size);
		jsonrpc_free_request(request);
+5 −0
Original line number Diff line number Diff line
@@ -336,6 +336,11 @@ more:
		return 0;
	}

	if (request->send_offset == 0) {
		/* A byte for the null terminator is included in the send buffer. */
		request->send_buf[request->send_len] = '\0';
	}

	if (request->send_len > 0) {
		rc = send(conn->sockfd, request->send_buf + request->send_offset,
			  request->send_len, 0);