Skip to content

Commit 5941830

Browse files
authored
Merge pull request #464 from aniket866/fix/Nil-Pointer-Dereference
fix (cmd,pkg) : Potential Panic/Crash via Nil Pointer Dereference in Switch Context Command
2 parents db5ed0f + a910fc5 commit 5941830

3 files changed

Lines changed: 15 additions & 3 deletions

File tree

cmd/context.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ microcks context http://localhost:8080 --delete`,
4747
}
4848

4949
ctxName := args[0]
50+
errors.CheckConfigNil(localCfg == nil, configPath)
5051
if localCfg.CurrentContext == ctxName {
5152
fmt.Printf("Already at context '%s'\n", localCfg.CurrentContext)
5253
return
@@ -100,9 +101,7 @@ func deleteContext(context, configPath string) error {
100101
func printMicrocksContexts(configPath string) {
101102
localCfg, err := config.ReadLocalConfig(configPath)
102103
errors.CheckError(err)
103-
if localCfg == nil {
104-
log.Fatalf("No contexts defined in %s", configPath)
105-
}
104+
errors.CheckConfigNil(localCfg == nil, configPath)
106105
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
107106
defer func() { _ = w.Flush() }()
108107
columnNames := []string{"CURRENT", "NAME", "SERVER"}

cmd/context_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,9 @@ func TestDeleteContext(t *testing.T) {
6464
_, err = config.ReadLocalConfig(testConfigFilePath)
6565
require.NoError(t, err)
6666
}
67+
68+
func TestDeleteContextEmpty(t *testing.T) {
69+
err := deleteContext("http://localhost:8080", "./testdata/non-existent-file.config")
70+
require.EqualError(t, err, "Nothing to logout from")
71+
}
72+

pkg/errors/error.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ func CheckError(err error) {
2424
}
2525
}
2626

27+
func CheckConfigNil(isNil bool, path string) {
28+
if isNil {
29+
Fatal(ErrorGeneric, "No contexts defined in "+path)
30+
}
31+
}
32+
33+
2734
// Fatal is a wrapper for log.Fatal() to exit with custom code
2835
func Fatal(exitcode int, args ...interface{}) {
2936
log.Println(args...)

0 commit comments

Comments
 (0)