Permalink
Browse files

GLK: FROTZ: Fix #10869 Crash initializing undo data

  • Loading branch information...
dreammaster committed Jan 6, 2019
1 parent fe96c94 commit 8515590b47ca297880df59f81f305b00b7c5384a
Showing with 47 additions and 39 deletions.
  1. +38 −35 engines/glk/frotz/mem.cpp
  2. +9 −4 engines/glk/frotz/mem.h
@@ -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) {
@@ -72,14 +72,19 @@ class Mem : public Header, public virtual UserOptions {
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).

0 comments on commit 8515590

Please sign in to comment.