From 8515590b47ca297880df59f81f305b00b7c5384a Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Sun, 6 Jan 2019 14:00:53 -0800 Subject: GLK: FROTZ: Fix #10869 Crash initializing undo data --- engines/glk/frotz/mem.cpp | 73 ++++++++++++++++++++++++----------------------- engines/glk/frotz/mem.h | 13 ++++++--- 2 files changed, 47 insertions(+), 39 deletions(-) (limited to 'engines/glk') diff --git a/engines/glk/frotz/mem.cpp b/engines/glk/frotz/mem.cpp index 227b67358d..ea3ca183c7 100644 --- a/engines/glk/frotz/mem.cpp +++ b/engines/glk/frotz/mem.cpp @@ -36,8 +36,44 @@ Mem::Mem() : story_fp(nullptr), story_size(0), first_undo(nullptr), last_undo(nu void Mem::initialize() { initializeStoryFile(); loadGameHeader(); + loadMemory(); initializeUndo(); + // Read header extension table + hx_table_size = get_header_extension(HX_TABLE_SIZE); + hx_unicode_table = get_header_extension(HX_UNICODE_TABLE); + hx_flags = get_header_extension(HX_FLAGS); +} + +void Mem::initializeStoryFile() { + if (story_fp->size() < 64) + error("This file is too small to be a Z-code file."); +} + +void Mem::loadGameHeader() { + // Load header + zmp = (byte *)malloc(64); + story_fp->seek(0); + story_fp->read(zmp, 64); + + Common::MemoryReadStream h(zmp, 64); + loadHeader(h); + + // Calculate story file size in bytes + if (h_file_size != 0) { + story_size = (long)2 * h_file_size; + + if (h_version >= V4) + story_size *= 2; + if (h_version >= V6) + story_size *= 2; + } else { + // Some old games lack the file size entry + story_size = story_fp->size(); + } +} + +void Mem::loadMemory() { // Allocate memory for story data if ((zmp = (zbyte *)realloc(zmp, story_size)) == nullptr) error("Out of memory"); @@ -51,16 +87,6 @@ void Mem::initialize() { if (story_fp->read(zmp + size, n) != n) error("Story file read error"); } - - // Read header extension table - hx_table_size = get_header_extension(HX_TABLE_SIZE); - hx_unicode_table = get_header_extension(HX_UNICODE_TABLE); - hx_flags = get_header_extension(HX_FLAGS); -} - -void Mem::initializeStoryFile() { - if (story_fp->size() < 64) - error("This file is too small to be a Z-code file."); } void Mem::initializeUndo() { @@ -82,31 +108,8 @@ void Mem::initializeUndo() { _undo_slots = 0; } - if (reserve_mem != 0) - delete reserved; -} - -void Mem::loadGameHeader() { - // Load header - zmp = (byte *)malloc(64); - story_fp->seek(0); - story_fp->read(zmp, 64); - - Common::MemoryReadStream h(zmp, 64); - loadHeader(h); - - // Calculate story file size in bytes - if (h_file_size != 0) { - story_size = (long)2 * h_file_size; - - if (h_version >= V4) - story_size *= 2; - if (h_version >= V6) - story_size *= 2; - } else { - // Some old games lack the file size entry - story_size = story_fp->size(); - } + if (reserve_mem) + delete[] reserved; } zword Mem::get_header_extension(int entry) { diff --git a/engines/glk/frotz/mem.h b/engines/glk/frotz/mem.h index a632bc6e63..c58ef658e4 100644 --- a/engines/glk/frotz/mem.h +++ b/engines/glk/frotz/mem.h @@ -72,14 +72,19 @@ private: void initializeStoryFile(); /** - * Setup undo data + * Handles loading the game header */ - void initializeUndo(); + void loadGameHeader(); /** - * Handles loading the game header + * Initializes memory and loads the story data */ - void loadGameHeader(); + void loadMemory(); + + /** + * Setup undo data + */ + void initializeUndo(); protected: /** * Read a value from the header extension (former mouse table). -- cgit v1.2.3