From: ThorstenB Date: Sat, 17 Mar 2012 09:02:13 +0000 (+0100) Subject: Avoid segfault when a texture file is missing. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=ca97e6751163fb35ec7747d4394473dfe8eeebb0;p=simgear.git Avoid segfault when a texture file is missing. Always check the return value of "findDataFile". If it's empty, always provide the _original_ path being searched in an error message. Otherwise we eventually just get useless 'Can't find file "".' error messages (and also a segfault here, since osgDB::readImageFile("") returns NULL). --- diff --git a/simgear/scene/material/mat.cxx b/simgear/scene/material/mat.cxx index 91303ac2..450b8ef3 100644 --- a/simgear/scene/material/mat.cxx +++ b/simgear/scene/material/mat.cxx @@ -211,35 +211,42 @@ SGMaterial::read_properties(const SGReaderWriterOptions* options, ompath = SGPath("Textures"); ompath.append(omname); fullMaskPath = SGModelLib::findDataFile(ompath.str(), options); - } - - osg::Image* image = osgDB::readImageFile(fullMaskPath, options); - if (image->valid()) + } + + if (fullMaskPath.empty()) { + SG_LOG(SG_GENERAL, SG_ALERT, "Cannot find texture file \"" + << ompath.str() << "\""); + } + else { - osg::Texture2D* object_mask = new osg::Texture2D; - - bool dds_mask = (ompath.lower_extension() == "dds"); - - if (dds[i] != dds_mask) { - // Texture format does not match mask format. This is relevant for - // the object mask, as DDS textures have an origin at the bottom - // left rather than top left, therefore we flip the object mask - // vertically. - image->flipVertical(); + osg::Image* image = osgDB::readImageFile(fullMaskPath, options); + if (image && image->valid()) + { + osg::Texture2D* object_mask = new osg::Texture2D; + + bool dds_mask = (ompath.lower_extension() == "dds"); + + if (dds[i] != dds_mask) { + // Texture format does not match mask format. This is relevant for + // the object mask, as DDS textures have an origin at the bottom + // left rather than top left, therefore we flip the object mask + // vertically. + image->flipVertical(); + } + + object_mask->setImage(image); + + // We force the filtering to be nearest, as the red channel (rotation) + // in particular, doesn't make sense to be interpolated between pixels. + object_mask->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST); + object_mask->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST); + + object_mask->setDataVariance(osg::Object::STATIC); + object_mask->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); + object_mask->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT); + _masks.push_back(object_mask); } - - object_mask->setImage(image); - - // We force the filtering to be nearest, as the red channel (rotation) - // in particular, doesn't make sense to be interpolated between pixels. - object_mask->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST); - object_mask->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST); - - object_mask->setDataVariance(osg::Object::STATIC); - object_mask->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); - object_mask->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT); - _masks.push_back(object_mask); - } + } } }