Skip to content

Commit ad7f6b8

Browse files
balmerdxhuangwei1024
authored andcommitted
Bugfix for aviod reading outside of the buffer. (cocos2d#19098)
Bugfix for aviod reading outside of the buffer. If new will allocate memory at the end of the heap - reading of MAX_LOG_LENGTH from memory would cause access violation reading location.
1 parent 23847ff commit ad7f6b8

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

cocos/base/CCConsole.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,16 +181,17 @@ void log(const char * format, ...)
181181

182182
do
183183
{
184-
std::copy(buf + pos, buf + pos + MAX_LOG_LENGTH, tempBuf);
184+
int dataSize = std::min(MAX_LOG_LENGTH, len - pos);
185+
std::copy(buf + pos, buf + pos + dataSize, tempBuf);
185186

186-
tempBuf[MAX_LOG_LENGTH] = 0;
187+
tempBuf[dataSize] = 0;
187188

188189
MultiByteToWideChar(CP_UTF8, 0, tempBuf, -1, wszBuf, sizeof(wszBuf));
189190
OutputDebugStringW(wszBuf);
190191
WideCharToMultiByte(CP_ACP, 0, wszBuf, -1, tempBuf, sizeof(tempBuf), nullptr, FALSE);
191192
printf("%s", tempBuf);
192193

193-
pos += MAX_LOG_LENGTH;
194+
pos += dataSize;
194195

195196
} while (pos < len);
196197
SendLogToWindow(buf);

0 commit comments

Comments
 (0)