From 88165c5e1e28d534a2e1f31e33a673f2b405c2fd Mon Sep 17 00:00:00 2001
From: Clinton Stimpson <clinton@elemtech.com>
Date: Mon, 9 Dec 2013 21:38:55 -0700
Subject: [PATCH] Encoding: Fix bug in kwsysEncoding_DupToNarrow.

Change-Id: I96564a95f1ee6111edc58bdd85a933ace7502a73
---
 EncodingC.c      |  2 +-
 testEncoding.cxx | 11 ++++++++++-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/EncodingC.c b/EncodingC.c
index a36eecc..cda78e2 100644
--- a/EncodingC.c
+++ b/EncodingC.c
@@ -68,7 +68,7 @@ size_t kwsysEncoding_wcstombs(char* dest, const wchar_t* str, size_t n)
 char* kwsysEncoding_DupToNarrow(const wchar_t* str)
 {
   char* ret = NULL;
-  size_t length = kwsysEncoding_wcstombs(0, str, 0);
+  size_t length = kwsysEncoding_wcstombs(0, str, 0) + 1;
   if(length > 0)
     {
     ret = malloc(length);
diff --git a/testEncoding.cxx b/testEncoding.cxx
index 8e74a50..a65c430 100644
--- a/testEncoding.cxx
+++ b/testEncoding.cxx
@@ -16,14 +16,18 @@
 #endif
 
 #include KWSYS_HEADER(Encoding.hxx)
+#include KWSYS_HEADER(Encoding.h)
 #include KWSYS_HEADER(ios/iostream)
 
 #include <locale.h>
+#include <string.h>
+#include <stdlib.h>
 
 // Work-around CMake dependency scanning limitation.  This must
 // duplicate the above list of headers.
 #if 0
 # include "Encoding.hxx.in"
+# include "Encoding.h.in"
 # include "kwsys_ios_iostream.h.in"
 #endif
 
@@ -68,11 +72,16 @@ static int testHelloWorldEncoding()
     std::cout << str << std::endl;
     std::wstring wstr = kwsys::Encoding::ToWide(str);
     std::string str2 = kwsys::Encoding::ToNarrow(wstr);
-    if(!wstr.empty() && str != str2)
+    wchar_t* c_wstr = kwsysEncoding_DupToWide(str.c_str());
+    char* c_str2 = kwsysEncoding_DupToNarrow(c_wstr);
+    if(!wstr.empty() && (str != str2 || strcmp(c_str2, str.c_str())))
       {
       std::cout << "converted string was different: " << str2 << std::endl;
+      std::cout << "converted string was different: " << c_str2 << std::endl;
       ret++;
       }
+    free(c_wstr);
+    free(c_str2);
     }
   return ret;
 }
-- 
GitLab