From 51047367f4e29694f0a5b78f5c418826d6b5a09e Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Mon, 10 Apr 2023 23:07:18 -0700 Subject: [PATCH] fix index out of bounds error when reading gems --- docs/changelog.txt | 2 ++ plugins/lua/stockpiles.lua | 2 +- plugins/stockpiles/StockpileSerializer.cpp | 20 ++++++++++++-------- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index 8beb3dc6e..7c52315ea 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -42,6 +42,8 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - `blueprint`: interpret saplings, shrubs, and twigs as floors instead of walls - `combine`: fix error processing stockpiles with boundaries that extend outside of the map - `prospector`: display both "raw" Z levels and "cooked" elevations +- `stockpiles`: fix crash when importing settings for gems from other worlds +- `stockpiles`: allow numbers in saved stockpile filenames ## Misc Improvements - `buildingplan`: items in the item selection dialog should now use the same item quality symbols as the base game diff --git a/plugins/lua/stockpiles.lua b/plugins/lua/stockpiles.lua index 6134f0a1b..9e17cbe2b 100644 --- a/plugins/lua/stockpiles.lua +++ b/plugins/lua/stockpiles.lua @@ -62,7 +62,7 @@ local function assert_safe_name(name) if not name or #name == 0 then qerror('name missing or empty') end - if name:find('[^%a._]') then + if name:find('[^%w._]') then qerror('name can only contain numbers, letters, periods, and underscores') end end diff --git a/plugins/stockpiles/StockpileSerializer.cpp b/plugins/stockpiles/StockpileSerializer.cpp index f6a63ff4f..529e402bd 100644 --- a/plugins/stockpiles/StockpileSerializer.cpp +++ b/plugins/stockpiles/StockpileSerializer.cpp @@ -1848,7 +1848,7 @@ void StockpileSerializer::read_gems(DeserializeMode mode, const vector& unserialize_list_material("mats/rough", all, val, filters, gem_mat_is_allowed, [&](const size_t& idx) -> const string& { return bgems.rough_mats(idx); }, - bgems.rough_mats_size(),pgems.rough_mats); + bgems.rough_mats_size(), pgems.rough_mats); unserialize_list_material("mats/cut", all, val, filters, gem_cut_mat_is_allowed, [&](const size_t& idx) -> const string& { return bgems.cut_mats(idx); }, @@ -1871,13 +1871,17 @@ void StockpileSerializer::read_gems(DeserializeMode mode, const vector& return; } else { MaterialInfo mi; - for (size_t i = 0; i < builtin_size; ++i) { - string id = bgems.rough_other_mats(i); - if (mi.find(id) && mi.isValid() && size_t(mi.type) < builtin_size) - set_filter_elem("other/rough", filters, val, id, mi.type, pgems.rough_other_mats.at(mi.type)); - id = bgems.cut_other_mats(i); - if (mi.find(id) && mi.isValid() && size_t(mi.type) < builtin_size) - set_filter_elem("other/cut", filters, val, id, mi.type, pgems.cut_other_mats.at(mi.type)); + for (int i = 0; i < (int)builtin_size; ++i) { + if (i < bgems.rough_other_mats_size()) { + string id = bgems.rough_other_mats(i); + if (mi.find(id) && mi.isValid() && size_t(mi.type) < builtin_size) + set_filter_elem("other/rough", filters, val, id, mi.type, pgems.rough_other_mats.at(mi.type)); + } + if (i < bgems.cut_other_mats_size()) { + string id = bgems.cut_other_mats(i); + if (mi.find(id) && mi.isValid() && size_t(mi.type) < builtin_size) + set_filter_elem("other/cut", filters, val, id, mi.type, pgems.cut_other_mats.at(mi.type)); + } } } });