Fix out of bounds vector access in itemdesignator/itemdump

develop
Petr Mrázek 2010-03-14 00:36:09 +01:00
parent f72bb0373d
commit 2bd68b9c58
2 changed files with 136 additions and 38 deletions

@ -26,7 +26,14 @@ string getMaterialType(DFHack::t_item item, const string & itemtype,const matGlo
// plant thread seeds // plant thread seeds
if(itemtype == "item_plant" || itemtype == "item_thread" || itemtype == "item_seeds" || itemtype == "item_leaves" ) if(itemtype == "item_plant" || itemtype == "item_thread" || itemtype == "item_seeds" || itemtype == "item_leaves" )
{ {
return mat.plantMat[item.material.type].id; if(item.material.type >= 0 && item.material.type < mat.plantMat.size())
{
return mat.plantMat[item.material.type].id;
}
else
{
return "some invalid plant material";
}
} }
else if (itemtype == "item_drink") // drinks must have different offset for materials else if (itemtype == "item_drink") // drinks must have different offset for materials
{ {
@ -45,15 +52,21 @@ string getMaterialType(DFHack::t_item item, const string & itemtype,const matGlo
itemtype == "item_meat" itemtype == "item_meat"
) )
{ {
return mat.creatureMat[item.material.type].id; if(item.material.type >= 0 && item.material.type < mat.creatureMat.size())
return mat.creatureMat[item.material.type].id;
return "bad material";
} }
else if(itemtype == "item_wood") else if(itemtype == "item_wood")
{ {
return mat.woodMat[item.material.type].id; if(item.material.type >= 0 && item.material.type < mat.woodMat.size())
return mat.woodMat[item.material.type].id;
return "bad material";
} }
else if(itemtype == "item_bar") else if(itemtype == "item_bar")
{ {
return mat.metalMat[item.material.type].id; if(item.material.type >= 0 && item.material.type < mat.metalMat.size())
return mat.metalMat[item.material.type].id;
return "bad material";
} }
else else
{ {
@ -78,49 +91,85 @@ string getMaterialType(DFHack::t_item item, const string & itemtype,const matGlo
switch (item.material.type) switch (item.material.type)
{ {
case DFHack::Mat_Wood: case DFHack::Mat_Wood:
return mat.woodMat[item.material.index].id; if(item.material.index >= 0 && item.material.index < mat.woodMat.size())
break; return mat.woodMat[item.material.index].id;
return "bad material";
case DFHack::Mat_Stone: case DFHack::Mat_Stone:
return mat.stoneMat[item.material.index].id; if(item.material.index >= 0 && item.material.index < mat.stoneMat.size())
break; return mat.stoneMat[item.material.index].id;
return "bad material";
case DFHack::Mat_Metal: case DFHack::Mat_Metal:
return mat.metalMat[item.material.index].id; if(item.material.index >= 0 && item.material.index < mat.metalMat.size())
break; return mat.metalMat[item.material.index].id;
return "bad material";
//case DFHack::Mat_Plant: //case DFHack::Mat_Plant:
case DFHack::Mat_PlantCloth: case DFHack::Mat_PlantCloth:
//return mat.plantMat[item.material.index].id; //return mat.plantMat[item.material.index].id;
return string(mat.plantMat[item.material.index].id) + " plant"; if(item.material.index >= 0 && item.material.index < mat.plantMat.size())
break; return string(mat.plantMat[item.material.index].id) + " plant";
return "bad material";
case 3: // bone case 3: // bone
return string(mat.creatureMat[item.material.index].id) + " bone"; if(item.material.index >= 0 && item.material.index < mat.creatureMat.size())
return string(mat.creatureMat[item.material.index].id) + " bone";
return "bad material";
case 25: // fat case 25: // fat
return string(mat.creatureMat[item.material.index].id) + " fat"; if(item.material.index >= 0 && item.material.index < mat.creatureMat.size())
return string(mat.creatureMat[item.material.index].id) + " fat";
return "bad material";
case 23: // tallow case 23: // tallow
return string(mat.creatureMat[item.material.index].id) + " tallow"; if(item.material.index >= 0 && item.material.index < mat.creatureMat.size())
return string(mat.creatureMat[item.material.index].id) + " tallow";
return "bad material";
case 9: // shell case 9: // shell
return string(mat.creatureMat[item.material.index].id) + " shell"; if(item.material.index >= 0 && item.material.index < mat.creatureMat.size())
return string(mat.creatureMat[item.material.index].id) + " shell";
return "bad material";
case DFHack::Mat_Leather: // really a generic creature material. meat for item_food, leather for item_box... case DFHack::Mat_Leather: // really a generic creature material. meat for item_food, leather for item_box...
return string(mat.creatureMat[item.material.index].id); if(item.material.index >= 0 && item.material.index < mat.creatureMat.size())
return string(mat.creatureMat[item.material.index].id);
return "bad material";
case DFHack::Mat_SilkCloth: case DFHack::Mat_SilkCloth:
return string(mat.creatureMat[item.material.index].id) + " silk"; if(item.material.index >= 0 && item.material.index < mat.creatureMat.size())
return string(mat.creatureMat[item.material.index].id) + " silk";
return "bad material";
case DFHack::Mat_Soap: case DFHack::Mat_Soap:
return string(mat.creatureMat[item.material.index].id) + " soap"; if(item.material.index >= 0 && item.material.index < mat.creatureMat.size())
return string(mat.creatureMat[item.material.index].id) + " soap";
return "bad material";
case DFHack::Mat_GreenGlass: case DFHack::Mat_GreenGlass:
return "Green Glass"; return "Green Glass";
case DFHack::Mat_ClearGlass: case DFHack::Mat_ClearGlass:
return "Clear Glass"; return "Clear Glass";
case DFHack::Mat_CrystalGlass: case DFHack::Mat_CrystalGlass:
return "Crystal Glass"; return "Crystal Glass";
case DFHack::Mat_Ice: case DFHack::Mat_Ice:
return "Ice"; return "Ice";
case DFHack::Mat_Charcoal: case DFHack::Mat_Charcoal:
return "Charcoal"; return "Charcoal";
/*case DFHack::Mat_Potash: /*case DFHack::Mat_Potash:
return "Potash";*/ return "Potash";*/
case DFHack::Mat_Ashes: case DFHack::Mat_Ashes:
return "Ashes"; return "Ashes";
case DFHack::Mat_PearlAsh: case DFHack::Mat_PearlAsh:
return "Pearlash"; return "Pearlash";
default: default:
cout << "unknown material hit: " << item.material.type << " " << item.material.index << " " << itemtype << endl; cout << "unknown material hit: " << item.material.type << " " << item.material.index << " " << itemtype << endl;
return "Invalid"; return "Invalid";

@ -26,7 +26,14 @@ string getMaterialType(DFHack::t_item item, const string & itemtype,const matGlo
// plant thread seeds // plant thread seeds
if(itemtype == "item_plant" || itemtype == "item_thread" || itemtype == "item_seeds" || itemtype == "item_leaves" ) if(itemtype == "item_plant" || itemtype == "item_thread" || itemtype == "item_seeds" || itemtype == "item_leaves" )
{ {
return mat.plantMat[item.material.type].id; if(item.material.type >= 0 && item.material.type < mat.plantMat.size())
{
return mat.plantMat[item.material.type].id;
}
else
{
return "some invalid plant material";
}
} }
else if (itemtype == "item_drink") // drinks must have different offset for materials else if (itemtype == "item_drink") // drinks must have different offset for materials
{ {
@ -45,15 +52,21 @@ string getMaterialType(DFHack::t_item item, const string & itemtype,const matGlo
itemtype == "item_meat" itemtype == "item_meat"
) )
{ {
return mat.creatureMat[item.material.type].id; if(item.material.type >= 0 && item.material.type < mat.creatureMat.size())
return mat.creatureMat[item.material.type].id;
return "bad material";
} }
else if(itemtype == "item_wood") else if(itemtype == "item_wood")
{ {
return mat.woodMat[item.material.type].id; if(item.material.type >= 0 && item.material.type < mat.woodMat.size())
return mat.woodMat[item.material.type].id;
return "bad material";
} }
else if(itemtype == "item_bar") else if(itemtype == "item_bar")
{ {
return mat.metalMat[item.material.type].id; if(item.material.type >= 0 && item.material.type < mat.metalMat.size())
return mat.metalMat[item.material.type].id;
return "bad material";
} }
else else
{ {
@ -78,49 +91,85 @@ string getMaterialType(DFHack::t_item item, const string & itemtype,const matGlo
switch (item.material.type) switch (item.material.type)
{ {
case DFHack::Mat_Wood: case DFHack::Mat_Wood:
return mat.woodMat[item.material.index].id; if(item.material.index >= 0 && item.material.index < mat.woodMat.size())
break; return mat.woodMat[item.material.index].id;
return "bad material";
case DFHack::Mat_Stone: case DFHack::Mat_Stone:
return mat.stoneMat[item.material.index].id; if(item.material.index >= 0 && item.material.index < mat.stoneMat.size())
break; return mat.stoneMat[item.material.index].id;
return "bad material";
case DFHack::Mat_Metal: case DFHack::Mat_Metal:
return mat.metalMat[item.material.index].id; if(item.material.index >= 0 && item.material.index < mat.metalMat.size())
break; return mat.metalMat[item.material.index].id;
return "bad material";
//case DFHack::Mat_Plant: //case DFHack::Mat_Plant:
case DFHack::Mat_PlantCloth: case DFHack::Mat_PlantCloth:
//return mat.plantMat[item.material.index].id; //return mat.plantMat[item.material.index].id;
return string(mat.plantMat[item.material.index].id) + " plant"; if(item.material.index >= 0 && item.material.index < mat.plantMat.size())
break; return string(mat.plantMat[item.material.index].id) + " plant";
return "bad material";
case 3: // bone case 3: // bone
return string(mat.creatureMat[item.material.index].id) + " bone"; if(item.material.index >= 0 && item.material.index < mat.creatureMat.size())
return string(mat.creatureMat[item.material.index].id) + " bone";
return "bad material";
case 25: // fat case 25: // fat
return string(mat.creatureMat[item.material.index].id) + " fat"; if(item.material.index >= 0 && item.material.index < mat.creatureMat.size())
return string(mat.creatureMat[item.material.index].id) + " fat";
return "bad material";
case 23: // tallow case 23: // tallow
return string(mat.creatureMat[item.material.index].id) + " tallow"; if(item.material.index >= 0 && item.material.index < mat.creatureMat.size())
return string(mat.creatureMat[item.material.index].id) + " tallow";
return "bad material";
case 9: // shell case 9: // shell
return string(mat.creatureMat[item.material.index].id) + " shell"; if(item.material.index >= 0 && item.material.index < mat.creatureMat.size())
return string(mat.creatureMat[item.material.index].id) + " shell";
return "bad material";
case DFHack::Mat_Leather: // really a generic creature material. meat for item_food, leather for item_box... case DFHack::Mat_Leather: // really a generic creature material. meat for item_food, leather for item_box...
return string(mat.creatureMat[item.material.index].id); if(item.material.index >= 0 && item.material.index < mat.creatureMat.size())
return string(mat.creatureMat[item.material.index].id);
return "bad material";
case DFHack::Mat_SilkCloth: case DFHack::Mat_SilkCloth:
return string(mat.creatureMat[item.material.index].id) + " silk"; if(item.material.index >= 0 && item.material.index < mat.creatureMat.size())
return string(mat.creatureMat[item.material.index].id) + " silk";
return "bad material";
case DFHack::Mat_Soap: case DFHack::Mat_Soap:
return string(mat.creatureMat[item.material.index].id) + " soap"; if(item.material.index >= 0 && item.material.index < mat.creatureMat.size())
return string(mat.creatureMat[item.material.index].id) + " soap";
return "bad material";
case DFHack::Mat_GreenGlass: case DFHack::Mat_GreenGlass:
return "Green Glass"; return "Green Glass";
case DFHack::Mat_ClearGlass: case DFHack::Mat_ClearGlass:
return "Clear Glass"; return "Clear Glass";
case DFHack::Mat_CrystalGlass: case DFHack::Mat_CrystalGlass:
return "Crystal Glass"; return "Crystal Glass";
case DFHack::Mat_Ice: case DFHack::Mat_Ice:
return "Ice"; return "Ice";
case DFHack::Mat_Charcoal: case DFHack::Mat_Charcoal:
return "Charcoal"; return "Charcoal";
/*case DFHack::Mat_Potash: /*case DFHack::Mat_Potash:
return "Potash";*/ return "Potash";*/
case DFHack::Mat_Ashes: case DFHack::Mat_Ashes:
return "Ashes"; return "Ashes";
case DFHack::Mat_PearlAsh: case DFHack::Mat_PearlAsh:
return "Pearlash"; return "Pearlash";
default: default:
cout << "unknown material hit: " << item.material.type << " " << item.material.index << " " << itemtype << endl; cout << "unknown material hit: " << item.material.type << " " << item.material.index << " " << itemtype << endl;
return "Invalid"; return "Invalid";