Skip to content

Commit

Permalink
fix(run): Prevent possible nil-pointers by always using Cut (#1600)
Browse files Browse the repository at this point in the history
Reviewed-by: Cezar Craciunoiu <cezar.craciunoiu@unikraft.io>
Approved-by: Cezar Craciunoiu <cezar.craciunoiu@unikraft.io>
  • Loading branch information
craciunoiuc committed Apr 25, 2024
2 parents 0601616 + 60c9b53 commit fc6cee1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
7 changes: 5 additions & 2 deletions internal/cli/kraft/run/runner_kraftfile_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,12 @@ func (runner *runnerKraftfileRuntime) Prepare(ctx context.Context, opts *RunOpti
}

for _, entry := range runtime.Initrd().Env() {
split := strings.SplitN(entry, "=", 2)
k, v, ok := strings.Cut(entry, "=")
if !ok {
continue
}

machine.Spec.Env[split[0]] = split[1]
machine.Spec.Env[k] = v
}

machine.Spec.ApplicationArgs = runtime.Initrd().Args()
Expand Down
7 changes: 3 additions & 4 deletions internal/cli/kraft/run/runner_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,12 @@ func (runner *runnerPackage) Prepare(ctx context.Context, opts *RunOptions, mach
}

for _, env := range v.Config.Env {
if strings.ContainsRune(env, '=') {
parts := strings.SplitN(env, "=", 2)
machine.Spec.Env[parts[0]] = parts[1]
k, v, ok := strings.Cut(env, "=")
if !ok {
continue
}

machine.Spec.Env[env] = ""
machine.Spec.Env[k] = v
}
default:
}
Expand Down
22 changes: 12 additions & 10 deletions internal/cli/kraft/run/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,12 @@ func (opts *RunOptions) prepareRootfs(ctx context.Context, machine *machineapi.M
}

for _, entry := range ramfs.Env() {
split := strings.SplitN(entry, "=", 2)
k, v, ok := strings.Cut(entry, "=")
if !ok {
continue
}

machine.Spec.Env[split[0]] = split[1]
machine.Spec.Env[k] = v
}

machine.Spec.ApplicationArgs = ramfs.Args()
Expand Down Expand Up @@ -429,9 +432,8 @@ func (opts *RunOptions) parseKraftfileEnv(_ context.Context, project app.Applica
continue
}

hostVal := os.Getenv(k)
if hostVal != "" {
machine.Spec.Env[k] = hostVal
if v, ok := os.LookupEnv(k); ok {
machine.Spec.Env[k] = v
}
}

Expand All @@ -444,11 +446,11 @@ func (opts *RunOptions) parseEnvs(_ context.Context, machine *machineapi.Machine
}

for _, env := range opts.Env {
if strings.ContainsRune(env, '=') {
split := strings.SplitN(env, "=", 2)
machine.Spec.Env[split[0]] = split[1]
} else {
machine.Spec.Env[env] = os.Getenv(env)
k, v, ok := strings.Cut(env, "=")
if ok {
machine.Spec.Env[k] = v
} else if v, ok := os.LookupEnv(k); ok {
machine.Spec.Env[k] = v
}
}

Expand Down

0 comments on commit fc6cee1

Please sign in to comment.