Skip to content

Commit 70dd55a

Browse files
TomArrowTom
andauthored
patterns/q3demo: Quake 3 demos: Handle corrupted files more gracefully (#414)
Quake 3 demos: Basic safeguard against corrupted files Co-authored-by: Tom <[email protected]>
1 parent 76f850c commit 70dd55a

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

patterns/q3demo.hexpat

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,15 @@ fn readString(ref auto data, s32 len, s32 bitindex){
118118
return test;
119119
};
120120

121+
bool corrupted = false;
122+
121123
struct Message {
122124
le s32 messageNum;
123125
le s32 len;
124-
if(len != FINAL_DEMO_MESSAGE_LENGTH || messageNum != FINAL_DEMO_MESSAGE_NUMBER) {
126+
if(len+$ > std::mem::size()){
127+
corrupted = true;
128+
}
129+
if(!corrupted && (len != FINAL_DEMO_MESSAGE_LENGTH || messageNum != FINAL_DEMO_MESSAGE_NUMBER)) {
125130
u8 data[len];
126131
if( len>=10){ // should usually be true unless corrupted
127132

@@ -189,7 +194,7 @@ namespace format {
189194
}
190195

191196
struct Q3Demo {
192-
Message messages[while(!std::mem::eof())];
197+
Message messages[while(!std::mem::eof() && !corrupted)];
193198
};
194199

195200

0 commit comments

Comments
 (0)