Skip to content

Commit a67d203

Browse files
committed
Possibly fixed bug in using LuaCOM which might cause a crash
1 parent 662585f commit a67d203

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

luacom/tUtil.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const char * tUtil::bstr2string(BSTR bstr)
9292
if(!size)
9393
LUACOM_ERROR(tUtil::GetErrorMessage(GetLastError()));
9494

95-
str = new char[size];
95+
str = new char[size + 1];
9696

9797
result = WideCharToMultiByte(
9898
CP_UTF8, // code page
@@ -108,6 +108,7 @@ const char * tUtil::bstr2string(BSTR bstr)
108108
if(!result)
109109
LUACOM_ERROR(tUtil::GetErrorMessage(GetLastError()));
110110

111+
str [size] = '\0';
111112
}
112113
else
113114
{
@@ -165,7 +166,7 @@ const char * tUtil::bstr2string(BSTR bstr, size_t& computedSize)
165166
if(!size)
166167
LUACOM_ERROR(tUtil::GetErrorMessage(GetLastError()));
167168

168-
str = new char[size];
169+
str = new char[size + 1];
169170

170171
result = WideCharToMultiByte(
171172
CP_UTF8, // code page
@@ -180,7 +181,9 @@ const char * tUtil::bstr2string(BSTR bstr, size_t& computedSize)
180181

181182
if(!result)
182183
LUACOM_ERROR(tUtil::GetErrorMessage(GetLastError()));
183-
184+
185+
str [size] = '\0';
186+
184187
computedSize = size; // Now holds the actual length of the string
185188
}
186189
else
@@ -200,7 +203,7 @@ const char * tUtil::bstr2string(BSTR bstr, size_t& computedSize)
200203
str[0] = '\0';
201204
}
202205

203-
tUtil::string_buffer.copyToBuffer(str, computedSize);
206+
tUtil::string_buffer.copyToBuffer(str, computedSize + 1);
204207

205208
delete[] str;
206209

0 commit comments

Comments
 (0)