diff --git a/examples/osgphotoalbum/PhotoArchive.cpp b/examples/osgphotoalbum/PhotoArchive.cpp index 59eac5d3e..1cf750ee7 100644 --- a/examples/osgphotoalbum/PhotoArchive.cpp +++ b/examples/osgphotoalbum/PhotoArchive.cpp @@ -41,7 +41,12 @@ bool PhotoArchive::readPhotoIndex(const std::string& filename) char* fileIndentifier = new char [FILE_IDENTIFER.size()]; in.read(fileIndentifier,FILE_IDENTIFER.size()); - if (FILE_IDENTIFER!=fileIndentifier) return false; + if (FILE_IDENTIFER!=fileIndentifier) + { + delete [] fileIndentifier; + return false; + } + delete [] fileIndentifier; unsigned int numPhotos; in.read((char*)&numPhotos,sizeof(numPhotos)); @@ -231,9 +236,9 @@ void PhotoArchive::buildArchive(const std::string& filename, const FileNameList& if (status!=0) { - delete [] newData; - + delete [] newData; osg::notify(osg::WARN) << "Error scaleImage() did not succeed : errorString = "<finishEditing(); // free resources - delete tmpArray; + delete [] tmpArray; } diff --git a/src/osgPlugins/Inventor/ReaderWriterIV.cpp b/src/osgPlugins/Inventor/ReaderWriterIV.cpp index d21667e00..a2cf4459d 100644 --- a/src/osgPlugins/Inventor/ReaderWriterIV.cpp +++ b/src/osgPlugins/Inventor/ReaderWriterIV.cpp @@ -234,7 +234,13 @@ ReaderWriterIV::readNode(std::istream& fin, dataSize += fin.gcount(); if (bufSize == dataSize) { bufSize *= 2; - buf = (char*)realloc(buf, bufSize); + char* new_buf = (char*)realloc(buf, bufSize); + if (!new_buf) + { + free(buf); + return osgDB::ReaderWriter::ReadResult::INSUFFICIENT_MEMORY_TO_LOAD; + } + buf = new_buf; } } input.setBuffer(buf, dataSize); diff --git a/src/osgPlugins/cfg/CameraConfig.cpp b/src/osgPlugins/cfg/CameraConfig.cpp index cd193993e..4c233b22d 100644 --- a/src/osgPlugins/cfg/CameraConfig.cpp +++ b/src/osgPlugins/cfg/CameraConfig.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include "CameraConfig.h" @@ -632,7 +633,8 @@ bool CameraConfig::defaultConfig() for( unsigned int i = 0; i < numScreens; i++ ) { - std::string name = "Screen" + i; + std::stringstream sstr; sstr<<"Screen"< >::iterator,bool> res = _camera_map.insert(std::pair >(name, new Camera)); diff --git a/src/osgPlugins/freetype/FreeTypeLibrary.cpp b/src/osgPlugins/freetype/FreeTypeLibrary.cpp index 2969093b6..be002bd63 100644 --- a/src/osgPlugins/freetype/FreeTypeLibrary.cpp +++ b/src/osgPlugins/freetype/FreeTypeLibrary.cpp @@ -119,6 +119,7 @@ FT_Byte* FreeTypeLibrary::getFace(std::istream& fontstream, unsigned int index, if (!fontstream || (static_cast(fontstream.gcount()) != length)) { OSG_WARN<<" .... the font file could not be read from its stream"<clear(); + delete color_palette; + } +} + + const geoValue *geoHeaderGeo::getGeoVar(const unsigned fid) const { const geoValue *st=intVars->getGeoVar(fid); if (!st) { @@ -207,37 +233,50 @@ public: int shademodel=gfshade ? gfshade->getInt() : -1; if (shademodel!=GEO_POLY_SHADEMODEL_LIT && shademodel!=GEO_POLY_SHADEMODEL_FLAT) { const geoField *gfd=gr->getField(GEO_DB_VRTX_NORMAL); - if (gfd->getType()==DB_UINT) { - if (gfd) { + if (gfd) + { + if (gfd->getType()==DB_UINT) + { unsigned int idx=gfd->getUInt(); normindices->push_back(idx); norms->push_back((*npool)[idx]); - } else { + } + else if (gfd->getType()==DB_VEC3F) + { + float *p=gfd->getVec3Arr(); + osg::Vec3 nrm; + nrm.set(p[0],p[1],p[2]); + norms->push_back(nrm); + } + else + { OSG_WARN << "No valid vertex index" << std::endl; } - } else if (gfd->getType()==DB_VEC3F) { - float *p=gfd->getVec3Arr(); - osg::Vec3 nrm; - nrm.set(p[0],p[1],p[2]); - norms->push_back(nrm); } } - const geoField *gfd=gr->getField(GEO_DB_VRTX_COORD); osg::Vec3 pos; - if (gfd->getType()==DB_INT) { - if (gfd) { - int idx=gfd->getInt(); + const geoField *gfd=gr->getField(GEO_DB_VRTX_COORD); + if (gfd) + { + if (gfd->getType()==DB_INT) + { + int idx=gfd->getInt(); pos=(*cpool)[idx]; coords->push_back((*cpool)[idx]); //osg::Vec3(cpool[3*idx],cpool[3*idx+1],cpool[3*idx+2])); coordindices->push_back(coords->size()); - } else { + } + else if (gfd->getType()==DB_VEC3F) + { + float *p=gfd->getVec3Arr(); + pos.set(p[0],p[1],p[2]); + coords->push_back(pos); //osg::Vec3(cpool[3*idx],cpool[3*idx+1],cpool[3*idx+2])); + } + else + { OSG_WARN << "No valid vertex index" << std::endl; } - } else if (gfd->getType()==DB_VEC3F) { - float *p=gfd->getVec3Arr(); - pos.set(p[0],p[1],p[2]); - coords->push_back(pos); //osg::Vec3(cpool[3*idx],cpool[3*idx+1],cpool[3*idx+2])); } + std::vector< georecord *>bhv=gr->getBehaviour(); // behaviours for vertices, eg tranlate, colour! if (!bhv.empty()) { int ncoord=coords->size(); @@ -898,19 +937,24 @@ class ReaderGEO (*itr)->getType()==DB_DSK_FAT_VERTEX || (*itr)->getType()==DB_DSK_SLIM_VERTEX) { // light point vertices - const geoField *gfd=(*itr)->getField(GEO_DB_VRTX_COORD); osg::Vec3 pos; - if (gfd->getType()==DB_INT) { - if (gfd) { - int idx=gfd->getInt(); - pos=coord_pool[idx]; - } else { + const geoField *gfd=(*itr)->getField(GEO_DB_VRTX_COORD); + if (gfd) { + if (gfd->getType()==DB_INT) + { + int idx=gfd->getInt(); + pos=coord_pool[idx]; + } + else if (gfd->getType()==DB_VEC3F) + { + float *p=gfd->getVec3Arr(); + pos.set(p[0],p[1],p[2]); + } + else { OSG_WARN << "No valid vertex index" << std::endl; } - } else if (gfd->getType()==DB_VEC3F) { - float *p=gfd->getVec3Arr(); - pos.set(p[0],p[1],p[2]); } + gfd=(*itr)->getField(GEO_DB_VRTX_PACKED_COLOR); if (gfd) { unsigned char *cls=gfd->getUCh4Arr(); diff --git a/src/osgPlugins/geo/osgGeoNodes.h b/src/osgPlugins/geo/osgGeoNodes.h index 0e0836383..fcb0e08e0 100644 --- a/src/osgPlugins/geo/osgGeoNodes.h +++ b/src/osgPlugins/geo/osgGeoNodes.h @@ -131,12 +131,8 @@ class geoHeaderGeo: public geoHeader { // including animation variables. public: geoHeaderGeo(); - ~geoHeaderGeo() { color_palette->clear(); } - geoHeaderGeo(const geoHeaderGeo &geo,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY) : - geoHeader(geo,copyop){ - intVars=new internalVars(*geo.intVars); useVars=new userVars(*geo.useVars); - extVars=new userVars(*geo.extVars); - } + geoHeaderGeo(const geoHeaderGeo &geo,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); + void addInternalVars(const georecord &gr) { intVars->addInternalVars(gr);} internalVars *getInternalVars(void) const { return intVars;} const std::string getVarname(const unsigned fid) const { @@ -174,14 +170,18 @@ public: } void addColour(unsigned char *cpal) {(*color_palette).push_back(cpal);} inline colourPalette *getColorPalette() const { return color_palette;} + +protected: + virtual ~geoHeaderGeo(); + private: - osg::Timer_t _lastFrameTick,_initialTick; - osg::Timer _timer; - internalVars *intVars; - userVars *useVars; - userVars *extVars; - void moveit(const double t); - colourPalette *color_palette; // the colour palette - used in colour animations + void moveit(const double t); + osg::Timer_t _lastFrameTick,_initialTick; + osg::Timer _timer; + internalVars *intVars; + userVars *useVars; + userVars *extVars; + colourPalette *color_palette; // the colour palette - used in colour animations }; #endif diff --git a/src/osgPlugins/md2/ReaderWriterMD2.cpp b/src/osgPlugins/md2/ReaderWriterMD2.cpp index cc75bfcd7..cd326bff8 100644 --- a/src/osgPlugins/md2/ReaderWriterMD2.cpp +++ b/src/osgPlugins/md2/ReaderWriterMD2.cpp @@ -153,7 +153,6 @@ load_md2 (const char *filename, const osgDB::ReaderWriter::Options* options) { struct stat st; void *mapbase; -// void *p; int file_fd; osg::Node* result = NULL; @@ -163,23 +162,23 @@ load_md2 (const char *filename, const osgDB::ReaderWriter::Options* options) file_fd = open (filename, O_RDONLY); if (file_fd <= 0) { - return NULL; - } - -#if 0 - mapbase = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, file_fd, 0); - if (mapbase == NULL) { close (file_fd); return NULL; } -#else + mapbase = malloc (st.st_size); - if (read(file_fd, mapbase, st.st_size)==0) + if (!mapbase) { close (file_fd); return NULL; } -#endif + + if (read(file_fd, mapbase, st.st_size)==0) + { + close (file_fd); + if (mapbase) free(mapbase); + return NULL; + } if (g_md2NormalsArray == NULL) { g_md2NormalsArray = new osg::Vec3Array; diff --git a/src/osgPlugins/pic/ReaderWriterPIC.cpp b/src/osgPlugins/pic/ReaderWriterPIC.cpp index 69f5c5f87..94b5c5804 100644 --- a/src/osgPlugins/pic/ReaderWriterPIC.cpp +++ b/src/osgPlugins/pic/ReaderWriterPIC.cpp @@ -149,8 +149,8 @@ int *numComponents_ret) if (tmpbuf == NULL || buffer == NULL) { picerror = ERROR_MEMORY; - if (tmpbuf) free(tmpbuf); - if (buffer) free(buffer); + if (tmpbuf) delete [] tmpbuf; + if (buffer) delete [] buffer; fclose(fp); return NULL; } diff --git a/src/osgPlugins/ply/plyfile.cpp b/src/osgPlugins/ply/plyfile.cpp index 2b66a6090..7c85468a1 100644 --- a/src/osgPlugins/ply/plyfile.cpp +++ b/src/osgPlugins/ply/plyfile.cpp @@ -828,6 +828,8 @@ PlyFile *ply_read(FILE *fp, int *nelems, char ***elem_names) /* create record for this object */ plyfile = (PlyFile *) myalloc (sizeof (PlyFile)); + if (!plyfile) return (NULL); + plyfile->nelems = 0; plyfile->comments = NULL; plyfile->num_comments = 0; @@ -840,15 +842,25 @@ PlyFile *ply_read(FILE *fp, int *nelems, char ***elem_names) words = get_words (plyfile->fp, &nwords, &orig_line); if (!words || !equal_strings (words[0], "ply")) + { + if (words) free( words); + free( plyfile ); return (NULL); + } - while (words) { + while (words) + { /* parse words */ - if (equal_strings (words[0], "format")) { + if (equal_strings (words[0], "format")) + { if (nwords != 3) + { + free( words ); + free( plyfile ); return (NULL); + } if (equal_strings (words[1], "ascii")) plyfile->file_type = PLY_ASCII; else if (equal_strings (words[1], "binary_big_endian")) @@ -856,10 +868,11 @@ PlyFile *ply_read(FILE *fp, int *nelems, char ***elem_names) else if (equal_strings (words[1], "binary_little_endian")) plyfile->file_type = PLY_BINARY_LE; else - { - free (words); + { + free( words ); + free( plyfile ); return (NULL); - } + } plyfile->version = osg::asciiToDouble (words[2]); } else if (equal_strings (words[0], "element")) @@ -871,10 +884,10 @@ PlyFile *ply_read(FILE *fp, int *nelems, char ***elem_names) else if (equal_strings (words[0], "obj_info")) add_obj_info (plyfile, orig_line); else if (equal_strings (words[0], "end_header")) - { + { free (words); break; - } + } /* free up words space */ free (words); diff --git a/src/osgPlugins/pvr/ReaderWriterPVR.cpp b/src/osgPlugins/pvr/ReaderWriterPVR.cpp index 8a5a9a672..304825251 100644 --- a/src/osgPlugins/pvr/ReaderWriterPVR.cpp +++ b/src/osgPlugins/pvr/ReaderWriterPVR.cpp @@ -172,11 +172,18 @@ public: else hasAlpha = false; - osg::Image *image = new osg::Image; - unsigned char *imageData = new unsigned char[header.dataLength]; + osg::ref_ptr image = new osg::Image; + if (!image) return ReadResult::INSUFFICIENT_MEMORY_TO_LOAD; + + unsigned char *imageData = new unsigned char[header.dataLength]; + if (!imageData) return ReadResult::INSUFFICIENT_MEMORY_TO_LOAD; + fin.read((char*)imageData, header.dataLength); if(!fin.good()) + { + delete [] imageData; return ReadResult::ERROR_IN_READING_FILE; + } image->setImage(header.width, header.height, 1, internalFormat, internalFormat, @@ -227,7 +234,7 @@ public: if(!mipmapdata.empty()) image->setMipmapLevels(mipmapdata); - return image; + return image.get(); } osg::notify(osg::WARN) << "Failed to read pvr data." << std::endl; diff --git a/src/osgPlugins/shp/ESRIShapeParser.cpp b/src/osgPlugins/shp/ESRIShapeParser.cpp index 81a8ca8ea..c601036bf 100644 --- a/src/osgPlugins/shp/ESRIShapeParser.cpp +++ b/src/osgPlugins/shp/ESRIShapeParser.cpp @@ -25,6 +25,7 @@ ESRIShapeParser::ESRIShapeParser( const std::string fileName, bool useDouble ): if( (fd = open( fileName.c_str(), O_RDONLY )) <= 0 ) #endif { + if (fd) close ( fd ); perror( fileName.c_str() ); return ; } diff --git a/src/osgPlugins/shp/XBaseParser.cpp b/src/osgPlugins/shp/XBaseParser.cpp index f151b9e8c..f93d319c7 100644 --- a/src/osgPlugins/shp/XBaseParser.cpp +++ b/src/osgPlugins/shp/XBaseParser.cpp @@ -93,7 +93,8 @@ XBaseParser::XBaseParser(const std::string fileName): #endif { perror( fileName.c_str() ); - return ; + if (fd) close( fd ); + return; } } diff --git a/src/osgPlugins/zip/unzip.cpp b/src/osgPlugins/zip/unzip.cpp index 5b9ae05d5..dd5526851 100644 --- a/src/osgPlugins/zip/unzip.cpp +++ b/src/osgPlugins/zip/unzip.cpp @@ -3147,17 +3147,18 @@ unzFile unzOpenInternal(LUFILE *fin) // return UNZ_OK if there is no problem. int unzClose (unzFile file) { - unz_s* s; - if (file==NULL) - return UNZ_PARAMERROR; - s=(unz_s*)file; + + if (file==NULL) + return UNZ_PARAMERROR; + + unz_s* s=(unz_s*)file; if (s->pfile_in_zip_read!=NULL) unzCloseCurrentFile(file); - lufclose(s->file); - if (s) zfree(s); // unused s=0; - return UNZ_OK; + lufclose(s->file); + zfree(s); + return UNZ_OK; } @@ -4144,15 +4145,18 @@ ZRESULT TUnzip::Find(const TCHAR *tname,bool ic,int *index,ZIPENTRY *ze) void EnsureDirectory(const TCHAR *rootdir, const TCHAR *dir) { // first check that rootdir exists. nb. rootdir has a trailing slash if (rootdir!=0) - { TCHAR rd[MAX_PATH]; + { TCHAR rd[MAX_PATH+1]; #ifdef ZIP_STD strncpy(rd,rootdir,MAX_PATH); #else _tcsncpy_s(rd,MAX_PATH,rootdir,MAX_PATH); -#endif +#endif + + // make sure there rd is always null terminated + rd[MAX_PATH] = 0; - size_t len=_tcslen(rd); + size_t len=_tcslen(rd); if (len>0 && (rd[len-1]=='/' || rd[len-1]=='\\')) rd[len-1]=0; #ifdef ZIP_STD if (!FileExists(rd)) lumkdir(rd); diff --git a/src/osgShadow/MinimalCullBoundsShadowMap.cpp b/src/osgShadow/MinimalCullBoundsShadowMap.cpp index c00769bf3..b4bfc6bbd 100644 --- a/src/osgShadow/MinimalCullBoundsShadowMap.cpp +++ b/src/osgShadow/MinimalCullBoundsShadowMap.cpp @@ -328,7 +328,7 @@ osg::BoundingBox MinimalCullBoundsShadowMap::ViewData::ComputeRenderLeavesBounds // Don't trust already computed bounds for cull generated drawables // LightPointDrawable & LightPointSpriteDrawable are such examples // they store wrong recorded bounds from very first pass - if( rl && rl->_modelview == NULL ) + if(rl->_modelview == NULL ) rl->_drawable->dirtyBound(); bb = rl->_drawable->getBound(); diff --git a/src/osgViewer/ScreenCaptureHandler.cpp b/src/osgViewer/ScreenCaptureHandler.cpp index 89d2bd46d..93908b690 100644 --- a/src/osgViewer/ScreenCaptureHandler.cpp +++ b/src/osgViewer/ScreenCaptureHandler.cpp @@ -606,9 +606,10 @@ ScreenCaptureHandler::CaptureOperation* ScreenCaptureHandler::getCaptureOperatio void ScreenCaptureHandler::addCallbackToViewer(osgViewer::ViewerBase& viewer) { osg::Camera* camera = findAppropriateCameraForCallback(viewer); + if (!camera) return; WindowCaptureCallback* callback = static_cast(_callback.get()); - if (camera && callback->getFramePosition() == WindowCaptureCallback::START_FRAME) + if (callback && callback->getFramePosition() == WindowCaptureCallback::START_FRAME) { camera->setInitialDrawCallback(_callback.get()); } @@ -621,9 +622,10 @@ void ScreenCaptureHandler::addCallbackToViewer(osgViewer::ViewerBase& viewer) void ScreenCaptureHandler::removeCallbackFromViewer(osgViewer::ViewerBase& viewer) { osg::Camera* camera = findAppropriateCameraForCallback(viewer); + if (!camera) return; WindowCaptureCallback* callback = static_cast(_callback.get()); - if (camera && callback->getFramePosition() == WindowCaptureCallback::START_FRAME) + if (callback && callback->getFramePosition() == WindowCaptureCallback::START_FRAME) { camera->setInitialDrawCallback(0); } diff --git a/src/osgVolume/Locator.cpp b/src/osgVolume/Locator.cpp index 627050f25..fe51c9963 100644 --- a/src/osgVolume/Locator.cpp +++ b/src/osgVolume/Locator.cpp @@ -207,6 +207,7 @@ void Locator::removeCallback(LocatorCallback* callback) if (*itr == callback) { _locatorCallbacks.erase(itr); + return; } } } diff --git a/src/osgVolume/RayTracedTechnique.cpp b/src/osgVolume/RayTracedTechnique.cpp index 385996fc9..752c210c9 100644 --- a/src/osgVolume/RayTracedTechnique.cpp +++ b/src/osgVolume/RayTracedTechnique.cpp @@ -269,16 +269,6 @@ void RayTracedTechnique::init() osg::ref_ptr tf_texture = new osg::Texture1D; tf_texture->setImage(tf->getImage()); -#if 1 - osgDB::writeImageFile(*(tf->getImage()),"tf.png"); - OSG_NOTICE<<"imageLayer->getTexelOffset()[3]="<getTexelOffset()[3]<getTexelScale()[3]="<getTexelScale()[3]<getMinimum()="<getMinimum()<getMaximum()="<getMaximum()<setResizeNonPowerOfTwoHint(false); tf_texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); tf_texture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); diff --git a/src/osgWrappers/serializers/osg/ShaderBinary.cpp b/src/osgWrappers/serializers/osg/ShaderBinary.cpp index f23a18536..484ed6294 100644 --- a/src/osgWrappers/serializers/osg/ShaderBinary.cpp +++ b/src/osgWrappers/serializers/osg/ShaderBinary.cpp @@ -25,7 +25,7 @@ static bool readData( osgDB::InputStream& is, osg::ShaderBinary& sb ) is >> osgDB::END_BRACKET; } sb.assign( size, (unsigned char*)data ); - delete data; + delete [] data; return true; }