From e1d6f3318bdaec4a389ae6e699e73fa66833becb Mon Sep 17 00:00:00 2001 From: Dirk Zimoch Date: Tue, 3 Nov 2015 15:23:14 +0100 Subject: [PATCH] avoid putenvprint memory leak in error case --- require.c | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/require.c b/require.c index 612b156..f98bdfd 100644 --- a/require.c +++ b/require.c @@ -20,10 +20,6 @@ #include -#ifndef __GNUC__ -#define __attribute__(arg) -#endif - #ifdef BASE_VERSION #define EPICS_3_13 #define epicsGetStdout() stdout @@ -309,14 +305,15 @@ typedef struct moduleitem moduleitem* loadedModules = NULL; +#ifdef __GNUC__ static int putenvprintf(const char* format, ...) __attribute__((format(printf,1,2))); +#endif static int putenvprintf(const char* format, ...) { va_list ap; char *var; -#ifndef vxWorks char *val; -#endif + int status = 0; va_start(ap, format); if (vasprintf(&var, format, ap) < 0) @@ -329,24 +326,31 @@ static int putenvprintf(const char* format, ...) if (requireDebug) printf("require: putenv(\"%s\")\n", var); -#ifdef vxWorks - putenv(var); /* vxWorks putenv() makes a copy */ -#else val = strchr(var, '='); if (!val) { fprintf(stderr, "putenvprintf: string contains no =: %s\n", var); - return -1; + status = -1; } - *val++ = 0; - if (setenv(var, val, 1) == -1) + else { - perror("require putenvprintf: setenv failed"); - return errno; - } +#ifdef vxWorks + if (putenv(var) != 0) /* vxWorks putenv() makes a copy */ + { + perror("require putenvprintf: putenv failed"); + status = errno; + } +#else + *val++ = 0; + if (setenv(var, val, 1) != 0) + { + perror("require putenvprintf: setenv failed"); + status = errno; + } #endif + } free(var); - return 0; + return status; } static int runLoadScript(const char* script, const char* module, const char* version)