Skip to content

Commit 0aebf2b

Browse files
committed
SCA: Improve determineShlibVersion to handle corner cases
During my tests I discovered that ResolvePackage will happily include obsolete package versions in the list of candidates it returns. For that reason, we need to sort the list using the package's BuildTime in order to get the latest one. Also, we need to relax the error handling when no suitable package candidate has been found for a versioned shared library, because we will often be dealing with cases when the library is actually vendorized by the package being built. Signed-off-by: Sergio Durigan Junior <[email protected]>
1 parent d9beca0 commit 0aebf2b

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

pkg/sca/sca.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,9 @@ func dereferenceCrossPackageSymlink(hdl SCAHandle, path string, extraLibDirs []s
292292
// returned. We can't do versioned "depends:" unless there is a
293293
// matching "provides:".
294294
//
295-
// If no match is found, that's an error.
295+
// If no match is found, then we assume that the shared library is
296+
// vendored by the package being built. In this case, we return an
297+
// empty version string.
296298
func determineShlibVersion(ctx context.Context, hdl SCAHandle, shlib string) (string, error) {
297299
log := clog.FromContext(ctx)
298300

@@ -340,6 +342,14 @@ func determineShlibVersion(ctx context.Context, hdl SCAHandle, shlib string) (st
340342
return "", err
341343
}
342344

345+
// ResolvePackage will return *all* packages that provide
346+
// shlib, including those that have been obsoleted. For that
347+
// reason, we need sort the list of candidates by build time,
348+
// making sure that we're always considering the newest ones.
349+
slices.SortFunc(candidates, func(p1 *apk.RepositoryPackage, p2 *apk.RepositoryPackage) int {
350+
return p2.BuildTime.Compare(p1.BuildTime)
351+
})
352+
343353
// Obtain the list of packages currently installed in the
344354
// build environment.
345355
pkgVersionMap := hdl.InstalledPackages()
@@ -415,7 +425,21 @@ func determineShlibVersion(ctx context.Context, hdl SCAHandle, shlib string) (st
415425
}
416426
}
417427

418-
return "", fmt.Errorf("could not find suitable package providing library so:%s", shlib)
428+
// If we're here, then one of the following scenarios happened:
429+
//
430+
// - The list of candidates is empty. This happens when we're
431+
// dealing with a vendored shared library that isn't
432+
// provided by any of our packages. Just return an empty
433+
// version string.
434+
//
435+
// - There is a list of candidates, but none of them provides
436+
// the version of shared library we're interested in. This
437+
// has been seen to happen when the package being built uses
438+
// a vendored library that *is* provided by one of our
439+
// packages, but the vendored version is higher than the
440+
// non-vendored one. This is also a case of vendorization,
441+
// so we return an empty version string.
442+
return "", nil
419443
}
420444

421445
func processSymlinkSo(ctx context.Context, hdl SCAHandle, path string, generated *config.Dependencies, extraLibDirs []string) error {

0 commit comments

Comments
 (0)