From e7a7b3f98dd4622c02682a6597f3b6b1eb27aeba Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Wed, 25 May 2022 14:47:14 +0200 Subject: [PATCH] Use ByteSliceToString from golang.org/x/sys/unix Use unix.ByteSliceToString to convert Utsname []byte fields to strings. This also allows to drop the charsToString helper which serves the same purpose and matches ByteSliceToString's implementation. Signed-off-by: Tobias Klauser --- sdk/resource/export_common_unix_test.go | 1 - sdk/resource/os_unix.go | 20 +++++--------------- sdk/resource/os_unix_test.go | 24 ------------------------ 3 files changed, 5 insertions(+), 40 deletions(-) diff --git a/sdk/resource/export_common_unix_test.go b/sdk/resource/export_common_unix_test.go index 3b8b03cf43e..a296ff6f245 100644 --- a/sdk/resource/export_common_unix_test.go +++ b/sdk/resource/export_common_unix_test.go @@ -19,7 +19,6 @@ package resource // import "go.opentelemetry.io/otel/sdk/resource" var ( Uname = uname - CharsToString = charsToString GetFirstAvailableFile = getFirstAvailableFile ) diff --git a/sdk/resource/os_unix.go b/sdk/resource/os_unix.go index 42894a15b5c..1c84afc1852 100644 --- a/sdk/resource/os_unix.go +++ b/sdk/resource/os_unix.go @@ -18,7 +18,6 @@ package resource // import "go.opentelemetry.io/otel/sdk/resource" import ( - "bytes" "fmt" "os" @@ -69,23 +68,14 @@ func uname() (string, error) { } return fmt.Sprintf("%s %s %s %s %s", - charsToString(utsName.Sysname[:]), - charsToString(utsName.Nodename[:]), - charsToString(utsName.Release[:]), - charsToString(utsName.Version[:]), - charsToString(utsName.Machine[:]), + unix.ByteSliceToString(utsName.Sysname[:]), + unix.ByteSliceToString(utsName.Nodename[:]), + unix.ByteSliceToString(utsName.Release[:]), + unix.ByteSliceToString(utsName.Version[:]), + unix.ByteSliceToString(utsName.Machine[:]), ), nil } -// charsToString converts a C-like null-terminated char array to a Go string. -func charsToString(charArray []byte) string { - if i := bytes.IndexByte(charArray, 0); i >= 0 { - charArray = charArray[:i] - } - - return string(charArray) -} - // getFirstAvailableFile returns an *os.File of the first available // file from a list of candidate file paths. func getFirstAvailableFile(candidates []string) (*os.File, error) { diff --git a/sdk/resource/os_unix_test.go b/sdk/resource/os_unix_test.go index af6178613e1..d6522d01dd0 100644 --- a/sdk/resource/os_unix_test.go +++ b/sdk/resource/os_unix_test.go @@ -64,30 +64,6 @@ func TestUnameError(t *testing.T) { resource.SetDefaultUnameProvider() } -func TestCharsToString(t *testing.T) { - tt := []struct { - Name string - Bytes []byte - Expected string - }{ - {"Nil array", nil, ""}, - {"Empty array", []byte{}, ""}, - {"Empty string (null terminated)", []byte{0x00}, ""}, - {"Nonempty string (null terminated)", []byte{0x31, 0x32, 0x33, 0x00}, "123"}, - {"Nonempty string (non-null terminated)", []byte{0x31, 0x32, 0x33}, "123"}, - {"Nonempty string with values after null", []byte{0x31, 0x32, 0x33, 0x00, 0x34}, "123"}, - } - - for _, tc := range tt { - tc := tc - - t.Run(tc.Name, func(t *testing.T) { - result := resource.CharsToString(tc.Bytes) - require.EqualValues(t, tc.Expected, result) - }) - } -} - func TestGetFirstAvailableFile(t *testing.T) { tempDir := t.TempDir()