From 5f58ead835ea50046d08e7f30fb45c6961c4bde2 Mon Sep 17 00:00:00 2001 From: rootkiller6788 <17553215+rootkiller6788@user.noreply.gitee.com> Date: Mon, 14 Sep 2026 20:58:19 +0800 Subject: [PATCH 1/2] analyzer: fix nil deref in -output=package with a custom capability map The package output mode uses capabilityInfo.Capability.String() as the map key. Capability is the enum field, and it stays unset when the capability name comes from a user-supplied capability map and isn't one of the Capability values, so oldCapability returns nil and String() dereferences it. Any capability map that defines its own capability name reproduces it. Use GetCapabilityName() instead, which is what the other output modes and the diff tooling already use. --- analyzer/scan.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/analyzer/scan.go b/analyzer/scan.go index f40f9d8..60ee9cb 100644 --- a/analyzer/scan.go +++ b/analyzer/scan.go @@ -79,12 +79,15 @@ func RunCapslock(args []string, output string, pkgs []*packages.Package, queried // Build a map of packages to unique capabilities. capabilitiesByPackageSets := make(map[string]map[string]struct{}) for _, capabilityInfo := range cil.CapabilityInfo { + // The Capability enum field is unset when the capability is a + // caller-defined one that isn't in the enum, so use the name instead. + capability := capabilityInfo.GetCapabilityName() for _, path := range capabilityInfo.Path { if _, ok := capabilitiesByPackageSets[*path.Package]; ok { - capabilitiesByPackageSets[*path.Package][capabilityInfo.Capability.String()] = struct{}{} + capabilitiesByPackageSets[*path.Package][capability] = struct{}{} } else { capabilitiesByPackageSets[*path.Package] = map[string]struct{}{ - capabilityInfo.Capability.String(): {}, + capability: {}, } } } From e7b87485f14b90c658e245a4d2f142b0af6076e4 Mon Sep 17 00:00:00 2001 From: rootkiller6788 <17553215+rootkiller6788@user.noreply.gitee.com> Date: Mon, 14 Sep 2026 20:58:22 +0800 Subject: [PATCH 2/2] testing: regression test for -output=package with a custom capability map Runs capslock over testpkgs/callos with a map that gives os.Getpid a capability name that isn't in the Capability enum, and checks that the package output reports it for that package. This panicked before the previous commit. --- testing/analyzepackages_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/testing/analyzepackages_test.go b/testing/analyzepackages_test.go index 129cee0..d1dd0fd 100644 --- a/testing/analyzepackages_test.go +++ b/testing/analyzepackages_test.go @@ -8,11 +8,14 @@ package analyzepackages_test import ( "bytes" + "encoding/json" "fmt" "log" "os" "os/exec" + "path/filepath" "regexp" + "slices" "strings" "sync" "testing" @@ -464,6 +467,35 @@ func TestCompare(t *testing.T) { } } +// TestPackageOutput checks that -output=package works when the capability map +// defines a capability that isn't one of the Capability enum values. The +// Capability field is left unset for those capabilities, so the package output +// mode has to report the capability name instead. +func TestPackageOutput(t *testing.T) { + name := filepath.Join(t.TempDir(), "custom.cm") + if err := os.WriteFile(name, []byte("func os.Getpid TESTCAP\n"), 0o600); err != nil { + t.Fatalf("writing capability map: %v", err) + } + cmd := exec.Command(bin, "-packages=../testpkgs/callos", "-output=package", "-capability_map="+name) + var output bytes.Buffer + cmd.Stdout = &output + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + t.Fatalf("running capslock: %v", err) + } + got := make(map[string][]string) + if err := json.Unmarshal(output.Bytes(), &got); err != nil { + t.Fatalf("parsing output %q: %v", output.String(), err) + } + caps, ok := got["github.com/google/capslock/testpkgs/callos"] + if !ok { + t.Fatalf("output %q does not mention the callos package", output.String()) + } + if !slices.Contains(caps, "TESTCAP") { + t.Errorf("capabilities of the callos package are %v, want them to include TESTCAP", caps) + } +} + func TestVersionFlag(t *testing.T) { out, err := exec.Command(bin, "-version").CombinedOutput() if err != nil {