[FIXED] bulletin board remove post crash

Found a bug in UOX3? Or experienced a server crash? Perhaps you've noticed a broken feature? Post the details here!
Post Reply
xantier
UOX3 Novice
Posts: 58
Joined: Tue Dec 20, 2011 6:51 am
Has thanked: 0
Been thanked: 0

bulletin board remove post crash

Post by xantier »

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 );
	}
}
User avatar
Xuri
Site Admin
Posts: 3704
Joined: Mon Jun 02, 2003 9:11 am
Location: Norway
Has thanked: 48 times
Been thanked: 8 times
Contact:

Post by Xuri »

Have seen this infinite loop bug happen myself, but don't know why - or how to fix it. :/
-= Ho Eyo He Hum =-
xantier
UOX3 Novice
Posts: 58
Joined: Tue Dec 20, 2011 6:51 am
Has thanked: 0
Been thanked: 0

Post by xantier »

i think whole post system must be changed. we can treat bulletin board as a 'container' that has books inside as posts. when you post something, a book will be created inside, book will be deleted when you click remove post etc.

it is infinite loop because file.eof() isnt called somehow.
Post Reply