Skip to content
Snippets Groups Projects
Commit 88165c5e authored by Clinton Stimpson's avatar Clinton Stimpson Committed by Brad King
Browse files

Encoding: Fix bug in kwsysEncoding_DupToNarrow.

Change-Id: I96564a95f1ee6111edc58bdd85a933ace7502a73
parent ab6f8c36
No related branches found
No related tags found
No related merge requests found
...@@ -68,7 +68,7 @@ size_t kwsysEncoding_wcstombs(char* dest, const wchar_t* str, size_t n) ...@@ -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* kwsysEncoding_DupToNarrow(const wchar_t* str)
{ {
char* ret = NULL; char* ret = NULL;
size_t length = kwsysEncoding_wcstombs(0, str, 0); size_t length = kwsysEncoding_wcstombs(0, str, 0) + 1;
if(length > 0) if(length > 0)
{ {
ret = malloc(length); ret = malloc(length);
......
...@@ -16,14 +16,18 @@ ...@@ -16,14 +16,18 @@
#endif #endif
#include KWSYS_HEADER(Encoding.hxx) #include KWSYS_HEADER(Encoding.hxx)
#include KWSYS_HEADER(Encoding.h)
#include KWSYS_HEADER(ios/iostream) #include KWSYS_HEADER(ios/iostream)
#include <locale.h> #include <locale.h>
#include <string.h>
#include <stdlib.h>
// Work-around CMake dependency scanning limitation. This must // Work-around CMake dependency scanning limitation. This must
// duplicate the above list of headers. // duplicate the above list of headers.
#if 0 #if 0
# include "Encoding.hxx.in" # include "Encoding.hxx.in"
# include "Encoding.h.in"
# include "kwsys_ios_iostream.h.in" # include "kwsys_ios_iostream.h.in"
#endif #endif
...@@ -68,11 +72,16 @@ static int testHelloWorldEncoding() ...@@ -68,11 +72,16 @@ static int testHelloWorldEncoding()
std::cout << str << std::endl; std::cout << str << std::endl;
std::wstring wstr = kwsys::Encoding::ToWide(str); std::wstring wstr = kwsys::Encoding::ToWide(str);
std::string str2 = kwsys::Encoding::ToNarrow(wstr); 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: " << str2 << std::endl;
std::cout << "converted string was different: " << c_str2 << std::endl;
ret++; ret++;
} }
free(c_wstr);
free(c_str2);
} }
return ret; return ret;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment