- check for empty strings before using substr to avoid crashes with more strict C++ engine

This commit is contained in:
SoftCoder
2017-09-30 21:46:56 -07:00
parent a7a92056eb
commit edfb1508f5
6 changed files with 49 additions and 53 deletions

View File

@@ -1167,23 +1167,24 @@ void Model::load(const string &path, bool deletePixMapAfterLoad,
if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) {
return; return;
} }
string extension= path.substr(path.find_last_of('.') + 1); size_t pos = path.find_last_of('.');
if(extension=="g3d" || extension=="G3D") { string extension = toLower(path.empty() == false ? path.substr(pos + 1) : "");
if(extension == "g3d") {
loadG3d(path,deletePixMapAfterLoad,loadedFileList, this->sourceLoader); loadG3d(path,deletePixMapAfterLoad,loadedFileList, this->sourceLoader);
} }
else { else {
throw megaglest_runtime_error("Unknown model format: " + extension); throw megaglest_runtime_error("#1 Unknown model format [" + extension + "] file [" + path + "]");
} }
} }
void Model::save(const string &path, string convertTextureToFormat, void Model::save(const string &path, string convertTextureToFormat,
bool keepsmallest) { bool keepsmallest) {
string extension= path.substr(path.find_last_of('.')+1); string extension = (path.empty() == false ? path.substr(path.find_last_of('.')+1) : "");
if(extension=="g3d" ||extension=="G3D") { if(toLower(extension) == "g3d") {
saveG3d(path,convertTextureToFormat,keepsmallest); saveG3d(path,convertTextureToFormat,keepsmallest);
} }
else { else {
throw megaglest_runtime_error("Unknown model format: " + extension); throw megaglest_runtime_error("#2 Unknown model format [" + extension + "] file [" + path + "]");
} }
} }

View File

@@ -815,15 +815,15 @@ Pixmap1D::~Pixmap1D(){
} }
void Pixmap1D::load(const string &path) { void Pixmap1D::load(const string &path) {
string extension= path.substr(path.find_last_of('.')+1); string extension = (path.empty() == false ? path.substr(path.find_last_of('.')+1) : "");
if(extension=="bmp") { if(toLower(extension) == "bmp") {
loadBmp(path); loadBmp(path);
} }
else if(extension=="tga") { else if(toLower(extension) == "tga") {
loadTga(path); loadTga(path);
} }
else { else {
throw megaglest_runtime_error("Unknown pixmap extension: " + extension); throw megaglest_runtime_error("Unknown pixmap extension [" + extension + "] file [" + path + "]");
} }
this->path = path; this->path = path;
CalculatePixelsCRC(pixels,getPixelByteCount(), crc); CalculatePixelsCRC(pixels,getPixelByteCount(), crc);
@@ -1017,7 +1017,7 @@ void Pixmap2D::load(const string &path) {
} }
void Pixmap2D::save(const string &path) { void Pixmap2D::save(const string &path) {
string extension= path.substr(path.find_last_of('.')+1); string extension = (path.empty() == false ? path.substr(path.find_last_of('.')+1) : "");
if(toLower(extension) == "bmp") { if(toLower(extension) == "bmp") {
saveBmp(path); saveBmp(path);
} }
@@ -1031,7 +1031,7 @@ void Pixmap2D::save(const string &path) {
savePng(path); savePng(path);
} }
else { else {
throw megaglest_runtime_error("Unknown pixmap extension: " + extension); throw megaglest_runtime_error("Unknown pixmap extension [" + extension + "] file [" + path + "]");
} }
} }
@@ -1500,18 +1500,18 @@ Pixmap3D::~Pixmap3D() {
void Pixmap3D::loadSlice(const string &path, int slice) { void Pixmap3D::loadSlice(const string &path, int slice) {
this->slice = slice; this->slice = slice;
string extension= path.substr(path.find_last_of('.') + 1); string extension = (path.empty() == false ? path.substr(path.find_last_of('.') + 1) : "");
if(extension == "png") { if(toLower(extension) == "png") {
loadSlicePng(path, slice); loadSlicePng(path, slice);
} }
else if(extension == "bmp") { else if(toLower(extension) == "bmp") {
loadSliceBmp(path, slice); loadSliceBmp(path, slice);
} }
else if(extension == "tga") { else if(toLower(extension) == "tga") {
loadSliceTga(path, slice); loadSliceTga(path, slice);
} }
else { else {
throw megaglest_runtime_error("Unknown pixmap extension: "+extension); throw megaglest_runtime_error("Unknown pixmap extension [" + extension + "] file [" + path + "]");
} }
this->path = path; this->path = path;
CalculatePixelsCRC(pixels,getPixelByteCount(), crc); CalculatePixelsCRC(pixels,getPixelByteCount(), crc);

View File

@@ -270,23 +270,6 @@ int64 Chrono::getCurTicks() {
// ===================================== // =====================================
void Tokenize(const string& str,vector<string>& tokens,const string& delimiters) { void Tokenize(const string& str,vector<string>& tokens,const string& delimiters) {
/*
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos) {
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
*/
// Assume textLine contains the line of text to parse. // Assume textLine contains the line of text to parse.
string textLine = str; string textLine = str;

View File

@@ -301,15 +301,17 @@ Ip::Ip(unsigned char byte0, unsigned char byte1, unsigned char byte2, unsigned c
} }
Ip::Ip(const string& ipString){ Ip::Ip(const string &ipString) {
size_t offset= 0; size_t offset= 0;
int byteIndex= 0; int byteIndex= 0;
for(byteIndex= 0; byteIndex<4; ++byteIndex){ if(ipString.empty() == false) {
size_t dotPos= ipString.find_first_of('.', offset); for(byteIndex= 0; byteIndex<4; ++byteIndex){
size_t dotPos= ipString.find_first_of('.', offset);
bytes[byteIndex]= atoi(ipString.substr(offset, dotPos-offset).c_str()); bytes[byteIndex]= atoi(ipString.substr(offset, dotPos-offset).c_str());
offset= dotPos+1; offset= dotPos+1;
}
} }
} }

View File

@@ -78,7 +78,7 @@ void StaticSound::load(const string &path) {
if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) {
return; return;
} }
string ext= path.substr(path.find_last_of('.')+1); string ext = (path.empty() == false ? path.substr(path.find_last_of('.')+1) : "");
soundFileLoader= SoundFileLoaderFactory::getInstance()->newInstance(ext); soundFileLoader= SoundFileLoaderFactory::getInstance()->newInstance(ext);
if(soundFileLoader == NULL) { if(soundFileLoader == NULL) {
@@ -116,7 +116,7 @@ void StrSound::open(const string &path) {
return; return;
} }
string ext= path.substr(path.find_last_of('.')+1); string ext = (path.empty() == false ? path.substr(path.find_last_of('.')+1) : "");
soundFileLoader= SoundFileLoaderFactory::getInstance()->newInstance(ext); soundFileLoader= SoundFileLoaderFactory::getInstance()->newInstance(ext);
soundFileLoader->open(path, &info); soundFileLoader->open(path, &info);

View File

@@ -640,7 +640,7 @@ string lastDir(const string &s) {
string retry = s.substr(0, pos); string retry = s.substr(0, pos);
return lastDir(retry); return lastDir(retry);
} }
string result = s.substr(pos+1, s.length()); string result = (s.empty() == false ? s.substr(pos+1, s.length()) : "");
replaceAll(result,"/",""); replaceAll(result,"/","");
replaceAll(result,"\\",""); replaceAll(result,"\\","");
@@ -715,16 +715,18 @@ string replaceBy(const string &s, char c1, char c2){
vector<string> split(string s,string d) { vector<string> split(string s,string d) {
vector<string> results; vector<string> results;
size_t lastOffset = 0;
while(true) if(s.empty() == false) {
{ size_t lastOffset = 0;
size_t offset = s.find_first_of(d, lastOffset);
results.push_back(s.substr(lastOffset, offset - lastOffset)); while(true) {
if (offset == string::npos) size_t offset = s.find_first_of(d, lastOffset);
break; results.push_back(s.substr(lastOffset, offset - lastOffset));
else if (offset == string::npos)
lastOffset = offset + d.size(); //skip the delimiter break;
else
lastOffset = offset + d.size(); //skip the delimiter
}
} }
return results; return results;
} }
@@ -822,7 +824,11 @@ int compareMajorMinorVersion(string versionA,string versionB) {
} }
int getMajor(string version) { int getMajor(string version) {
vector<string> parts = split(version.substr(1),"."); vector<string> parts;
if(version.empty() == false) {
parts = split(version.substr(1),".");
}
if(parts.size() > 1 && parts[0] != "" && IsNumeric(parts[0].c_str(),false)) { if(parts.size() > 1 && parts[0] != "" && IsNumeric(parts[0].c_str(),false)) {
return strToInt(parts[0]); return strToInt(parts[0]);
@@ -833,7 +839,11 @@ int getMajor(string version) {
} }
int getMinor(string version) { int getMinor(string version) {
vector<string> parts = split(version.substr(1),"."); vector<string> parts;
if(version.empty() == false) {
parts = split(version.substr(1),".");
}
if(parts.size() > 1 && parts[1] != "") { if(parts.size() > 1 && parts[1] != "") {
string resultStr=""; string resultStr="";
for (int i = 0; i < (int)parts[1].length(); ++i) { for (int i = 0; i < (int)parts[1].length(); ++i) {