avoid panic when storing a nil pointer in cached_mysql (#8020)

related to #7420, this improves the logic of the clone function in cached_mysql to properly handle nil and nil pointers.
This commit is contained in:
Roberto Dip
2022-10-03 13:29:20 -03:00
committed by GitHub
parent cd2ab6b17c
commit c0bb0cc4e7
3 changed files with 20 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
* Fixed a server panic happening when a team was edited via yaml without an `agent_options` key.
@@ -35,6 +35,10 @@ func clone(v interface{}) (interface{}, error) {
return cloner.Clone()
}
if v == nil {
return nil, nil
}
// Use reflection to initialize a clone of v of the same type.
vv := reflect.ValueOf(v)
@@ -43,6 +47,9 @@ func clone(v interface{}) (interface{}, error) {
isPtr := false
if vv.Kind() == reflect.Ptr {
isPtr = true
if vv.IsNil() {
return nil, nil
}
vv = vv.Elem()
}
@@ -15,6 +15,8 @@ import (
)
func TestClone(t *testing.T) {
var nilRawMessage *json.RawMessage
tests := []struct {
name string
src interface{}
@@ -61,6 +63,16 @@ func TestClone(t *testing.T) {
src: &[]string{"foo", "bar"},
want: &[]string{"foo", "bar"},
},
{
name: "nil",
src: nil,
want: nil,
},
{
name: "nil pointer",
src: nilRawMessage,
want: nil,
},
}
for _, tc := range tests {