mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
Fix panic for empty input to Swift cataloger (#2226)
* survive invalid input in swift parser Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add empty file Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> --------- Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
parent
144ed725a7
commit
31f1d7dbf0
@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/anchore/syft/internal/log"
|
||||
"github.com/anchore/syft/syft/artifact"
|
||||
"github.com/anchore/syft/syft/file"
|
||||
"github.com/anchore/syft/syft/pkg"
|
||||
@ -67,7 +68,17 @@ func parsePackageResolved(_ file.Resolver, _ *generic.Environment, reader file.L
|
||||
}
|
||||
}
|
||||
|
||||
var pins, err = pinsForVersion(packageResolvedData, packageResolvedData["version"].(float64))
|
||||
if packageResolvedData["version"] == nil {
|
||||
log.Trace("no version found in Package.resolved file, skipping")
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
version, ok := packageResolvedData["version"].(float64)
|
||||
if !ok {
|
||||
return nil, nil, fmt.Errorf("failed to parse Package.resolved file: version is not a number")
|
||||
}
|
||||
|
||||
var pins, err = pinsForVersion(packageResolvedData, version)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
@ -1,8 +1,12 @@
|
||||
package swift
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/anchore/syft/syft/artifact"
|
||||
"github.com/anchore/syft/syft/file"
|
||||
"github.com/anchore/syft/syft/pkg"
|
||||
@ -80,3 +84,24 @@ func TestParsePackageResolved(t *testing.T) {
|
||||
|
||||
pkgtest.TestFileParser(t, fixture, parsePackageResolved, expectedPkgs, expectedRelationships)
|
||||
}
|
||||
|
||||
func TestParsePackageResolved_empty(t *testing.T) {
|
||||
// regression for https://github.com/anchore/syft/issues/2225
|
||||
fixture := "test-fixtures/empty-packages.resolved"
|
||||
|
||||
pkgtest.TestFileParser(t, fixture, parsePackageResolved, nil, nil)
|
||||
|
||||
dir := t.TempDir()
|
||||
fixture = filepath.Join(dir, "Package.resolved")
|
||||
_, err := os.Create(fixture)
|
||||
require.NoError(t, err)
|
||||
|
||||
pkgtest.TestFileParser(t, fixture, parsePackageResolved, nil, nil)
|
||||
}
|
||||
|
||||
func TestParsePackageResolved_versionNotANumber(t *testing.T) {
|
||||
// regression for https://github.com/anchore/syft/issues/2225
|
||||
fixture := "test-fixtures/bad-version-packages.resolved"
|
||||
|
||||
pkgtest.NewCatalogTester().FromFile(t, fixture).WithError().TestParser(t, parsePackageResolved)
|
||||
}
|
||||
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"version" : "2"
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
{}
|
||||
Loading…
x
Reference in New Issue
Block a user