From 381bd5e9a73cd75618a8a5a13f272927aa402187 Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Sun, 3 Nov 2019 09:48:24 -0800 Subject: GLK: ARCHETYPE: Further array indexing fixes --- engines/glk/archetype/archetype.cpp | 9 ++++----- engines/glk/archetype/array.cpp | 5 ++++- engines/glk/archetype/game_stat.cpp | 8 ++++---- engines/glk/archetype/heap_sort.cpp | 10 +++++----- engines/glk/archetype/id_table.cpp | 4 ++-- engines/glk/archetype/keywords.cpp | 4 ++-- engines/glk/archetype/linked_list.cpp | 6 ++---- engines/glk/archetype/linked_list.h | 2 +- engines/glk/archetype/saveload.cpp | 6 +++--- engines/glk/archetype/semantic.cpp | 6 +++--- engines/glk/archetype/token.cpp | 6 +++--- 11 files changed, 33 insertions(+), 33 deletions(-) (limited to 'engines') diff --git a/engines/glk/archetype/archetype.cpp b/engines/glk/archetype/archetype.cpp index e7e8da210d..d17f0c753c 100644 --- a/engines/glk/archetype/archetype.cpp +++ b/engines/glk/archetype/archetype.cpp @@ -873,7 +873,7 @@ void Archetype::exec_stmt(StatementPtr the_stmt, ResultType &result, ContextType c = context; c.each = 1; - while (!b && c.each < (int)Object_List.size()) { + while (!b && c.each <= (int)Object_List.size()) { if (eval_condition(the_stmt->_data._loop.selection, c)) { exec_stmt(the_stmt->_data._loop.action, result, c); b = (result._kind == RESERVED) && (result._data._reserved.keyword == RW_BREAK); @@ -903,8 +903,7 @@ void Archetype::exec_stmt(StatementPtr the_stmt, ResultType &result, ContextType if (!assignment(r1, result)) { cleanup(result); - } - else { + } else { // do it for real the_object = new ObjectType(); the_object->inherited_from = the_stmt->_data._create.archetype; @@ -919,7 +918,7 @@ void Archetype::exec_stmt(StatementPtr the_stmt, ResultType &result, ContextType while (access_xarray(Object_List, i, q, PEEK_ACCESS) && q != nullptr) ++i; - if (i >= (int)Object_List.size()) + if (i > (int)Object_List.size()) append_to_xarray(Object_List, p); else b = access_xarray(Object_List, i, p, POKE_ACCESS); @@ -943,7 +942,7 @@ void Archetype::exec_stmt(StatementPtr the_stmt, ResultType &result, ContextType dispose_object(the_object); p = nullptr; b = access_xarray(Object_List, result._data._ident.ident_int, p, POKE_ACCESS); - if (result._data._ident.ident_int == ((int)Object_List.size() - 1)) + if (result._data._ident.ident_int == (int)Object_List.size()) shrink_xarray(Object_List); } else { wraperr("Can only destroy previously created objects"); diff --git a/engines/glk/archetype/array.cpp b/engines/glk/archetype/array.cpp index 79a486faf1..33147d1a9a 100644 --- a/engines/glk/archetype/array.cpp +++ b/engines/glk/archetype/array.cpp @@ -38,7 +38,10 @@ void append_to_xarray(XArrayType &the_xarray, void *element) { } bool access_xarray(XArrayType &the_xarray, int index, void *&result, AccessType direction) { - if (index < 1 || index > (int)the_xarray.size()) + if (index <= 0) + error("Invalid index - double check arrays were 1 based in original"); + + if (index > (int)the_xarray.size()) return false; switch (direction) { diff --git a/engines/glk/archetype/game_stat.cpp b/engines/glk/archetype/game_stat.cpp index 9f6a8b85d6..015065a3ed 100644 --- a/engines/glk/archetype/game_stat.cpp +++ b/engines/glk/archetype/game_stat.cpp @@ -40,7 +40,7 @@ void save_game_state(Common::WriteStream *bfile, XArrayType &objects) { // Get the encryption straight - reset the seed cryptinit(Encryption, GTimeStamp); - for (i = 0; i < Dynamic - 1; ++i) { + for (i = 1; i < Dynamic; ++i) { if (index_xarray(objects, i, p)) { ObjectPtr op = (ObjectPtr)p; bfile->writeUint32LE(vContSeq); @@ -49,7 +49,7 @@ void save_game_state(Common::WriteStream *bfile, XArrayType &objects) { } } - for (i = Dynamic; i < (int)objects.size(); ++i) { + for (i = Dynamic; i <= (int)objects.size(); ++i) { if (index_xarray(objects, i, p)) { bfile->writeUint32LE(vContSeq); dump_object(bfile, (ObjectPtr)p); @@ -81,7 +81,7 @@ bool load_game_state(Common::ReadStream *bfile, XArrayType &objects) { // Need to flush out the previous attributes andload in the new ones. Dynamically allocated // objects are a little different since they might vary between game states - for (i = 0; i < Dynamic - 1; ++i) { + for (i = 1; i < Dynamic; ++i) { if (index_xarray(objects, i, p)) { sentinel = (StatementKind)bfile->readUint32LE(); op = (ObjectPtr)p; @@ -92,7 +92,7 @@ bool load_game_state(Common::ReadStream *bfile, XArrayType &objects) { // Flush dynamic objects.Dispose of each object andshrink back the xarray - for (i = objects.size() - 1; i >= Dynamic; --i) { + for (i = objects.size(); i >= Dynamic; --i) { if (index_xarray(objects, i, p)) { op = (ObjectPtr)p; dispose_object(op); diff --git a/engines/glk/archetype/heap_sort.cpp b/engines/glk/archetype/heap_sort.cpp index 98753ccc70..2f0edd27fb 100644 --- a/engines/glk/archetype/heap_sort.cpp +++ b/engines/glk/archetype/heap_sort.cpp @@ -46,7 +46,7 @@ static void heapup() { Element temp; L = H.size(); - while (L > 0) { + while (L > 1) { if ((L % 2) == 0) parent = L / 2; else @@ -73,17 +73,17 @@ static void heapdown() { Element comparep; Element temp; - L = 0; + L = 1; while (L < H.size()) { lc = L * 2; - if (lc >= H.size()) { + if (lc > H.size()) { L = lc; } else { rc = lc + 1; if (!access_xarray(H, lc, lcp, PEEK_ACCESS)) g_vm->writeln(CANT_PEEK); - if (rc >= H.size()) { + if (rc > H.size()) { compare = lc; comparep = lcp; } else { @@ -106,7 +106,7 @@ static void heapdown() { g_vm->writeln(CANT_POKE); L = compare; } else { - L = H.size(); + L = H.size() + 1; } } } diff --git a/engines/glk/archetype/id_table.cpp b/engines/glk/archetype/id_table.cpp index a611503b71..5bae5112ed 100644 --- a/engines/glk/archetype/id_table.cpp +++ b/engines/glk/archetype/id_table.cpp @@ -49,13 +49,13 @@ int add_ident(const String &id_str) { append_to_xarray(h_index, new_rec); new_rec->id_kind = DefaultClassification; - new_rec->id_index = h_index.size() - 1; + new_rec->id_index = h_index.size(); new_rec->id_integer = new_rec->id_index; new_rec->id_name = NewConstStr(id_str); new_rec->next = p->next; p->next = new_rec; - return h_index.size() - 1; + return h_index.size(); } else { // found existing identifier return p->next->id_index; diff --git a/engines/glk/archetype/keywords.cpp b/engines/glk/archetype/keywords.cpp index 97cac8457f..05d6bcacb5 100644 --- a/engines/glk/archetype/keywords.cpp +++ b/engines/glk/archetype/keywords.cpp @@ -120,7 +120,7 @@ void dump_text_list(Common::WriteStream *fOut, XArrayType &the_list) { void *p; fOut->writeUint16LE(the_list.size()); - for (uint i = 0; i < the_list.size(); ++i) { + for (uint i = 1; i <= the_list.size(); ++i) { if (index_xarray(the_list, i, p)) dump_string(fOut, *(StringPtr)(p)); } @@ -129,7 +129,7 @@ void dump_text_list(Common::WriteStream *fOut, XArrayType &the_list) { void dispose_text_list(XArrayType &the_list) { void *p; - for (uint i = 0; i < the_list.size(); ++i) { + for (uint i = 1; i <= the_list.size(); ++i) { if (index_xarray(the_list, i, p)) delete (StringPtr)p; } diff --git a/engines/glk/archetype/linked_list.cpp b/engines/glk/archetype/linked_list.cpp index eef55b4e01..0965e65bde 100644 --- a/engines/glk/archetype/linked_list.cpp +++ b/engines/glk/archetype/linked_list.cpp @@ -29,9 +29,7 @@ namespace Archetype { void new_list(ListType &the_list) { the_list = new NodeType(); add_bytes(sizeof(NodeType)); - the_list->key = 0; - the_list->data = nullptr; - the_list->next = nullptr; + the_list->next = the_list; } void dispose_list(ListType &the_list) { @@ -45,7 +43,7 @@ void dispose_list(ListType &the_list) { } -bool iterate_list(ListType &the_list, NodePtr index) { +bool iterate_list(ListType &the_list, NodePtr &index) { if (index == nullptr) index = the_list->next; else diff --git a/engines/glk/archetype/linked_list.h b/engines/glk/archetype/linked_list.h index e0f4b08cd2..6f8fea84d8 100644 --- a/engines/glk/archetype/linked_list.h +++ b/engines/glk/archetype/linked_list.h @@ -53,7 +53,7 @@ extern void dispose_list(ListType &the_list); /** * Iterates through the given list */ -extern bool iterate_list(ListType &the_list, NodePtr index); +extern bool iterate_list(ListType &the_list, NodePtr &index); /** * Appends a new item to the list diff --git a/engines/glk/archetype/saveload.cpp b/engines/glk/archetype/saveload.cpp index 7c47d77b8d..61069e3161 100644 --- a/engines/glk/archetype/saveload.cpp +++ b/engines/glk/archetype/saveload.cpp @@ -633,7 +633,7 @@ void load_obj_list(Common::ReadStream *f_in, XArrayType &obj_list) { // Objects may be dynamically allocated beneath this limit. It is okay to set that limit // at this time since this routine is only invoked when initially loading a game - Dynamic = obj_list.size(); // TODO: Check if this should be size() + 1 + Dynamic = obj_list.size() + 1; } void dump_obj_list(Common::WriteStream *f_out, XArrayType &obj_list) { @@ -643,7 +643,7 @@ void dump_obj_list(Common::WriteStream *f_out, XArrayType &obj_list) { f_out->writeUint16LE(obj_list.size()); - for (i = 0; i < obj_list.size(); ++i) { + for (i = 1; i <= obj_list.size(); ++i) { if (index_xarray(obj_list, i, p)) { this_obj = (ObjectPtr)p; dump_object(f_out, this_obj); @@ -656,7 +656,7 @@ void dispose_obj_list(XArrayType &obj_list) { void *p; ObjectPtr axe_obj; - for (i = 0; i < obj_list.size(); ++i) { + for (i = 1; i <= obj_list.size(); ++i) { if (index_xarray(obj_list, i, p)) { axe_obj = (ObjectPtr)p; dispose_object(axe_obj); diff --git a/engines/glk/archetype/semantic.cpp b/engines/glk/archetype/semantic.cpp index 9e0660faf8..5f84adeef7 100644 --- a/engines/glk/archetype/semantic.cpp +++ b/engines/glk/archetype/semantic.cpp @@ -52,7 +52,7 @@ int classify_as(progfile &f, int id_number, ClassifyType interpretation, void *p case TYPE_ID: append_to_xarray(g_vm->Type_List, ptr_to_data); append_to_xarray(g_vm->Type_ID_List, (void *)the_id_ptr->id_name); - the_id_ptr->id_integer = g_vm->Type_List.size() - 1; + the_id_ptr->id_integer = g_vm->Type_List.size(); break; case OBJECT_ID: @@ -66,13 +66,13 @@ int classify_as(progfile &f, int id_number, ClassifyType interpretation, void *p append_to_xarray(g_vm->Object_List, ptr_to_data); append_to_xarray(g_vm->Object_ID_List, (void *)the_id_ptr->id_name); - the_id_ptr->id_integer = g_vm->Object_List.size() - 1; + the_id_ptr->id_integer = g_vm->Object_List.size(); } break; case ATTRIBUTE_ID: append_to_xarray(g_vm->Attribute_ID_List, (void *)the_id_ptr->id_name); - the_id_ptr->id_integer = g_vm->Attribute_ID_List.size() - 1; + the_id_ptr->id_integer = g_vm->Attribute_ID_List.size(); break; default: diff --git a/engines/glk/archetype/token.cpp b/engines/glk/archetype/token.cpp index 3be0e2365b..9d550a56d5 100644 --- a/engines/glk/archetype/token.cpp +++ b/engines/glk/archetype/token.cpp @@ -112,7 +112,7 @@ static int add_unique_str(XArrayType &the_xarray, const String &the_str) { if (the_xarray.empty()) { append_to_xarray(the_xarray, (void *)new_str); - return the_xarray.size() - 1; + return the_xarray.size(); } else { i = 1; while (index_xarray(the_xarray, i, p) && *((StringPtr)p) != the_str) @@ -123,7 +123,7 @@ static int add_unique_str(XArrayType &the_xarray, const String &the_str) { return i; } else { append_to_xarray(the_xarray, (void *)new_str); - return the_xarray.size() - 1; + return the_xarray.size(); } } } @@ -134,7 +134,7 @@ static int add_unique_str(XArrayType &the_xarray, const String &the_str) { */ static int add_non_unique_str(XArrayType &the_xarray, const String &the_str) { append_to_xarray(the_xarray, (void *)NewConstStr(the_str)); - return the_xarray.size() - 1; + return the_xarray.size(); } bool get_token(progfile &f) { -- cgit v1.2.3