fix(boltdb): remove undefined behavior when deleting objects while iterating EE-5643 (#9129)

pull/9132/head
andres-portainer 2023-06-27 16:42:52 -03:00 committed by GitHub
parent c96e076871
commit f1f46f4da1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 4 deletions

View File

@ -47,6 +47,8 @@ func (tx *DbTransaction) DeleteObject(bucketName string, key []byte) error {
} }
func (tx *DbTransaction) DeleteAllObjects(bucketName string, obj interface{}, matchingFn func(o interface{}) (id int, ok bool)) error { func (tx *DbTransaction) DeleteAllObjects(bucketName string, obj interface{}, matchingFn func(o interface{}) (id int, ok bool)) error {
var ids []int
bucket := tx.tx.Bucket([]byte(bucketName)) bucket := tx.tx.Bucket([]byte(bucketName))
cursor := bucket.Cursor() cursor := bucket.Cursor()
@ -57,11 +59,14 @@ func (tx *DbTransaction) DeleteAllObjects(bucketName string, obj interface{}, ma
} }
if id, ok := matchingFn(obj); ok { if id, ok := matchingFn(obj); ok {
err := bucket.Delete(tx.conn.ConvertToKey(id)) ids = append(ids, id)
if err != nil {
return err
} }
} }
for _, id := range ids {
if err := bucket.Delete(tx.conn.ConvertToKey(id)); err != nil {
return err
}
} }
return nil return nil