commit 0495f2ed4d921ad2c7dfffab19295c88a72a7765
parent d05682a2466fd3fef0fa13d483dcf62e034acd91
Author: Haelwenn (lanodan) Monnier <contact+sr.ht@hacktivis.me>
Date: Sat, 30 Jul 2022 17:32:54 +0200
os/exec: Make setenv return errors::invalid instead of aborting
Diffstat:
4 files changed, 12 insertions(+), 10 deletions(-)
diff --git a/cmd/hare/plan.ha b/cmd/hare/plan.ha
@@ -261,7 +261,7 @@ fn execute(
};
for (let i = 0z; i < len(plan.environ); i += 1) {
let e = plan.environ[i];
- exec::setenv(&cmd, e.0, e.1);
+ exec::setenv(&cmd, e.0, e.1)!;
};
const pipe = if (plan.progress.tty is io::file) {
diff --git a/cmd/hare/release.ha b/cmd/hare/release.ha
@@ -287,7 +287,7 @@ fn signtag(tmpdir: str, name: str, tag: str, key: str) (void | release_error) =
defer exec::kill(agent)!;
const addkey = exec::cmd("ssh-add", key)?;
- exec::setenv(&addkey, "SSH_AUTH_SOCK", socket);
+ exec::setenv(&addkey, "SSH_AUTH_SOCK", socket)!;
const addkey = exec::start(&addkey)?;
const addkey = exec::wait(&addkey)?;
exec::check(&addkey)?;
@@ -299,9 +299,9 @@ fn signtag(tmpdir: str, name: str, tag: str, key: str) (void | release_error) =
const ssh = exec::cmd("ssh-keygen",
"-Y", "sign", "-f", key, "-n", "file")?;
const note = exec::cmd("git", "notes", "add", "-F", "-", tag)?;
- exec::setenv(¬e, "GIT_NOTES_REF", "refs/notes/signatures/tar.gz");
+ exec::setenv(¬e, "GIT_NOTES_REF", "refs/notes/signatures/tar.gz")!;
- exec::setenv(&ssh, "SSH_AUTH_SOCK", socket);
+ exec::setenv(&ssh, "SSH_AUTH_SOCK", socket)!;
// Squelch "Signing data on standard input" message
// TODO: It might be better to capture this and print it to stderr
// ourselves if ssh-keygen exits nonzero, so that the error details are
diff --git a/cmd/haredoc/main.ha b/cmd/haredoc/main.ha
@@ -298,7 +298,7 @@ fn init_tty(ctx: *context) io::handle = {
const pipe = exec::pipe();
exec::addfile(&pager, os::stdin_file, pipe.0);
- exec::setenv(&pager, "LESS", "FRX");
+ exec::setenv(&pager, "LESS", "FRX")!;
ctx.pager = exec::start(&pager)!;
return pipe.1;
};
diff --git a/os/exec/cmd.ha b/os/exec/cmd.ha
@@ -94,18 +94,20 @@ export fn clearenv(cmd: *command) void = {
// current process environment. The 'key' must be a valid environment variable
// name per POSIX definition 3.235. This includes underscores and alphanumeric
// ASCII characters, and cannot begin with a number.
-export fn setenv(cmd: *command, key: str, value: str) void = {
+export fn setenv(cmd: *command, key: str, value: str) (void | errors::invalid) = {
let iter = strings::iter(key);
for (let i = 0z; true; i += 1) match (strings::next(&iter)) {
case void =>
break;
case let r: rune =>
if (i == 0) {
- assert(r == '_' || ascii::isalpha(r),
- "Invalid environment variable");
+ if(!(r == '_' || ascii::isalpha(r))) {
+ return errors::invalid;
+ };
} else {
- assert(r == '_' || ascii::isalnum(r),
- "Invalid environment variable");
+ if(!(r == '_' || ascii::isalnum(r))) {
+ return errors::invalid;
+ };
};
};