From a405d88e6d4e3bc1d917deec8b3b0a5bee30a6b9 Mon Sep 17 00:00:00 2001 From: Dmitry Kovba Date: Fri, 24 Oct 2025 11:19:25 -0700 Subject: [PATCH] Throw an error for an empty array (#349) Throw an error for an empty array before accessing the first element. --- vminitd/Sources/vmexec/vmexec.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/vminitd/Sources/vmexec/vmexec.swift b/vminitd/Sources/vmexec/vmexec.swift index 199a29ed..dd057dfd 100644 --- a/vminitd/Sources/vmexec/vmexec.swift +++ b/vminitd/Sources/vmexec/vmexec.swift @@ -73,6 +73,10 @@ extension App { } static func exec(process: ContainerizationOCI.Process, currentEnv: [String]? = nil) throws { + guard !process.args.isEmpty else { + throw App.Errno(stage: "exec", info: "process args cannot be empty") + } + // lookup executable let path = Path.findPath(currentEnv) ?? Path.getCurrentPath() guard let resolvedExecutable = Path.lookPath(process.args[0], path: path) else { @@ -88,11 +92,11 @@ extension App { // switch cwd guard chdir(cwd) == 0 else { - throw App.Errno(stage: "chdir(cwd)", info: "Failed to change directory to '\(cwd)'") + throw App.Errno(stage: "chdir(cwd)", info: "failed to change directory to '\(cwd)'") } guard execvpe(executable, argv, env) != -1 else { - throw App.Errno(stage: "execvpe(\(String(describing: executable)))", info: "Failed to exec [\(process.args.joined(separator: " "))]") + throw App.Errno(stage: "execvpe(\(String(describing: executable)))", info: "failed to exec [\(process.args.joined(separator: " "))]") } fatalError("execvpe failed") }