From: Daniel Vrátil Date: Wed, 12 Sep 2018 15:20:00 +0000 (+0200) Subject: exec: Call putenv() with a global buffer X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=33b2b04f702b3c30661521ddfe2d0f000c0fa5d9;p=collectd.git exec: Call putenv() with a global buffer putenv() adds the string pointer to the environment so it should not be called with an automatic variable. Signed-off-by: Florian Forster --- diff --git a/src/exec.c b/src/exec.c index 8340f8b0..a4de6730 100644 --- a/src/exec.c +++ b/src/exec.c @@ -245,11 +245,17 @@ static int exec_config(oconfig_item_t *ci) /* {{{ */ return 0; } /* int exec_config }}} */ +#if !defined(HAVE_SETENV) +static char env_interval[64]; +// max hostname len is 255, so this should be enough +static char env_hostname[300]; +#endif + static void set_environment(void) /* {{{ */ { +#ifdef HAVE_SETENV char buffer[1024]; -#ifdef HAVE_SETENV snprintf(buffer, sizeof(buffer), "%.3f", CDTIME_T_TO_DOUBLE(plugin_get_interval())); setenv("COLLECTD_INTERVAL", buffer, /* overwrite = */ 1); @@ -257,12 +263,13 @@ static void set_environment(void) /* {{{ */ sstrncpy(buffer, hostname_g, sizeof(buffer)); setenv("COLLECTD_HOSTNAME", buffer, /* overwrite = */ 1); #else - snprintf(buffer, sizeof(buffer), "COLLECTD_INTERVAL=%.3f", + snprintf(env_interval, sizeof(env_interval), "COLLECTD_INTERVAL=%.3f", CDTIME_T_TO_DOUBLE(plugin_get_interval())); - putenv(buffer); + putenv(env_interval); - snprintf(buffer, sizeof(buffer), "COLLECTD_HOSTNAME=%s", hostname_g); - putenv(buffer); + snprintf(env_hostname, sizeof(env_hostname), "COLLECTD_HOSTNAME=%s", + hostname_g); + putenv(env_hostname); #endif } /* }}} void set_environment */