Skip to content

Commit

Permalink
newt: Fix config check for entries with description
Browse files Browse the repository at this point in the history
When "description" key was included in entry's Value field
it was causing that interface was holding other
type then we expected and newt was crashing.
Now we extract only "value" key from Value
interface and call reflect.DeepEqual, which will
prevent crashing.
  • Loading branch information
m-gorecki committed Sep 9, 2024
1 parent c7afc5f commit e12a7be
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
16 changes: 14 additions & 2 deletions newt/ycfg/ycfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package ycfg
import (
"fmt"
"mynewt.apache.org/newt/newt/cfgv"
"reflect"
"strings"

"github.com/spf13/cast"
Expand Down Expand Up @@ -470,8 +471,19 @@ func (yc *YCfg) GetStringMap(
}

if _, exists := result[k]; exists {
if (entry.Value != result[k].Value) && (result[k].Expr != nil) {
return nil, fmt.Errorf("Setting %s collision - two conditions true:\n[%s, %s]\n"+
entryInterfaceMap, ok := entry.Value.(map[interface{}]interface{})
if !ok {
return nil, fmt.Errorf("internal error: unexpected interface type")
}
resultInterfaceMap, ok := result[k].Value.(map[interface{}]interface{})
if !ok {
return nil, fmt.Errorf("internal error: unexpected intreface type")
}

entryVal := util.GetInterfaceValueFromMap(entryInterfaceMap, "value")
resultVal := util.GetInterfaceValueFromMap(resultInterfaceMap, "value")
if !reflect.DeepEqual(entryVal, resultVal) && (result[k].Expr != nil) && (entry.Expr != nil) {
return nil, fmt.Errorf("setting %s collision - two conditions true:\n[%s, %s]\n"+
"Conflicting file: %s",
k, entry.Expr.String(), result[k].Expr.String(), yc.name)
}
Expand Down
8 changes: 8 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,3 +1044,11 @@ func SliceContains(slice interface{}, elem interface{}) bool {
}
return false
}

func GetInterfaceValueFromMap(m map[interface{}]interface{}, k string) interface{} {
if v, exists := m[k]; exists {
return v
}

return nil
}

0 comments on commit e12a7be

Please sign in to comment.