From 961cf9d3b17f975db7013b8410d541f6fdfcf249 Mon Sep 17 00:00:00 2001 From: nobody <59990325+vrnobody@users.noreply.github.com> Date: Thu, 11 Jan 2024 23:34:26 +0800 Subject: [PATCH] Remove slices dependency. (#2930) * Remove slices dependency. * Fix nil pointer dereference bug. --------- Co-authored-by: nobody --- common/reflect/marshal.go | 47 +++++++++++++++++++++------------------ core/config.go | 10 +++++---- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/common/reflect/marshal.go b/common/reflect/marshal.go index 96e83351..736afc01 100644 --- a/common/reflect/marshal.go +++ b/common/reflect/marshal.go @@ -3,7 +3,6 @@ package reflect import ( "encoding/json" "reflect" - "slices" cserial "github.com/xtls/xray-core/common/serial" ) @@ -18,6 +17,9 @@ func MarshalToJson(v interface{}) (string, bool) { } func marshalTypedMessage(v *cserial.TypedMessage, ignoreNullValue bool) interface{} { + if v == nil { + return nil + } tmsg, err := v.GetInstance() if err != nil { return nil @@ -111,28 +113,29 @@ func marshalKnownType(v interface{}, ignoreNullValue bool) (interface{}, bool) { } } -var valueKinds = []reflect.Kind{ - reflect.Bool, - reflect.Int, - reflect.Int8, - reflect.Int16, - reflect.Int32, - reflect.Int64, - reflect.Uint, - reflect.Uint8, - reflect.Uint16, - reflect.Uint32, - reflect.Uint64, - reflect.Uintptr, - reflect.Float32, - reflect.Float64, - reflect.Complex64, - reflect.Complex128, - reflect.String, -} - func isValueKind(kind reflect.Kind) bool { - return slices.Contains(valueKinds, kind) + switch kind { + case reflect.Bool, + reflect.Int, + reflect.Int8, + reflect.Int16, + reflect.Int32, + reflect.Int64, + reflect.Uint, + reflect.Uint8, + reflect.Uint16, + reflect.Uint32, + reflect.Uint64, + reflect.Uintptr, + reflect.Float32, + reflect.Float64, + reflect.Complex64, + reflect.Complex128, + reflect.String: + return true + default: + return false + } } func marshalInterface(v interface{}, ignoreNullValue bool) interface{} { diff --git a/core/config.go b/core/config.go index ec5ad6a4..1aa21f5f 100644 --- a/core/config.go +++ b/core/config.go @@ -2,7 +2,6 @@ package core import ( "io" - "slices" "strings" "github.com/xtls/xray-core/common" @@ -60,9 +59,12 @@ func GetMergedConfig(args cmdarg.Arg) (string, error) { supported := []string{"json", "yaml", "toml"} for _, file := range args { format := getFormat(file) - if slices.Contains(supported, format) { - files = append(files, file) - formats = append(formats, format) + for _, s := range supported { + if s == format { + files = append(files, file) + formats = append(formats, format) + break + } } } return ConfigMergedFormFiles(files, formats)