Language: Go
Label: safe
CWE: CWE-362 \u2014 Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')
The product contains a code sequence that can run concurrently with other code, and the code sequence requires temporary, exclusive access to a shared resource, but a timing window exists in which the shared resource can be modified by another code sequence that is operating concurrently.

Code:
func (cr *collectionRepo) AddCollection(ctx context.Context, collection *entity.Collection) (err error) {
	_, err = cr.data.DB.Transaction(func(session *xorm.Session) (result any, err error) {
		var has bool
		dbcollection := &entity.Collection{}
		result = nil
		has, err = session.Where("user_id = ? and object_id = ?", collection.UserID, collection.ObjectID).Get(dbcollection)
		if err != nil {
			return
		}
		if has {
			return
		}
		id, err := cr.uniqueIDRepo.GenUniqueIDStr(ctx, collection.TableName())
		if err == nil {
			collection.ID = id
			_, err = session.Insert(collection)
			if err != nil {
				return result, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
			}
		}
		return
	})
	return err
}