From b7fc2e183f824003556e8c2cbf9bfa40d611228b Mon Sep 17 00:00:00 2001
From: Andre Noll <maan@systemlinux.org>
Date: Tue, 30 Jul 2013 00:44:37 +0200
Subject: [PATCH] server/gcrypt: Fix sending the empty status items.

When para_server enters the "stopped" state, it needs to inform all
clients which are executing the stat command that no audio file is
currently available.

To this aim the stat command handler calls empty_status_items(), which
creates a buffer containing empty values for most status items. This
function tries to be smart by computing the buffer only once. It
saves a reference in a static variable so that on subsequent calls
it can simply return the same buffer.

This works fine when para_server is compiled against the openssl
crypto library. However, it fails when libgcrypt is used because with
libgcrypt the send buffer is encrypted in-place. Hence on subsequent
calls the already encrypted buffer will be encrypted again, resulting
in garbage being sent to the client.

This patch avoids this bug by using a fresh buffer each time the
empty status items are sent.
---
 command.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/command.c b/command.c
index 79ca3652..41a58eac 100644
--- a/command.c
+++ b/command.c
@@ -474,11 +474,8 @@ static int com_version(struct command_context *cc)
  */
 static unsigned empty_status_items(int parser_friendly, char **result)
 {
-	static char *esi;
-	static unsigned len;
-
-	if (esi)
-		goto out;
+	char *esi;
+	unsigned len;
 
 	if (parser_friendly)
 		len = xasprintf(&esi,
@@ -498,7 +495,6 @@ static unsigned empty_status_items(int parser_friendly, char **result)
 			EMPTY_STATUS_ITEMS
 			#undef ITEM
 		);
-out:
 	*result = esi;
 	return len;
 }
@@ -509,7 +505,7 @@ static int com_stat(struct command_context *cc)
 {
 	int i, ret;
 	struct misc_meta_data tmp, *nmmd = &tmp;
-	char *s, *esi = NULL;
+	char *s;
 	int32_t num = 0;
 	int parser_friendly = 0;
 
@@ -549,12 +545,15 @@ static int com_stat(struct command_context *cc)
 		if (ret < 0)
 			goto out;
 		if (nmmd->vss_status_flags & VSS_NEXT) {
+			char *esi;
 			ret = empty_status_items(parser_friendly, &esi);
 			if (cc->use_sideband)
 				ret = send_sb(&cc->scc, esi, ret, SBD_OUTPUT,
-					true);
-			else
+					false);
+			else {
 				ret = sc_send_bin_buffer(&cc->scc, esi, ret);
+				free(esi);
+			}
 			if (ret < 0)
 				goto out;
 		} else
@@ -568,7 +567,6 @@ static int com_stat(struct command_context *cc)
 			goto out;
 	}
 out:
-	free(esi);
 	return ret;
 }
 
-- 
2.39.5