From e85378b2245e9b9d02bf9c4c307795f026d0ab2b Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Fri, 31 Dec 2021 15:04:40 -0600
Subject: [PATCH 03/10] Avoid buffer overflow found by valgrind

---
 src/common/zpq_stream.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/common/zpq_stream.c b/src/common/zpq_stream.c
index 294ad249ce7..307444e846d 100644
--- a/src/common/zpq_stream.c
+++ b/src/common/zpq_stream.c
@@ -985,7 +985,7 @@ zpq_serialize_compressors(zpq_compressor const *compressors, size_t n_compressor
 
 		/*
 		 * single entry looks like "alg_name:compression_level," so +2 is for
-		 * ":" and "," symbols
+		 * ":" and "," symbols (or trailing null)
 		 */
 		total_len += strlen(supported_algorithms[compressors[i].impl]) + level_len + 2;
 	}
@@ -994,8 +994,9 @@ zpq_serialize_compressors(zpq_compressor const *compressors, size_t n_compressor
 
 	for (i = 0; i < n_compressors; i++)
 	{
-		p += sprintf(p, "%s:%d,", supported_algorithms[compressors[i].impl], compressors[i].level);
+		p += sprintf(p, "%s:%d", supported_algorithms[compressors[i].impl], compressors[i].level);
+		if (i < n_compressors - 1)
+			*p++ = ',';
 	}
-	p[-1] = '\0';
 	return res;
 }
-- 
2.17.1

