From cc915eefafa89fe49da3afaf0f3fdaef24679b62 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 17 Jul 2024 17:54:09 +0200 Subject: [PATCH] env-util: suppress unnecessary setenv() in setenvf() --- src/basic/env-util.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/basic/env-util.c b/src/basic/env-util.c index 3e385e5ee4b..cf2c47d2142 100644 --- a/src/basic/env-util.c +++ b/src/basic/env-util.c @@ -1146,5 +1146,10 @@ int setenvf(const char *name, bool overwrite, const char *valuef, ...) { if (r < 0) return -ENOMEM; + /* Try to suppress writes if the value is already set correctly (simply because memory management of + * environment variables sucks a bit. */ + if (streq_ptr(getenv(name), value)) + return 0; + return RET_NERRNO(setenv(name, value, overwrite)); }