bulletin board remove post crash
Posted: Mon Jul 23, 2012 8:24 am
located this problem at remove post function, seems like an infinite loop at while (!file.eof()), i verified this by simply putting a Console.Error under while. But i still couldn't why it doesn't work and seek end of the file.
Code: Select all
void MsgBoardRemovePost( CSocket *mSock )
{
CChar *mChar = mSock->CurrcharObj();
if( !ValidateObject( mChar ) || !mChar->IsGM() )
{
mSock->sysmessage( "Only GM's may delete posts!" );
return;
}
CItem *msgBoard = calcItemObjFromSer( mSock->GetDWord( 4 ) );
if( !ValidateObject( msgBoard ) )
return;
std::string fileName = GetMsgBoardFile( msgBoard->GetSerial(), (mSock->GetByte( 8 ) - 0x40 ) );
if( fileName.empty() )
return;
if( !cwmWorldState->ServerData()->Directory( CSDDP_MSGBOARD ).empty() )
fileName = cwmWorldState->ServerData()->Directory( CSDDP_MSGBOARD ) + fileName;
SERIAL tmpSerial = 0;
size_t totalSize = 0;
UI16 tmpSize = 0;
char rBuffer[4];
bool foundPost = false;
bool removeReply = true;
const SERIAL msgSer = (mSock->GetDWord( 8 ) - BASEITEMSERIAL );
std::fstream file ( fileName.c_str(), std::ios::in | std::ios::out | std::ios::binary );
if( file.is_open() )
{
while( !file.eof() )
{
removeReply = false;
file.read( rBuffer, 2 );
tmpSize = ( (rBuffer[0]<<8) + rBuffer[1] );
file.seekg( 1, std::ios::cur );
file.read( rBuffer, 4 );
tmpSerial = calcserial( rBuffer[0], rBuffer[1], rBuffer[2], rBuffer[3] );
if( tmpSerial == msgSer ) // We need to remove any replies to a thread
{
removeReply = true;
file.seekp( totalSize+2, std::ios::beg );
file.put( 0x00 );
}
if( removeReply || !foundPost )
{
file.seekg( tmpSize-11, std::ios::cur );
file.read( rBuffer, 4 );
tmpSerial = calcserial( rBuffer[0], rBuffer[1], rBuffer[2], rBuffer[3] );
if( removeReply )
{
CPRemoveItem toRemove;
toRemove.Serial( (tmpSerial + BASEITEMSERIAL) );
mSock->Send( &toRemove );
}
else if( tmpSerial == msgSer ) // Mark it found, but don't break out of the loop, in order to get all replies
{
foundPost = true;
file.seekp( totalSize+2, std::ios::beg );
file.put( 0x00 );
mSock->sysmessage( 734 );
}
}
totalSize += tmpSize;
file.seekg( totalSize, std::ios::beg );
}
file.close();
}
else
Console.Error( "Could not open file %s for reading", fileName.c_str() );
if( foundPost )
{
CPRemoveItem toRemove;
toRemove.Serial( mSock->GetDWord( 8 ) );
mSock->Send( &toRemove );
}
}