Implement $[&name] for environment-variable access

The env-var branch in Tag::getVariable was dead code - it checked for "%",
but that sigil was already claimed by the keyword-lookup branch earlier in
the same if/else chain, so it could never be reached. Switched to "&"
(unused by any other sigil) per explicit user decision.

Also fixed a real crash risk while making this reachable: getenv() returns
NULL for an unset variable, and MString's const char* constructor calls
strlen() unconditionally - now throws a clean "environment variable is not
set." error instead.

Verified via real jet-2.0 runs: a set variable reads and applies modifiers
(;toupper) correctly, an unset one throws cleanly rather than crashing.
Full regression suite and 30/30 compile check still pass.
This commit is contained in:
Brad Arant committed 2026-08-26 13:05:24 -07:00
1 parent b276bdb6cc
commit 7893746582
1 file changed
+6 -9
+6 -9
View File
@@ -471,16 +471,13 @@ namespace jet {
throw coreutils::Exception("session variable requested but no session is active (missing sessiondir keyword, or cgi mode is off)."); throw coreutils::Exception("session variable requested but no session is active (missing sessiondir keyword, or cgi mode is off).");
coreutils::MString sessionValue = (*global.sessionFile)[name]; coreutils::MString sessionValue = (*global.sessionFile)[name];
return processModifier(sessionValue, modifier); return processModifier(sessionValue, modifier);
} else if(variable.ifNext("%")) { } else if(variable.ifNext("&")) {
// NOTE: unreachable - '%' is already claimed by the keyword-lookup branch
// above (variable.ifNext("%") at the top of this chain), so this branch
// can never be entered. This looks like a copy-paste where the intended
// sigil character for environment-variable access was meant to be
// something else. Left as-is rather than guessing a new template-syntax
// character unilaterally - confirm the intended sigil before wiring this
// branch up to a reachable condition.
renderVariableName(variable, name, modifier); renderVariableName(variable, name, modifier);
return getenv(name.c_str()); const char *envValue = getenv(name.c_str());
if(envValue == NULL)
throw coreutils::Exception("environment variable is not set.");
coreutils::MString envValueStr(envValue);
return processModifier(envValueStr, modifier);
} else if(variable.ifNext("^")) { } else if(variable.ifNext("^")) {
renderVariableName(variable, name, modifier); renderVariableName(variable, name, modifier);
return global.cookies.data[name]; return global.cookies.data[name];