Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop ErrorLogger from from overflowing its internal buffer #106

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions ThirdParty/PSCommon/XnLib/Source/Win32/XnWin32Strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,11 @@ XN_C_API XnStatus xnOSStrFormatV(XnChar* cpDestString, const XnUInt32 nDestLengt

// nRes is the number of bytes written, not including NULL termination

if ((nRes == -1) || // string was truncated
(nRes == (XnInt32)nDestLength && cpDestString[nRes] != '\0')) // no space for the NULL termination
if ( nRes < 0 // encoding error
|| nRes > (XnInt32)nDestLength // string was truncated
|| (nRes == (XnInt32)nDestLength && cpDestString[nRes] != '\0')) // no space for the NULL termination
{
*pnCharsWritten = nDestLength;
return (XN_STATUS_INTERNAL_BUFFER_TOO_SMALL);
}

Expand Down
37 changes: 24 additions & 13 deletions ThirdParty/PSCommon/XnLib/Source/XnErrorLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,49 @@ namespace xnl
{
SingleBuffer* pBuffer = getBuffer();

if (pBuffer->m_currentEnd > ms_bufferSize)
if (pBuffer->m_currentEnd >= ms_bufferSize)
return;

pBuffer->m_errorBuffer[pBuffer->m_currentEnd++] = '\t';
unsigned int charsWritten;

va_list args;
va_start(args, cpFormat);
xnOSStrFormatV(pBuffer->m_errorBuffer+pBuffer->m_currentEnd, ms_bufferSize-pBuffer->m_currentEnd, &charsWritten, cpFormat, args);
XnStatus status = xnOSStrFormatV(pBuffer->m_errorBuffer+pBuffer->m_currentEnd, ms_bufferSize-pBuffer->m_currentEnd, &charsWritten, cpFormat, args);
va_end(args);

pBuffer->m_currentEnd += charsWritten;
pBuffer->m_errorBuffer[pBuffer->m_currentEnd++] = '\n';
pBuffer->m_errorBuffer[pBuffer->m_currentEnd] = '\0';

if (status == XN_STATUS_OK)
{
pBuffer->m_currentEnd += charsWritten;
pBuffer->m_errorBuffer[pBuffer->m_currentEnd++] = '\n';
pBuffer->m_errorBuffer[pBuffer->m_currentEnd] = '\0';
}
else
{
pBuffer->m_currentEnd += charsWritten;
}
}

void ErrorLogger::AppendV(const XnChar* cpFormat, va_list args)
{
SingleBuffer* pBuffer = getBuffer();

if (pBuffer->m_currentEnd > ms_bufferSize)
if (pBuffer->m_currentEnd >= ms_bufferSize)
return;

pBuffer->m_errorBuffer[pBuffer->m_currentEnd++] = '\t';
unsigned int charsWritten;

xnOSStrFormatV(pBuffer->m_errorBuffer+pBuffer->m_currentEnd, ms_bufferSize-pBuffer->m_currentEnd, &charsWritten, cpFormat, args);

pBuffer->m_currentEnd += charsWritten;
pBuffer->m_errorBuffer[pBuffer->m_currentEnd++] = '\n';
pBuffer->m_errorBuffer[pBuffer->m_currentEnd] = '\0';

if (xnOSStrFormatV(pBuffer->m_errorBuffer + pBuffer->m_currentEnd, ms_bufferSize - pBuffer->m_currentEnd, &charsWritten, cpFormat, args) == XN_STATUS_OK)
{
pBuffer->m_currentEnd += charsWritten;
pBuffer->m_errorBuffer[pBuffer->m_currentEnd++] = '\n';
pBuffer->m_errorBuffer[pBuffer->m_currentEnd] = '\0';
}
else
{
pBuffer->m_currentEnd += charsWritten;
}
}

ErrorLogger::ErrorLogger()
Expand Down