From 1ff24753478ceea81ec6c8e90bb1a59d57cc0501 Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Thu, 2 Mar 2017 17:53:42 -0500 Subject: [PATCH] libopkg/opkg_cmd.c: more robust PATH handling preserve semantics of PATH when PATH is not set in environment error and undefined behavior reported in https://github.com/openwrt/packages/issues/1922 when PATH not set in environment (lighttpd executes CGI with empty base env, plus standard CGI env vars) Signed-off-by: Glenn Strauss [Jo-Philipp Wich: avoid free() on NULL, use default from cmake cache string] Signed-off-by: Jo-Philipp Wich --- libopkg/opkg_cmd.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/libopkg/opkg_cmd.c b/libopkg/opkg_cmd.c index d52bcb1..2ef2ff4 100644 --- a/libopkg/opkg_cmd.c +++ b/libopkg/opkg_cmd.c @@ -214,13 +214,19 @@ static opkg_intercept_t opkg_prep_intercepts(void) ctx = xcalloc(1, sizeof(*ctx)); ctx->oldpath = xstrdup(getenv("PATH")); - sprintf_alloc(&newpath, "%s/opkg/intercept:%s", DATADIR, ctx->oldpath); + + sprintf_alloc(&newpath, "%s/opkg/intercept:%s", DATADIR, + ctx->oldpath ? ctx->oldpath : PATH_SPEC); + sprintf_alloc(&ctx->statedir, "%s/opkg-intercept-XXXXXX", - conf->tmp_dir); + conf->tmp_dir); if (mkdtemp(ctx->statedir) == NULL) { opkg_perror(ERROR, "Failed to make temp dir %s", ctx->statedir); - free(ctx->oldpath); + + if (ctx->oldpath) + free(ctx->oldpath); + free(ctx->statedir); free(newpath); free(ctx); @@ -239,8 +245,13 @@ static int opkg_finalize_intercepts(opkg_intercept_t ctx) DIR *dir; int err = 0; - setenv("PATH", ctx->oldpath, 1); - free(ctx->oldpath); + if (ctx->oldpath) { + setenv("PATH", ctx->oldpath, 1); + free(ctx->oldpath); + } + else { + unsetenv("PATH"); + } dir = opendir(ctx->statedir); if (dir) { -- 2.30.2