From 338b0e2b7b51efc8eabf3167b32d4061f884e83b Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 1 Nov 2017 17:38:33 +0000 Subject: [PATCH] Moved getenv usage across to safer osg::getEnvVar() usage --- include/osg/EnvVar | 10 ++++++++++ src/osg/GLExtensions.cpp | 21 +++++++++++---------- src/osg/GraphicsContext.cpp | 7 ++++--- src/osg/Notify.cpp | 6 +++--- src/osg/Program.cpp | 1 - src/osg/State.cpp | 18 +++++++++++------- 6 files changed, 39 insertions(+), 24 deletions(-) diff --git a/include/osg/EnvVar b/include/osg/EnvVar index ddfd6a0e1..6ef49266a 100644 --- a/include/osg/EnvVar +++ b/include/osg/EnvVar @@ -17,6 +17,7 @@ #include #ifdef OSG_ENVVAR_SUPPORTED + #include #include #endif @@ -29,6 +30,15 @@ inline unsigned int getClampedLength(const char* str, unsigned int maxNumChars=4 return i; } +inline std::string getEnvVar(const char* name) +{ + std::string value; + const char* ptr = getenv(name); + if (ptr) value.assign(ptr, getClampedLength(ptr)); + return value; + } + + template inline bool getEnvVar(const char* name, T& value) { diff --git a/src/osg/GLExtensions.cpp b/src/osg/GLExtensions.cpp index 77984a202..882fe96ab 100644 --- a/src/osg/GLExtensions.cpp +++ b/src/osg/GLExtensions.cpp @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include #include @@ -65,6 +67,9 @@ static osg::buffered_object s_gluExtensionSetList; static osg::buffered_object s_gluRendererList; static osg::buffered_value s_gluInitializedList; +static ApplicationUsageProxy GLEXtension_e0(ApplicationUsage::ENVIRONMENTAL_VARIABLE, "OSG_GL_EXTENSION_DISABLE ", "Use space deliminarted list of GL extensions to disable associated GL extensions"); +static ApplicationUsageProxy GLEXtension_e1(ApplicationUsage::ENVIRONMENTAL_VARIABLE, "OSG_MAX_TEXTURE_SIZE ", "Clamp the maximum GL texture size to specified value."); + float osg::getGLVersionNumber() { // needs to be extended to do proper things with subversions like 1.5.1, etc. @@ -308,8 +313,7 @@ void osg::setGLExtensionDisableString(const std::string& disableString) std::string& osg::getGLExtensionDisableString() { - static const char* envVar = getenv("OSG_GL_EXTENSION_DISABLE"); - static std::string s_GLExtensionDisableString(envVar?envVar:"Nothing defined"); + static std::string s_GLExtensionDisableString(getEnvVar("OSG_GL_EXTENSION_DISABLE")); return s_GLExtensionDisableString; } @@ -901,15 +905,12 @@ GLExtensions::GLExtensions(unsigned int in_contextID): if (validContext) glGetIntegerv(GL_MAX_TEXTURE_SIZE,&maxTextureSize); char *ptr; - if( (ptr = getenv("OSG_MAX_TEXTURE_SIZE")) != 0) + + GLint osg_max_size = maxTextureSize; + + if( (getEnvVar("OSG_MAX_TEXTURE_SIZE", osg_max_size)) && osg_max_size #include #include +#include #include #include @@ -153,10 +154,10 @@ std::string GraphicsContext::ScreenIdentifier::displayName() const void GraphicsContext::ScreenIdentifier::readDISPLAY() { - const char* ptr = 0; - if ((ptr=getenv("DISPLAY")) != 0) + std::string str; + if (getEnvVar("DISPLAY", str)) { - setScreenIdentifier(ptr); + setScreenIdentifier(str); } } diff --git a/src/osg/Notify.cpp b/src/osg/Notify.cpp index e562aea4d..6ca85b37e 100644 --- a/src/osg/Notify.cpp +++ b/src/osg/Notify.cpp @@ -12,6 +12,7 @@ */ #include #include +#include #include #include #include @@ -140,9 +141,8 @@ struct NotifySingleton _notifyLevel = osg::NOTICE; // Default value - char* OSGNOTIFYLEVEL=getenv("OSG_NOTIFY_LEVEL"); - if (!OSGNOTIFYLEVEL) OSGNOTIFYLEVEL=getenv("OSGNOTIFYLEVEL"); - if(OSGNOTIFYLEVEL) + std::string OSGNOTIFYLEVEL; + if(getEnvVar("OSG_NOTIFY_LEVEL", OSGNOTIFYLEVEL) || getEnvVar("OSGNOTIFYLEVEL", OSGNOTIFYLEVEL)) { std::string stringOSGNOTIFYLEVEL(OSGNOTIFYLEVEL); diff --git a/src/osg/Program.cpp b/src/osg/Program.cpp index 85c49a37d..68fadaf0c 100644 --- a/src/osg/Program.cpp +++ b/src/osg/Program.cpp @@ -516,7 +516,6 @@ void Program::apply( osg::State& state ) const { // for shader debugging: to minimize performance impact, // optionally validate based on notify level. - // TODO: enable this using notify level, or perhaps its own getenv()? #ifndef __APPLE__ if( osg::isNotifyEnabled(osg::INFO) ) pcp->validateProgram(); diff --git a/src/osg/State.cpp b/src/osg/State.cpp index 05933aa30..5ed4f277e 100644 --- a/src/osg/State.cpp +++ b/src/osg/State.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -75,14 +76,17 @@ State::State(): _checkGLErrors = ONCE_PER_FRAME; - const char* str = getenv("OSG_GL_ERROR_CHECKING"); - if (str && (strcmp(str,"ONCE_PER_ATTRIBUTE")==0 || strcmp(str,"ON")==0 || strcmp(str,"on")==0)) + std::string str; + if (getEnvVar("OSG_GL_ERROR_CHECKING", str)) { - _checkGLErrors = ONCE_PER_ATTRIBUTE; - } - else if(str && (strcmp(str, "OFF") == 0 || strcmp(str, "off") == 0)) - { - _checkGLErrors = NEVER_CHECK_GL_ERRORS; + if (str=="ONCE_PER_ATTRIBUTE" || str=="ON" || str=="on") + { + _checkGLErrors = ONCE_PER_ATTRIBUTE; + } + else if (str=="OFF" || str=="off") + { + _checkGLErrors = NEVER_CHECK_GL_ERRORS; + } } _currentActiveTextureUnit=0;