@@ -40,7 +40,7 @@ static bool parseHexColorString(const std::string &value, video::SColor &color);
40
40
static bool parseNamedColorString (const std::string &value, video::SColor &color);
41
41
42
42
#ifndef _WIN32
43
- size_t convert (const char *to, const char *from, char *outbuf,
43
+ bool convert (const char *to, const char *from, char *outbuf,
44
44
size_t outbuf_size, char *inbuf, size_t inbuf_size)
45
45
{
46
46
iconv_t cd = iconv_open (to, from);
@@ -56,11 +56,18 @@ size_t convert(const char *to, const char *from, char *outbuf,
56
56
size_t *inbuf_left_ptr = &inbuf_size;
57
57
size_t *outbuf_left_ptr = &outbuf_size;
58
58
59
- while (inbuf_size > 0 )
59
+ size_t old_size = inbuf_size;
60
+ while (inbuf_size > 0 ) {
60
61
iconv (cd, &inbuf_ptr, inbuf_left_ptr, &outbuf_ptr, outbuf_left_ptr);
62
+ if (inbuf_size == old_size) {
63
+ iconv_close (cd);
64
+ return false ;
65
+ }
66
+ old_size = inbuf_size;
67
+ }
61
68
62
69
iconv_close (cd);
63
- return 0 ;
70
+ return true ;
64
71
}
65
72
66
73
std::wstring utf8_to_wide (const std::string &input)
@@ -74,7 +81,13 @@ std::wstring utf8_to_wide(const std::string &input)
74
81
char *outbuf = new char [outbuf_size];
75
82
memset (outbuf, 0 , outbuf_size);
76
83
77
- convert (" WCHAR_T" , " UTF-8" , outbuf, outbuf_size, inbuf, inbuf_size);
84
+ if (!convert (" WCHAR_T" , " UTF-8" , outbuf, outbuf_size, inbuf, inbuf_size)) {
85
+ infostream << " Couldn't convert UTF-8 string 0x" << hex_encode (input)
86
+ << " into wstring" << std::endl;
87
+ delete[] inbuf;
88
+ delete[] outbuf;
89
+ return L" <invalid UTF-8 string>" ;
90
+ }
78
91
std::wstring out ((wchar_t *)outbuf);
79
92
80
93
delete[] inbuf;
@@ -101,7 +114,13 @@ std::string wide_to_utf8(const std::wstring &input)
101
114
char *outbuf = new char [outbuf_size];
102
115
memset (outbuf, 0 , outbuf_size);
103
116
104
- convert (" UTF-8" , " WCHAR_T" , outbuf, outbuf_size, inbuf, inbuf_size);
117
+ if (!convert (" UTF-8" , " WCHAR_T" , outbuf, outbuf_size, inbuf, inbuf_size)) {
118
+ infostream << " Couldn't convert wstring 0x" << hex_encode (inbuf, inbuf_size)
119
+ << " into wstring" << std::endl;
120
+ delete[] inbuf;
121
+ delete[] outbuf;
122
+ return " <invalid wstring>" ;
123
+ }
105
124
std::string out (outbuf);
106
125
107
126
delete[] inbuf;
0 commit comments