From 160ef74363d246eb9aa87eb13a8ee985446106af Mon Sep 17 00:00:00 2001 From: Giteabot Date: Tue, 12 Dec 2023 23:35:32 +0800 Subject: [PATCH] Fix possible nil pointer access (#28428) (#28440) Backport #28428 by @KN4CK3R There could be a nil pointer exception if the file is not found because that specific error is suppressed but not handled. Co-authored-by: KN4CK3R (cherry picked from commit 97a0bf151a1fed2119b0ecb3c94f68462979c699) --- services/packages/alpine/repository.go | 12 ++++-------- services/packages/container/cleanup.go | 6 ++---- services/packages/debian/repository.go | 19 +++++++------------ services/packages/rpm/repository.go | 5 +---- 4 files changed, 14 insertions(+), 28 deletions(-) diff --git a/services/packages/alpine/repository.go b/services/packages/alpine/repository.go index 46093d5bdd..30b7a06eb3 100644 --- a/services/packages/alpine/repository.go +++ b/services/packages/alpine/repository.go @@ -82,10 +82,7 @@ func BuildAllRepositoryFiles(ctx context.Context, ownerID int64) error { } for _, pf := range pfs { - if err := packages_model.DeleteAllProperties(ctx, packages_model.PropertyTypeFile, pf.ID); err != nil { - return err - } - if err := packages_model.DeleteFileByID(ctx, pf.ID); err != nil { + if err := packages_service.DeletePackageFile(ctx, pf); err != nil { return err } } @@ -157,12 +154,11 @@ func buildPackagesIndex(ctx context.Context, ownerID int64, repoVersion *package pf, err := packages_model.GetFileForVersionByName(ctx, repoVersion.ID, IndexFilename, fmt.Sprintf("%s|%s|%s", branch, repository, architecture)) if err != nil && !errors.Is(err, util.ErrNotExist) { return err + } else if pf == nil { + return nil } - if err := packages_model.DeleteAllProperties(ctx, packages_model.PropertyTypeFile, pf.ID); err != nil { - return err - } - return packages_model.DeleteFileByID(ctx, pf.ID) + return packages_service.DeletePackageFile(ctx, pf) } // Cache data needed for all repository files diff --git a/services/packages/container/cleanup.go b/services/packages/container/cleanup.go index 1a9ef26391..dd3f158dbf 100644 --- a/services/packages/container/cleanup.go +++ b/services/packages/container/cleanup.go @@ -11,6 +11,7 @@ import ( container_model "code.gitea.io/gitea/models/packages/container" container_module "code.gitea.io/gitea/modules/packages/container" "code.gitea.io/gitea/modules/util" + packages_service "code.gitea.io/gitea/services/packages" digest "github.com/opencontainers/go-digest" ) @@ -47,10 +48,7 @@ func cleanupExpiredUploadedBlobs(ctx context.Context, olderThan time.Duration) e } for _, pf := range pfs { - if err := packages_model.DeleteAllProperties(ctx, packages_model.PropertyTypeFile, pf.ID); err != nil { - return err - } - if err := packages_model.DeleteFileByID(ctx, pf.ID); err != nil { + if err := packages_service.DeletePackageFile(ctx, pf); err != nil { return err } } diff --git a/services/packages/debian/repository.go b/services/packages/debian/repository.go index 373fa7abc9..b968ac2f81 100644 --- a/services/packages/debian/repository.go +++ b/services/packages/debian/repository.go @@ -110,10 +110,7 @@ func BuildAllRepositoryFiles(ctx context.Context, ownerID int64) error { } for _, pf := range pfs { - if err := packages_model.DeleteAllProperties(ctx, packages_model.PropertyTypeFile, pf.ID); err != nil { - return err - } - if err := packages_model.DeleteFileByID(ctx, pf.ID); err != nil { + if err := packages_service.DeletePackageFile(ctx, pf); err != nil { return err } } @@ -182,12 +179,11 @@ func buildPackagesIndices(ctx context.Context, ownerID int64, repoVersion *packa pf, err := packages_model.GetFileForVersionByName(ctx, repoVersion.ID, filename, key) if err != nil && !errors.Is(err, util.ErrNotExist) { return err + } else if pf == nil { + continue } - if err := packages_model.DeleteAllProperties(ctx, packages_model.PropertyTypeFile, pf.ID); err != nil { - return err - } - if err := packages_model.DeleteFileByID(ctx, pf.ID); err != nil { + if err := packages_service.DeletePackageFile(ctx, pf); err != nil { return err } } @@ -285,12 +281,11 @@ func buildReleaseFiles(ctx context.Context, ownerID int64, repoVersion *packages pf, err := packages_model.GetFileForVersionByName(ctx, repoVersion.ID, filename, distribution) if err != nil && !errors.Is(err, util.ErrNotExist) { return err + } else if pf == nil { + continue } - if err := packages_model.DeleteAllProperties(ctx, packages_model.PropertyTypeFile, pf.ID); err != nil { - return err - } - if err := packages_model.DeleteFileByID(ctx, pf.ID); err != nil { + if err := packages_service.DeletePackageFile(ctx, pf); err != nil { return err } } diff --git a/services/packages/rpm/repository.go b/services/packages/rpm/repository.go index c9db0247f6..7e718d321f 100644 --- a/services/packages/rpm/repository.go +++ b/services/packages/rpm/repository.go @@ -148,10 +148,7 @@ func BuildRepositoryFiles(ctx context.Context, ownerID int64) error { return err } for _, pf := range pfs { - if err := packages_model.DeleteAllProperties(ctx, packages_model.PropertyTypeFile, pf.ID); err != nil { - return err - } - if err := packages_model.DeleteFileByID(ctx, pf.ID); err != nil { + if err := packages_service.DeletePackageFile(ctx, pf); err != nil { return err } }