Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

analyzer: fix nil deref in -output=package with a custom capability map - #325

Open
rootkiller6788 wants to merge 2 commits into
google:mainfrom
rootkiller6788:fix-package-output-nil-capability
Open

rootkiller6788 wants to merge 2 commits into
google:mainfrom
rootkiller6788:fix-package-output-nil-capability

Conversation

@rootkiller6788

Copy link
Copy Markdown

-output=package crashes when the capability map defines its own capability name:

$ cat my.cm
func os.Getpid TESTCAP
$ capslock -packages=./... -output=package -capability_map=my.cm
panic: runtime error: invalid memory address or nil pointer dereference
        analyzer/scan.go:87

The package output builds its map with capabilityInfo.Capability.String(). Capability is the enum field, and it stays unset for a capability name that comes from the user's map and isn't in the enum, so oldCapability returns nil there - and since String() has a value receiver, calling it on the nil pointer blows up. Capability maps that only use enum names are fine, which is probably why this went unnoticed.

Nothing else in the tree reads the enum field directly: compare.go and capslock-git-diff both key off the capability name, so I did the same here and hoisted it out of the loop while I was at it (it was being computed twice). No behaviour change for capabilities that do have an enum value.

Added a test in testing/ that writes a map with func os.Getpid TESTCAP, runs capslock over testpkgs/callos with -output=package, and checks TESTCAP is reported for that package. It panics without the fix.

go vet is clean and go test ./analyzer/... ./interesting/... ./testing/... passes here, though I have to run it with -p 1 / GOMAXPROCS=1 on this machine - a couple of the tests in testing/ fail on the unmodified tree too when memory gets tight.

rootkiller6788 added 2 commits September 14, 2026 20:58
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.
… 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.
@google-cla

google-cla Bot commented Nov 18, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@rootkiller6788
rootkiller6788 marked this pull request as ready for review November 25, 2026 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant