Skip to content

Commit

Permalink
Com: avoid intentional memory leak in epicsEnvSet
Browse files Browse the repository at this point in the history
Switch default impl. to setenv/unsetenv
Switch WIN32 to use _putenv_s
On vxWorks putenv() is documented to make a copy.

log error, but never halt, if env (un)set not possible.

RTEMS <4.10 compat where unsetenv() returns void.
  • Loading branch information
mdavidsaver committed Mar 26, 2021
1 parent 5319a49 commit bf7b08e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 323 deletions.
72 changes: 0 additions & 72 deletions modules/libcom/src/osi/os/Darwin/osdEnv.c

This file was deleted.

62 changes: 0 additions & 62 deletions modules/libcom/src/osi/os/RTEMS/osdEnv.c

This file was deleted.

31 changes: 10 additions & 21 deletions modules/libcom/src/osi/os/WIN32/osdEnv.c
Expand Up @@ -21,38 +21,28 @@

#include "epicsStdio.h"
#include "errlog.h"
#include "cantProceed.h"
#include "envDefs.h"
#include "osiUnistd.h"
#include "epicsFindSymbol.h"
#include "iocsh.h"

static
void setEnv(const char *name, const char *value)
{
errno_t err = _putenv_s(name, value);
if(err)
errlogPrintf("Can't set environment %s=\"%s\" : %d\n", name, value, (int)err);
}

/*
* Set the value of an environment variable
* Leaks memory, but the assumption is that this routine won't be
* called often enough for the leak to be a problem.
*/
LIBCOM_API void epicsStdCall epicsEnvSet (const char *name, const char *value)
{
char *cp;

iocshEnvClear(name);

cp = mallocMustSucceed (strlen (name) + strlen (value) + 2, "epicsEnvSet");
strcpy (cp, name);
strcat (cp, "=");
strcat (cp, value);
if (putenv (cp) < 0) {
errPrintf(
-1L,
__FILE__,
__LINE__,
"Failed to set environment parameter \"%s\" to \"%s\": %s\n",
name,
value,
strerror (errno));
free (cp);
}
setEnv(name, value);
}

/*
Expand All @@ -63,8 +53,7 @@ LIBCOM_API void epicsStdCall epicsEnvSet (const char *name, const char *value)
LIBCOM_API void epicsStdCall epicsEnvUnset (const char *name)
{
iocshEnvClear(name);
if (getenv(name) != NULL)
epicsEnvSet((char*)name, "");
setEnv(name, "");
}

/*
Expand Down
39 changes: 17 additions & 22 deletions modules/libcom/src/osi/os/default/osdEnv.c
Expand Up @@ -20,40 +20,35 @@
#include <errno.h>

#include "epicsStdio.h"
#include "epicsVersion.h"
#include "errlog.h"
#include "cantProceed.h"
#include "envDefs.h"
#include "osiUnistd.h"
#include "epicsFindSymbol.h"
#include "iocsh.h"

#ifdef __rtems__
# include <rtems.h>
# define RTEMS_VERSION_INT VERSION_INT(__RTEMS_MAJOR__, __RTEMS_MINOR__, 0, 0)
#endif

#if defined(__RTEMS_MAJOR__) && RTEMS_VERSION_INT<VERSION_INT(4,10,0,0)
/* newlib w/ RTEMS <=4.9 returns void */
# define unSetEnv(name) ({unsetenv(name); 0;})
#else
# define unSetEnv(name) unsetenv(name)
#endif

/*
* Set the value of an environment variable
* Leaks memory, but the assumption is that this routine won't be
* called often enough for the leak to be a problem.
*/
LIBCOM_API void epicsStdCall epicsEnvSet (const char *name, const char *value)
{
char *cp;

if (!name) return;
iocshEnvClear(name);

cp = mallocMustSucceed (strlen (name) + strlen (value) + 2, "epicsEnvSet");
strcpy (cp, name);
strcat (cp, "=");
strcat (cp, value);
if (putenv (cp) < 0) {
errPrintf(
-1L,
__FILE__,
__LINE__,
"Failed to set environment parameter \"%s\" to \"%s\": %s\n",
name,
value,
strerror (errno));
free (cp);
}
if(setenv(name, value, 1))
errlogPrintf("setenv(\"%s\", \"%s\") -> %d\n", name, value, errno);
}

/*
Expand All @@ -64,8 +59,8 @@ LIBCOM_API void epicsStdCall epicsEnvSet (const char *name, const char *value)
LIBCOM_API void epicsStdCall epicsEnvUnset (const char *name)
{
iocshEnvClear(name);
if (getenv(name) != NULL)
putenv((char*)name);
if(unSetEnv(name))
errlogPrintf("unsetenv(\"%s\") -> %d\n", name, errno);
}

/*
Expand Down
69 changes: 0 additions & 69 deletions modules/libcom/src/osi/os/iOS/osdEnv.c

This file was deleted.

62 changes: 0 additions & 62 deletions modules/libcom/src/osi/os/solaris/osdEnv.c

This file was deleted.

0 comments on commit bf7b08e

Please sign in to comment.