Skip to content

Commit 49b619b

Browse files
Fixed #397 and #398
1 parent 249b1c0 commit 49b619b

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

src/SBaseCommon.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,11 @@ void * LoadMpqTable(
10191019
return NULL;
10201020
}
10211021
}
1022+
else
1023+
{
1024+
// pocs/MPQ_2025_06_BadHashTableSize.mpq
1025+
dwCompressedSize = dwTableSize;
1026+
}
10221027

10231028
// Get the file offset from which we will read the table
10241029
// Note: According to Storm.dll from Warcraft III (version 2002),

src/SBaseFileTable.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,7 +2508,8 @@ TMPQHetTable * LoadHetTable(TMPQArchive * ha)
25082508
TMPQHeader * pHeader = ha->pHeader;
25092509

25102510
// If the HET table position is not 0, we expect the table to be present
2511-
if(pHeader->HetTablePos64 && pHeader->HetTableSize64)
2511+
// Alsom the HET table must have a reasonable size
2512+
if(pHeader->HetTablePos64 && pHeader->HetTableSize64 && pHeader->HetTableSize64 < BET_TABLE_MAX_SIZE)
25122513
{
25132514
// Attempt to load the HET table (Hash Extended Table)
25142515
pExtTable = LoadExtTable(ha, pHeader->HetTablePos64, (size_t)pHeader->HetTableSize64, HET_TABLE_SIGNATURE, MPQ_KEY_HASH_TABLE);
@@ -2530,7 +2531,7 @@ TMPQBetTable * LoadBetTable(TMPQArchive * ha)
25302531
TMPQHeader * pHeader = ha->pHeader;
25312532

25322533
// If the BET table position is not 0, we expect the table to be present
2533-
if(pHeader->BetTablePos64 && pHeader->BetTableSize64)
2534+
if(pHeader->BetTablePos64 && pHeader->BetTableSize64 && pHeader->BetTableSize64 < BET_TABLE_MAX_SIZE)
25342535
{
25352536
// Attempt to load the HET table (Hash Extended Table)
25362537
pExtTable = LoadExtTable(ha, pHeader->BetTablePos64, (size_t)pHeader->BetTableSize64, BET_TABLE_SIGNATURE, MPQ_KEY_BLOCK_TABLE);

src/StormLib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ extern "C" {
292292
// Signatures for HET and BET table
293293
#define HET_TABLE_SIGNATURE 0x1A544548 // 'HET\x1a'
294294
#define BET_TABLE_SIGNATURE 0x1A544542 // 'BET\x1a'
295+
#define BET_TABLE_MAX_SIZE 0x00100000 // Maximum acceptable size of HET&BET tables
295296

296297
// Decryption keys for MPQ tables
297298
#define MPQ_KEY_HASH_TABLE 0xC3AF3770 // Obtained by HashString("(hash table)", MPQ_HASH_FILE_KEY)

test/StormTest.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3960,8 +3960,8 @@ static void Test_PlayingSpace()
39603960
LPBYTE pbData;
39613961
DWORD dwFileSize = 529298;
39623962
DWORD dwBytesRead = 0;
3963-
3964-
if(SFileOpenArchive(_T("c:\\War3.mpq"), 0, 0, &hMpq))
3963+
/*
3964+
if(SFileOpenArchive(_T("e:\\2.mpq"), 0, 0, &hMpq))
39653965
{
39663966
if(SFileOpenFileEx(hMpq, "(listfile)", 0, &hFile))
39673967
{
@@ -3974,6 +3974,7 @@ static void Test_PlayingSpace()
39743974
}
39753975
SFileCloseArchive(hMpq);
39763976
}
3977+
*/
39773978
}
39783979

39793980
//-----------------------------------------------------------------------------
@@ -4237,7 +4238,7 @@ static const TEST_INFO1 TestList_MasterMirror[] =
42374238
static const TEST_INFO1 Test_OpenMpqs[] =
42384239
{
42394240

4240-
// PoC's by Gabe Sherman, tinh0.
4241+
// PoC's by Gabe Sherman, tinh0, Zao Yang
42414242
{_T("pocs/MPQ_2024_01_HeapOverrun.mpq"), NULL, "7008f95dcbc4e5d840830c176dec6969", 14},
42424243
{_T("pocs/MPQ_2024_02_StackOverflow.mpq"), NULL, "7093fcbcc9674b3e152e74e8e8a937bb", 4},
42434244
{_T("pocs/MPQ_2024_03_TooBigAlloc.mpq"), NULL, "--------------------------------", TFLG_WILL_FAIL},
@@ -4255,6 +4256,8 @@ static const TEST_INFO1 Test_OpenMpqs[] =
42554256
{_T("pocs/MPQ_2025_03_InvalidPatchInfo.mpq"), NULL, "93b885adfe0da089cdf634904fd59f71", TFLG_WILL_FAIL},
42564257
{_T("pocs/MPQ_2025_04_InvalidArchiveSize64.mpq"), NULL, "--------------------------------", TFLG_WILL_FAIL},
42574258
{_T("pocs/MPQ_2025_05_AddFileError.mpq"), NULL, "ce9b8afed4221a53663d391f10691ba6", TFLG_WILL_FAIL},
4259+
{_T("pocs/MPQ_2025_06_BadHashTableSize.mpq"), NULL, "00000000000000000000000000000000", TFLG_WILL_FAIL},
4260+
{_T("pocs/MPQ_2025_07_BadHetTableSize.mpq"), NULL, "00000000000000000000000000000000", TFLG_WILL_FAIL},
42584261

42594262
// Correct or damaged archives
42604263
{_T("MPQ_1997_v1_Diablo1_DIABDAT.MPQ"), NULL, "554b538541e42170ed41cb236483489e", 2910, &TwoFilesD1}, // Base MPQ from Diablo 1

test/stormlib-test-001.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ TestReadingMpq (pocs/MPQ_2025_02_SectorOffsetSizeNotAligned.mpq) succeeded.
3636
TestReadingMpq (pocs/MPQ_2025_03_InvalidPatchInfo.mpq) succeeded.
3737
TestReadingMpq (pocs/MPQ_2025_04_InvalidArchiveSize64.mpq) succeeded.
3838
TestReadingMpq (pocs/MPQ_2025_05_AddFileError.mpq) succeeded.
39+
TestReadingMpq (pocs/MPQ_2025_06_BadHashTableSize.mpq) succeeded.
40+
TestReadingMpq (pocs/MPQ_2025_07_BadHetTableSize.mpq) succeeded.
3941
TestReadingMpq (MPQ_1997_v1_Diablo1_DIABDAT.MPQ) succeeded.
4042
TestReadingMpq (MPQ_1997_v1_patch_rt_SC1B.mpq) succeeded.
4143
TestReadingMpq (MPQ_1997_v1_StarDat_SC1B.mpq) succeeded.

0 commit comments

Comments
 (0)