From 66fd8d5ec8b1f8b83f69966075a97cfb74bcb257 Mon Sep 17 00:00:00 2001 From: okatu-loli Date: Fri, 12 Sep 2025 14:22:13 +0800 Subject: [PATCH] fix(storage): Fixed a potential null value issue with not checking firstMount. - Added a check to see if `firstMount` is null to prevent logic errors. - Adjusted the loading logic of `GetAllRoles` and `GetAllUsers` to only execute when `firstMount` is non-null. - Fixed the `usedBy` check logic to ensure that an error message is returned under the correct conditions. - Optimized code structure to reduce unnecessary execution paths. --- internal/op/storage.go | 44 ++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/internal/op/storage.go b/internal/op/storage.go index c4cc1ee1..dfb305aa 100644 --- a/internal/op/storage.go +++ b/internal/op/storage.go @@ -280,30 +280,32 @@ func DeleteStorageById(ctx context.Context, id uint) error { return errors.WithMessage(err, "failed get storage") } firstMount := firstPathSegment(storage.MountPath) - roles, err := db.GetAllRoles() - if err != nil { - return errors.WithMessage(err, "failed to load roles") - } - users, err := db.GetAllUsers() - if err != nil { - return errors.WithMessage(err, "failed to load users") - } - var usedBy []string - for _, r := range roles { - for _, entry := range r.PermissionScopes { - if firstPathSegment(entry.Path) == firstMount { - usedBy = append(usedBy, "role:"+r.Name) - break + if firstMount != "" { + roles, err := db.GetAllRoles() + if err != nil { + return errors.WithMessage(err, "failed to load roles") + } + users, err := db.GetAllUsers() + if err != nil { + return errors.WithMessage(err, "failed to load users") + } + var usedBy []string + for _, r := range roles { + for _, entry := range r.PermissionScopes { + if firstPathSegment(entry.Path) == firstMount { + usedBy = append(usedBy, "role:"+r.Name) + break + } } } - } - for _, u := range users { - if firstPathSegment(u.BasePath) == firstMount { - usedBy = append(usedBy, "user:"+u.Username) + for _, u := range users { + if firstPathSegment(u.BasePath) == firstMount { + usedBy = append(usedBy, "user:"+u.Username) + } + } + if len(usedBy) > 0 { + return errors.Errorf("storage is used by %s, please cancel usage first", strings.Join(usedBy, ", ")) } - } - if len(usedBy) > 0 { - return errors.Errorf("storage is used by %s, please cancel usage first", strings.Join(usedBy, ", ")) } if !storage.Disabled { storageDriver, err := GetStorageByMountPath(storage.MountPath)