aboutsummaryrefslogtreecommitdiff
path: root/src/libutil
diff options
context:
space:
mode:
authorFélix Baylac Jacqué <felix@alternativebit.fr>2023-03-01 20:01:36 +0100
committerzimbatm <zimbatm@zimbatm.com>2023-03-01 20:50:07 +0100
commit25300c0ecdedcd8720b996b41e78dfe1037bfcff (patch)
treea74404a7ff503134cb954794e0b9aca0cd9f0abd /src/libutil
parent4489def1b36aeaee2254159efc1c21c868cc8585 (diff)
Treat empty env var paths as unset
We make sure the env var paths are actually set (ie. not "") before sending them to the canonicalization function. If we forget to do so, the user will end up facing a puzzled failed assertion internal error. We issue a non-failing warning as a stop-gap measure. We could want to revisit this to issue a detailed failing error message in the future.
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/util.cc12
-rw-r--r--src/libutil/util.hh4
2 files changed, 16 insertions, 0 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 885bae69c..5377f093b 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -54,6 +54,18 @@ std::optional<std::string> getEnv(const std::string & key)
return std::string(value);
}
+std::optional<std::string> getEnvNonEmpty(const std::string & key) {
+ auto value = getEnv(key);
+ if (value == "") {
+ // TODO: determine whether this should be a warning or an error.
+ logWarning({
+ .msg = hintfmt("ignoring the '%1%' env variable, its value has been set to \"\"", key)
+ });
+ return std::nullopt;
+ } else {
+ return value;
+ }
+}
std::map<std::string, std::string> getEnv()
{
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index b5625ecef..3293c758f 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -39,6 +39,10 @@ extern const std::string nativeSystem;
/* Return an environment variable. */
std::optional<std::string> getEnv(const std::string & key);
+/* Return a non empty environment variable. Returns nullopt if the env
+variable is set to "" */
+std::optional<std::string> getEnvNonEmpty(const std::string & key);
+
/* Get the entire environment. */
std::map<std::string, std::string> getEnv();