From 602c6035e5f6178f6a1e6f5777cad0d73663167c Mon Sep 17 00:00:00 2001 From: Lemongrass3110 Date: Tue, 17 Nov 2020 11:51:01 +0100 Subject: [PATCH] Fixed some potential csv2yaml crashes (#5557) Fixes #5549 Thanks to @Atemo --- src/tool/csv2yaml.cpp | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/tool/csv2yaml.cpp b/src/tool/csv2yaml.cpp index fd9dd116ea..ca7a3cd68f 100644 --- a/src/tool/csv2yaml.cpp +++ b/src/tool/csv2yaml.cpp @@ -3074,11 +3074,33 @@ static bool itemdb_read_db(const char* file) { int type = atoi(str[3]), subtype = atoi(str[18]); - body << YAML::Key << "Type" << YAML::Value << name2Upper(constant_lookup(type, "IT_") + 3); - if (type == IT_WEAPON && subtype) - body << YAML::Key << "SubType" << YAML::Value << name2Upper(constant_lookup(subtype, "W_") + 2); - else if (type == IT_AMMO && subtype) - body << YAML::Key << "SubType" << YAML::Value << name2Upper(constant_lookup(subtype, "AMMO_") + 5); + const char* constant = constant_lookup( type, "IT_" ); + + if( constant == nullptr ){ + ShowError( "itemdb_read_db: Unknown item type %d for item %u, skipping.\n", type, nameid ); + continue; + } + + body << YAML::Key << "Type" << YAML::Value << name2Upper( constant + 3 ); + if( type == IT_WEAPON && subtype ){ + constant = constant_lookup( subtype, "W_" ); + + if( constant == nullptr ){ + ShowError( "itemdb_read_db: Unknown weapon type %d for item %u, skipping.\n", subtype, nameid ); + continue; + } + + body << YAML::Key << "SubType" << YAML::Value << name2Upper( constant + 2 ); + }else if( type == IT_AMMO && subtype ){ + constant = constant_lookup( subtype, "AMMO_" ); + + if( constant == nullptr ){ + ShowError( "itemdb_read_db: Unknown ammo type %d for item %u, skipping.\n", subtype, nameid ); + continue; + } + + body << YAML::Key << "SubType" << YAML::Value << name2Upper(constant + 5); + } if (atoi(str[4]) > 0) body << YAML::Key << "Buy" << YAML::Value << atoi(str[4]);