[PATCH v2 08/13] env: Early return from env_get_f() on NULL name

Marek Behún
2021-10-13T15:47:51Z

From: Marek Behún <marek.behun@nic.cz>

Test non-NULL name immediately, not in env_match().

Signed-off-by: Marek Behún <marek.behun@nic.cz>
---
cmd/nvedit.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/cmd/nvedit.c b/cmd/nvedit.c
index 9f0caceadf..7c99a693ea 100644
--- a/cmd/nvedit.c
+++ b/cmd/nvedit.c
@@ -708,13 +708,11 @@ char *from_env(const char *envvar)

static int env_match(const char *env, const char *s1, int i2)
{
- if (s1 == NULL || *s1 == '\0')
- return -1;
-
while (*s1 == env[i2++] && *s1 != '\0')
if (*s1++ == '=')
return i2;

+ /* We can look at env[i2-1] because i2 was incremented at least once. */
if (*s1 == '\0' && env[i2-1] == '=')
return i2;

@@ -729,6 +727,9 @@ int env_get_f(const char *name, char *buf, unsigned len)
const char *env;
int i, nxt;

+ if (name == NULL || *name == '\0')
+ return -1;
+
if (gd->env_valid == ENV_INVALID)
env = (const char *)default_environment;
else
--
2.32.0