Skip to content

Commit

Permalink
Reuse result from getenv
Browse files Browse the repository at this point in the history
In theory, calling getenv a second time could return NULL after
previously returning non-NULL, which would be undefined behavior when
passed to strcmp. Re-using the non-NULL value is slightly safer, and
keeps clang's static analyzer happier.
  • Loading branch information
mabraham committed Oct 3, 2024
1 parent 2047882 commit 6980cf8
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/wrapper/Plumed.h
Original file line number Diff line number Diff line change
Expand Up @@ -4015,6 +4015,8 @@ void plumed_retrieve_functions(plumed_plumedmain_function_holder* functions, plu
void* p;
char* debug;
int dlopenmode;
/* possible value of PLUMED_LOAD_NAMESPACE environment variable */
char *load_namespace;
g.create=__PLUMED_WRAPPER_CXX_NULLPTR;
g.cmd=__PLUMED_WRAPPER_CXX_NULLPTR;
g.finalize=__PLUMED_WRAPPER_CXX_NULLPTR;
Expand Down Expand Up @@ -4045,7 +4047,8 @@ void plumed_retrieve_functions(plumed_plumedmain_function_holder* functions, plu
__PLUMED_FPRINTF(stderr,"+++ PLUMED_KERNEL=\"%s\" +++\n",path);
if(debug) __PLUMED_FPRINTF(stderr,"+++ Loading with mode RTLD_NOW");
dlopenmode=RTLD_NOW;
if(__PLUMED_GETENV("PLUMED_LOAD_NAMESPACE") && !__PLUMED_WRAPPER_STD strcmp(__PLUMED_GETENV("PLUMED_LOAD_NAMESPACE"),"GLOBAL")) {
load_namespace = __PLUMED_GETENV("PLUMED_LOAD_NAMESPACE");
if(load_namespace && !__PLUMED_WRAPPER_STD strcmp(load_namespace,"GLOBAL")) {
dlopenmode=dlopenmode|RTLD_GLOBAL;
if(debug) __PLUMED_FPRINTF(stderr,"|RTLD_GLOBAL");
} else {
Expand Down Expand Up @@ -4151,6 +4154,8 @@ plumed plumed_create(void) {
plumed p;
/* pointer to implementation */
plumed_implementation* pimpl;
/* possible value of PLUMED_LOAD_DLCLOSE environment variable */
char *load_dlclose;
/* allocate space for implementation object. this is free-ed in plumed_finalize(). */
pimpl=plumed_malloc_pimpl();
/* store pointers in pimpl */
Expand All @@ -4161,7 +4166,8 @@ plumed plumed_create(void) {
#endif
/* note if handle should not be dlclosed */
pimpl->dlclose=1;
if(__PLUMED_GETENV("PLUMED_LOAD_DLCLOSE") && !__PLUMED_WRAPPER_STD strcmp(__PLUMED_GETENV("PLUMED_LOAD_DLCLOSE"),"no")) pimpl->dlclose=0;
load_dlclose = __PLUMED_GETENV("PLUMED_LOAD_DLCLOSE");
if(load_dlclose && !__PLUMED_WRAPPER_STD strcmp(load_dlclose,"no")) pimpl->dlclose=0;
/* in case of failure, return */
/* the resulting object should be plumed_finalized, though you cannot use plumed_cmd */
if(!pimpl->functions.create) {
Expand Down

0 comments on commit 6980cf8

Please sign in to comment.