Skip to content

Commit bd91c59

Browse files
committed
Raise CorruptLogError when NUL bytes are present
1 parent fe0f992 commit bd91c59

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

hslog/exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ class RegexParsingError(ParsingError):
1414
pass
1515

1616

17+
class CorruptLogError(ParsingError):
18+
"""
19+
Raised when a log is obviously corrupt, for example when containing NUL (0x00) bytes.
20+
"""
21+
pass
22+
23+
1724
class ExporterError(Exception):
1825
"""
1926
Generic exception that happens during PacketTree export.

hslog/parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
)
1010

1111
from . import packets, tokens
12-
from .exceptions import ParsingError, RegexParsingError
12+
from .exceptions import CorruptLogError, ParsingError, RegexParsingError
1313
from .packets import (
1414
Block, Choices, ChosenEntities, CreateGame,
1515
MetaData, Packet, PacketTree, SendChoices, SubSpell
@@ -327,6 +327,8 @@ def handle_data(self, ps: ParsingState, ts, data):
327327
return
328328
ps.current_block.targets.append(entity)
329329
else:
330+
if "\x00" in data:
331+
raise CorruptLogError("Log contains contains a NUL (0x00) byte")
330332
raise NotImplementedError(data)
331333

332334
def handle_power(self, ps: ParsingState, ts, opcode, data):

tests/data.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,31 @@
243243
SHUFFLE_DECK = """
244244
D 17:21:14.4464414 GameState.DebugPrintPower() - SHUFFLE_DECK PlayerID=2
245245
""".strip()
246+
247+
248+
CORRUPT_SHOW_ENTITY = """
249+
D 13:24:38.7829954 GameState.DebugPrintPower() - SHOW_ENTITY - Updating Entity=9537 CardID=BG21_017
250+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=CONTROLLER value=11
251+
D 13:24:38.7829954 GameState.DebugPrintPower() - \x01\x00\x10\x00\x01 \x10\x00\x03\x00\x10\x00 tag=CARDTYPE value=MINION
252+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=TAG_LAST_KNOWN_COST_IN_HAND value=4
253+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=479 value=4
254+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=COST value=4
255+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=ATK value=4
256+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=HEALTH value=4
257+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=TRIGGER_VISUAL value=1
258+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=ZONE value=PLAY
259+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=ENTITY_ID value=9537
260+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=CARDRACE value=PIRATE
261+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=CREATOR value=9508
262+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=1037 value=3
263+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=1067 value=0
264+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=1068 value=0
265+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=CREATOR_DBID value=72230
266+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=1429 value=73951
267+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=TECH_LEVEL value=3
268+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=IS_BACON_POOL_MINION value=1
269+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=SUPPRESS_ALL_SUMMON_VO value=1
270+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=SPAWN_TIME_COUNT value=1
271+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=COPIED_FROM_ENTITY_ID value=9531
272+
D 13:24:38.7829954 GameState.DebugPrintPower() - tag=1596 value=1
273+
""".strip()

tests/test_parser.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
)
1111

1212
from hslog import LogParser, packets
13-
from hslog.exceptions import ParsingError
13+
from hslog.exceptions import CorruptLogError, ParsingError
1414
from hslog.parser import parse_initial_tag
1515

1616
from . import data
@@ -516,3 +516,13 @@ def test_shuffle_deck_only(self):
516516
shuffle_deck_packet = packet_tree.packets[1]
517517

518518
assert shuffle_deck_packet.player_id == 2
519+
520+
def test_nul_byte(self):
521+
parser = LogParser()
522+
parser.read(StringIO(data.INITIAL_GAME))
523+
524+
with pytest.raises(
525+
CorruptLogError,
526+
match=r"Log contains contains a NUL \(0x00\) byte"
527+
):
528+
parser.read(StringIO(data.CORRUPT_SHOW_ENTITY))

0 commit comments

Comments
 (0)