Skip to content

Commit bf9c23b

Browse files
committed
Fixed bug where Z_BUF_ERROR error was not handled correctly
1 parent 00fd050 commit bf9c23b

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

doc.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ CString str = strText;
13431343

13441344
void CMUSHclientDoc::ReceiveMsg()
13451345
{
1346-
char buff [1000];
1346+
char buff [1000]; // must be less than COMPRESS_BUFFER_LENGTH or it won't fit
13471347
int count = m_pSocket->Receive (buff, sizeof (buff) - 1);
13481348

13491349
Frame.CheckTimerFallback (); // see if time is up for timers to fire
@@ -1470,7 +1470,30 @@ int count = m_pSocket->Receive (buff, sizeof (buff) - 1);
14701470
}
14711471

14721472
// decompress it
1473-
int iCompressResult = inflate (&m_zCompress, Z_SYNC_FLUSH);
1473+
int iCompressResult;
1474+
do {
1475+
iCompressResult = inflate (&m_zCompress, Z_SYNC_FLUSH);
1476+
1477+
// buffer too small? (highly compressed text, huh?)
1478+
// make larger, try again - version 4.74
1479+
// See: http://www.gammon.com.au/forum/?id=11160
1480+
if (iCompressResult == Z_BUF_ERROR)
1481+
{
1482+
m_nCompressionOutputBufferSize += COMPRESS_BUFFER_LENGTH;
1483+
m_zCompress.avail_out += COMPRESS_BUFFER_LENGTH;
1484+
m_CompressOutput = (Bytef *) realloc (m_CompressOutput, m_nCompressionOutputBufferSize);
1485+
1486+
if (m_CompressOutput == NULL)
1487+
{
1488+
OnConnectionDisconnect (); // close the world
1489+
free (m_CompressInput); // may as well get rid of compression input as well
1490+
m_CompressInput = NULL;
1491+
TMessageBox ("Insufficient memory to decompress MCCP text.", MB_ICONEXCLAMATION);
1492+
return;
1493+
} // end of cannot get more memory
1494+
} // end of Z_BUF_ERROR
1495+
1496+
} while (iCompressResult == Z_BUF_ERROR);
14741497

14751498
if (App.m_iCounterFrequency)
14761499
{
@@ -1481,18 +1504,20 @@ int count = m_pSocket->Receive (buff, sizeof (buff) - 1);
14811504
// error?
14821505
if (iCompressResult < 0)
14831506
{
1507+
char * pMsg = m_zCompress.msg; // closing may clear the message
1508+
14841509
OnConnectionDisconnect (); // close the world
1485-
if (m_zCompress.msg)
1510+
if (pMsg)
14861511
UMessageBox (TFormat ("Could not decompress text from MUD: %s",
1487-
(LPCTSTR) m_zCompress.msg), MB_ICONEXCLAMATION);
1512+
(LPCTSTR) pMsg), MB_ICONEXCLAMATION);
14881513
else
14891514
UMessageBox (TFormat ("Could not decompress text from MUD: %i",
14901515
iCompressResult), MB_ICONEXCLAMATION);
14911516
return;
14921517
}
14931518

14941519
// work out how much we got, and display it
1495-
int iLength = COMPRESS_BUFFER_LENGTH - m_zCompress.avail_out;
1520+
int iLength = m_nCompressionOutputBufferSize - m_zCompress.avail_out;
14961521

14971522
// stats - count uncompressed bytes
14981523
m_nTotalUncompressed += iLength;
@@ -1507,7 +1532,7 @@ int count = m_pSocket->Receive (buff, sizeof (buff) - 1);
15071532

15081533
DisplayMsg ((LPCTSTR) m_CompressOutput, iLength, 0); // send uncompressed data to screen
15091534
m_zCompress.next_out = m_CompressOutput; // reset for more output
1510-
m_zCompress.avail_out = COMPRESS_BUFFER_LENGTH;
1535+
m_zCompress.avail_out = m_nCompressionOutputBufferSize;
15111536
}
15121537

15131538
// if end of stream, turn decompression off
@@ -1971,7 +1996,7 @@ CString strLine (lpszText, size);
19711996
m_zCompress.next_in = m_CompressInput;
19721997
m_zCompress.avail_in = size;
19731998
m_zCompress.next_out = m_CompressOutput;
1974-
m_zCompress.avail_out = COMPRESS_BUFFER_LENGTH;
1999+
m_zCompress.avail_out = m_nCompressionOutputBufferSize;
19752000
m_nTotalCompressed += size;
19762001
return; // done with this loop, now it needs to be decompressed
19772002
}
@@ -1993,7 +2018,7 @@ CString strLine (lpszText, size);
19932018
m_zCompress.next_in = m_CompressInput;
19942019
m_zCompress.avail_in = size;
19952020
m_zCompress.next_out = m_CompressOutput;
1996-
m_zCompress.avail_out = COMPRESS_BUFFER_LENGTH;
2021+
m_zCompress.avail_out = m_nCompressionOutputBufferSize;
19972022
m_nTotalCompressed += size;
19982023
return; // done with this loop, now it needs to be decompressed
19992024
}

doc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,7 @@ class CMUSHclientDoc : public CDocument
10161016
__int64 m_nTotalUncompressed;
10171017
__int64 m_nTotalCompressed;
10181018
LONGLONG m_iCompressionTimeTaken; // time taken to decompress
1019+
long m_nCompressionOutputBufferSize; // size of decompression output buffer
10191020

10201021

10211022
int m_iMCCP_type; // MCCP protocol type in use: 0 = none, 1 = v1, 2 = v2

doc_construct.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ int i;
222222
m_nTotalUncompressed = 0;
223223
m_nTotalCompressed = 0;
224224
m_iCompressionTimeTaken = 0;
225+
m_nCompressionOutputBufferSize = COMPRESS_BUFFER_LENGTH; // initial value
225226

226227
// we will defer initialising zlib until we really have to
227228

telnet_phases.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ void CMUSHclientDoc::Phase_WILL (const unsigned char c)
248248
if (m_bCompressInitOK && !m_bDisableCompression)
249249
{
250250
if (!m_CompressOutput)
251-
m_CompressOutput = (Bytef *) malloc (COMPRESS_BUFFER_LENGTH);
251+
m_CompressOutput = (Bytef *) malloc (m_nCompressionOutputBufferSize);
252252
if (!m_CompressInput)
253253
m_CompressInput = (Bytef *) malloc (COMPRESS_BUFFER_LENGTH);
254254

0 commit comments

Comments
 (0)