Skip to content

Commit a405d88

Browse files
authored
Throw an error for an empty array (#349)
Throw an error for an empty array before accessing the first element.
1 parent ca657f5 commit a405d88

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

vminitd/Sources/vmexec/vmexec.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ extension App {
7373
}
7474

7575
static func exec(process: ContainerizationOCI.Process, currentEnv: [String]? = nil) throws {
76+
guard !process.args.isEmpty else {
77+
throw App.Errno(stage: "exec", info: "process args cannot be empty")
78+
}
79+
7680
// lookup executable
7781
let path = Path.findPath(currentEnv) ?? Path.getCurrentPath()
7882
guard let resolvedExecutable = Path.lookPath(process.args[0], path: path) else {
@@ -88,11 +92,11 @@ extension App {
8892

8993
// switch cwd
9094
guard chdir(cwd) == 0 else {
91-
throw App.Errno(stage: "chdir(cwd)", info: "Failed to change directory to '\(cwd)'")
95+
throw App.Errno(stage: "chdir(cwd)", info: "failed to change directory to '\(cwd)'")
9296
}
9397

9498
guard execvpe(executable, argv, env) != -1 else {
95-
throw App.Errno(stage: "execvpe(\(String(describing: executable)))", info: "Failed to exec [\(process.args.joined(separator: " "))]")
99+
throw App.Errno(stage: "execvpe(\(String(describing: executable)))", info: "failed to exec [\(process.args.joined(separator: " "))]")
96100
}
97101
fatalError("execvpe failed")
98102
}

0 commit comments

Comments
 (0)