Game Development Community

Buffer overflow in GuiTextEditCtrl

by Claude-Alain Fournier · in Torque Game Engine Advanced · 07/24/2006 (1:20 am) · 3 replies

Got some very strange memory problems and ugly crash when trying to display my game login panel, I found out it's linked to the following code in GuiTextEditCtrl::drawText (GuiTextEditCtrl.cpp)

if (mPasswordText)
   {		
      for (U32 i = 0; i < mTextBuffer.length() [b]- 1 [/b]; i++)
         textBuffer.append(StringBuffer(mPasswordMask));
   }

The problem is in the loop, if mTextBuffer.length() return 0 (it may if password is not initialised) as length() return a U32 integer, 0 - 1 = 4294967296 that's 4 giga of * in the buffer as result ;)

Whatever that - 1was there for ? it make the string one character short anyway.

Correct code :

if (mPasswordText)
   {		
      for (U32 i = 0; i < mTextBuffer.length(); i++)
         textBuffer.append(StringBuffer(mPasswordMask));
   }

In the same method after the this code you should use only textBuffer and not mTextBuffer. That's the reason why it does not show the password mask "*" instead of the clear text password.

#1
07/24/2006 (8:35 am)
Be careful about this, you may be running into the old 0-based array bug. If mTextBuffer is a char array, then the upper boundary of the array is one less than the "length." If mTextBuffer.length() = 3 then there are elements 0, 1, and 2. Why don't you try this:
if (mPasswordText [b]&& mTextBuffer.length() > 0[/b])
   {		
      for (U32 i = 0; i < mTextBuffer.length() - 1 ; i++)
         textBuffer.append(StringBuffer(mPasswordMask));
   }
#2
07/25/2006 (8:56 am)
I am aware of this, I checked the length() implementation before correcting ;)

Here is what length return :

return mBuffer.size() - 1; // Don't count the NULL of course.

I am too old a C developer to be forgetting this potential problem :P
#3
07/25/2006 (11:25 am)
Looks like the person who wrote the original code didn't check the definition of .length() then.