Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(AIP-203): avoid panic if name field is missing #1282

Merged
merged 2 commits into from
Nov 1, 2023

Conversation

radhus
Copy link
Contributor

@radhus radhus commented Nov 1, 2023

This proto reproduces the following crash:

syntax = "proto3";

package dummy;

import "google/api/field_behavior.proto";
import "google/api/resource.proto";

message Dummy {
  option (google.api.resource) = {
    type: "dummy/Dummy"
    pattern: "dummies/{dummy}"
  };
  string other = 1 [(google.api.field_behavior) = REQUIRED];
}
goroutine 1 [running]:
runtime/debug.Stack()
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/debug/stack.go:24 +0x64
runtime/debug.PrintStack()
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/debug/stack.go:16 +0x1c
github.com/googleapis/api-linter/lint.(*Linter).runAndRecoverFromPanics.func1()
        /Users/william/dev/oss/api-linter/lint/lint.go:125 +0x50
panic({0x102971740?, 0x102e6e5f0?})
        /opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/panic.go:914 +0x218
github.com/jhump/protoreflect/desc.(*FieldDescriptor).GetFieldOptions(...)
        /Users/william/go/pkg/mod/github.com/jhump/[email protected]/desc/descriptor.go:954
github.com/googleapis/api-linter/rules/internal/utils.GetFieldBehavior(0x14000331180?)
        /Users/william/dev/oss/api-linter/rules/internal/utils/extension.go:30 +0x1c
github.com/googleapis/api-linter/rules/aip0203.glob..func4(0x1400046e700?)
        /Users/william/dev/oss/api-linter/rules/aip0203/resource_name_identifier.go:30 +0x64
github.com/googleapis/api-linter/lint.(*MessageRule).Lint(0x102e779c0, 0x0?)
        /Users/william/dev/oss/api-linter/lint/rule.go:104 +0xc0
github.com/googleapis/api-linter/lint.(*Linter).runAndRecoverFromPanics(0x140000de840?, {0x102a2edb8?, 0x102e779c0?}, 0x14000045c80?)
        /Users/william/dev/oss/api-linter/lint/lint.go:135 +0x74
github.com/googleapis/api-linter/lint.(*Linter).lintFileDescriptor(0x1400021e510, 0x1400046e700)
        /Users/william/dev/oss/api-linter/lint/lint.go:96 +0x174
github.com/googleapis/api-linter/lint.(*Linter).LintProtos(0x140002d4220?, {0x140004968b8, 0x1, 0x140002d8100?})
        /Users/william/dev/oss/api-linter/lint/lint.go:71 +0x98
main.(*cli).lint(0x140000de780, 0x140002bcf60, {0x102ec1820?, 0x1022f2788?, 0x140000686e8?})
        /Users/william/dev/oss/api-linter/cmd/api-linter/cli.go:190 +0x73c
main.runCLI({0x14000032100?, 0x0?, 0x140001e9f28?})
        /Users/william/dev/oss/api-linter/cmd/api-linter/main.go:46 +0x44
main.main()
        /Users/william/dev/oss/api-linter/cmd/api-linter/main.go:39 +0x54
2023/11/01 11:34:08 runtime error: invalid memory address or nil pointer dereference

With the fix, the lint rule will skip checking when there is no name field - this is covered by AIP-123.

@radhus radhus requested a review from a team as a code owner November 1, 2023 11:28
Copy link
Collaborator

@noahdietz noahdietz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @radhus this is a great catch. I think we need to adjust the solution. Can we instead just skip the rule if there the resource name field does not exist? Meaning, put this check into the OnlyIf:

OnlyIf: func(m *desc.MessageDescriptor) bool {
  return utils.IsResource(m) && m.FindFieldByName(utils.GetResourceNameField(utils.GetResource(m))) != nil
}

Could even put that name field existence check into a helper if you'd like.

The core functionality of this rule is not to enforce that the resource name field must exist - that is/should be a different rule. This rule must only lint the one thing it is meant to lint for. This makes the fine grained configuration more accurate.

@radhus radhus force-pushed the fix-aip0203-crash branch from 7895fe6 to 2c1b523 Compare November 1, 2023 15:57
@radhus
Copy link
Contributor Author

radhus commented Nov 1, 2023

Thanks @radhus this is a great catch. I think we need to adjust the solution. Can we instead just skip the rule if there the resource name field does not exist? Meaning, put this check into the OnlyIf:

OnlyIf: func(m *desc.MessageDescriptor) bool {
  return utils.IsResource(m) && m.FindFieldByName(utils.GetResourceNameField(utils.GetResource(m))) != nil
}

Could even put that name field existence check into a helper if you'd like.

The core functionality of this rule is not to enforce that the resource name field must exist - that is/should be a different rule. This rule must only lint the one thing it is meant to lint for. This makes the fine grained configuration more accurate.

@noahdietz you're right, this is even covered by AIP-123 already. Took your suggestion, much cleaner, and I like the pattern of keeping the rule of only linting one thing!

rules/aip0203/resource_name_identifier_test.go Outdated Show resolved Hide resolved
@noahdietz noahdietz added the automerge Summon MOG for automerging label Nov 1, 2023
@noahdietz
Copy link
Collaborator

@radhus thank you very much!

@gcf-merge-on-green gcf-merge-on-green bot merged commit 2050717 into googleapis:main Nov 1, 2023
6 checks passed
@gcf-merge-on-green gcf-merge-on-green bot removed the automerge Summon MOG for automerging label Nov 1, 2023
@noahdietz
Copy link
Collaborator

This will be in v1.59.1 #1283

@radhus radhus deleted the fix-aip0203-crash branch November 1, 2023 16:16
gcf-merge-on-green bot pushed a commit that referenced this pull request Nov 1, 2023
radhus added a commit to einride/sage that referenced this pull request Nov 1, 2023
radhus added a commit to einride/sage that referenced this pull request Nov 1, 2023
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.

2 participants