From aa6918cc2226833a08e0d159802d797777ae25c3 Mon Sep 17 00:00:00 2001 From: Michael Habinsky Date: Wed, 6 May 2026 01:48:47 -0600 Subject: [PATCH] Avoid indexing errors by using strings.HasSuffix --- component/attr_cache/attr_cache_test.go | 6 +++--- component/azstorage/block_blob.go | 3 +-- component/s3storage/client.go | 2 +- component/s3storage/s3wrappers.go | 6 ++---- internal/component_options.go | 14 +++----------- 5 files changed, 10 insertions(+), 21 deletions(-) diff --git a/component/attr_cache/attr_cache_test.go b/component/attr_cache/attr_cache_test.go index 5ebfe446a..7e876b60f 100644 --- a/component/attr_cache/attr_cache_test.go +++ b/component/attr_cache/attr_cache_test.go @@ -109,7 +109,7 @@ func (suite *attrCacheTestSuite) assertNotInCache(path string) { } func (suite *attrCacheTestSuite) addPathToCache(path string, metadata bool) { - isDir := path[len(path)-1] == '/' + isDir := strings.HasSuffix(path, "/") path = internal.TruncateDirName(path) pathAttr := getPathAttr(path, defaultSize, fs.FileMode(defaultMode), metadata) if isDir { @@ -241,7 +241,7 @@ func generateNestedPathAttr(path string, size int64, mode os.FileMode) []*intern pathAttrs := make([]*internal.ObjAttr, 0) for p := a.Front(); p != nil; p = p.Next() { pString := p.Value.(string) - isDir := pString[len(pString)-1] == '/' + isDir := strings.HasSuffix(pString, "/") pString = internal.TruncateDirName(pString) newPathAttr := getPathAttr(pString, size, mode, true) if isDir { @@ -1321,7 +1321,7 @@ func (suite *attrCacheTestSuite) TestSyncDir() { // directory cache is enabled, so a dir paths should NOT be invalid for p := a.Front(); p != nil; p = p.Next() { path := p.Value.(string) - isDir := path[len(path)-1] == '/' + isDir := strings.HasSuffix(path, "/") truncatedPath = internal.TruncateDirName(path) if isDir { suite.assertUntouched(truncatedPath) diff --git a/component/azstorage/block_blob.go b/component/azstorage/block_blob.go index b200f8153..12dec586a 100644 --- a/component/azstorage/block_blob.go +++ b/component/azstorage/block_blob.go @@ -721,8 +721,7 @@ func (bb *BlockBlob) List( func (bb *BlockBlob) getListPath(prefix string) string { listPath := bb.getFormattedPath(prefix) - if (prefix != "" && prefix[len(prefix)-1] == '/') || - (prefix == "" && bb.Config.prefixPath != "") { + if strings.HasSuffix(prefix, "/") || (prefix == "" && bb.Config.prefixPath != "") { listPath += "/" } return listPath diff --git a/component/s3storage/client.go b/component/s3storage/client.go index dc56789d9..5d3155480 100644 --- a/component/s3storage/client.go +++ b/component/s3storage/client.go @@ -625,7 +625,7 @@ func (cl *Client) RenameDirectory(source string, target string) error { // If name is a directory, the trailing slash is optional. func (cl *Client) GetAttr(name string) (*internal.ObjAttr, error) { log.Trace("Client::GetAttr : name %s", name) - explicitDirLookup := len(name) > 0 && name[len(name)-1] == '/' + explicitDirLookup := strings.HasSuffix(name, "/") dirName := internal.ExtendDirName(name) // first let's suppose the caller is looking for a file diff --git a/component/s3storage/s3wrappers.go b/component/s3storage/s3wrappers.go index 810272815..e7ebd1e1d 100644 --- a/component/s3storage/s3wrappers.go +++ b/component/s3storage/s3wrappers.go @@ -429,16 +429,14 @@ func (cl *Client) List( // combine the configured prefix and the prefix being given to List to get a full listPath listPath := cl.getKey(prefix, false, false) // replace any trailing forward slash stripped by common.JoinUnixFilepath - if (prefix != "" && prefix[len(prefix)-1] == '/') || - (prefix == "" && cl.Config.prefixPath != "") { + if strings.HasSuffix(prefix, "/") || (prefix == "" && cl.Config.prefixPath != "") { listPath += "/" } // Only look for CommonPrefixes (subdirectories) if List was called with a prefix ending in a slash. // If prefix does not end in a slash, CommonPrefixes would find unwanted results. // For example, it would find "filet-of-fish/" when searching for "file". - // Check for an empty path to prevent indexing to [-1] - findCommonPrefixes := listPath == "" || listPath[len(listPath)-1] == '/' + findCommonPrefixes := strings.HasSuffix(listPath, "/") var nextMarker *string var token *string diff --git a/internal/component_options.go b/internal/component_options.go index 5ec31de92..35a6d0702 100644 --- a/internal/component_options.go +++ b/internal/component_options.go @@ -27,6 +27,7 @@ package internal import ( "os" + "strings" "github.com/Seagate/cloudfuse/internal/handlemap" ) @@ -198,20 +199,11 @@ type CommittedBlock struct { type CommittedBlockList []CommittedBlock func TruncateDirName(name string) string { - if len(name) == 0 { - return "" - } - if name[len(name)-1:] == "/" { - name = name[:len(name)-1] - } - return name + return strings.TrimSuffix(name, "/") } func ExtendDirName(name string) string { - if len(name) == 0 { - return "/" - } - if name[len(name)-1:] != "/" { + if !strings.HasSuffix(name, "/") { name = name + "/" } return name