From bbd63e136deb929f6d77c8152912f5cbb4bfb010 Mon Sep 17 00:00:00 2001
From: David Lawrence Ramsey <pooka109@gmail.com>
Date: Wed, 5 Jan 2005 16:59:49 +0000
Subject: [PATCH] fix potential memory corruption in display_string() when
 adding UTF-8 characters, found by valgrind (and caused by a bad merge of part
 of DB's code)

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2234 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 src/winio.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/winio.c b/src/winio.c
index 2d0b23aa..c07e5c14 100644
--- a/src/winio.c
+++ b/src/winio.c
@@ -2287,7 +2287,15 @@ char *display_string(const char *buf, size_t start_col, size_t len, bool
     assert(column <= start_col);
 
     alloc_len = display_string_len(buf + start_index, start_col,
-	start_col + len) + 2;
+	start_col + len);
+    alloc_len +=
+#ifdef NANO_WIDE
+	MB_CUR_MAX
+#else
+	1
+#endif
+	 * 2;
+
     converted = charalloc(alloc_len + 1);
     index = 0;
 
@@ -2464,8 +2472,6 @@ char *display_string(const char *buf, size_t start_col, size_t len, bool
     }
 
     /* Make sure that converted is at most len columns wide. */
-    converted[index] = '\0';
-    index = actual_x(converted, len);
     null_at(&converted, index);
 
     return converted;
-- 
GitLab