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

Make sure flagValue.String() can be called even when flagValue is created by reflect.New #11651

Merged
merged 4 commits into from
Nov 19, 2024

Conversation

ogaca-dd
Copy link
Contributor

Description

Make sure flagValue.String() can be called even when flagValue is created by reflect.New

import (
	"flag"
	"log"

	"go.opentelemetry.io/collector/featuregate"
)

func main() {
	fs := new(flag.FlagSet)

	featuregate.GlobalRegistry().RegisterFlags(fs)
	if err := fs.Parse([]string{"--help"}); err != nil {
		log.Fatalf("failed to parse flags: %v", err)
	}
}

Before this PR

Usage:
  -feature-gates value
        Comma-delimited list of feature gate identifiers. Prefix with '-' to disable the feature. '+' or no prefix will enable the feature.

panic calling String method on zero featuregate.flagValue for flag feature-gates: runtime error: invalid memory address or nil pointer dereference
failed to parse flags: flag: help requested

Note that there is a panic in the message.

After this PR

Usage:
  -feature-gates value
        Comma-delimited list of feature gate identifiers. Prefix with '-' to disable the feature. '+' or no prefix will enable the feature.
failed to parse flags: flag: help requested

@ogaca-dd ogaca-dd requested a review from a team as a code owner November 12, 2024 17:51
@ogaca-dd ogaca-dd requested a review from bogdandrutu November 12, 2024 17:51
Copy link

linux-foundation-easycla bot commented Nov 12, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

Copy link
Member

@songy23 songy23 left a comment

Choose a reason for hiding this comment

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

LGTM, does this need a changelog?

Copy link

codecov bot commented Nov 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.64%. Comparing base (9c4c007) to head (2a97dfc).
Report is 39 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #11651      +/-   ##
==========================================
+ Coverage   91.58%   91.64%   +0.05%     
==========================================
  Files         440      442       +2     
  Lines       23765    23751      -14     
==========================================
  Hits        21766    21766              
+ Misses       1627     1616      -11     
+ Partials      372      369       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@songy23 songy23 added the ready-to-merge Code review completed; ready to merge by maintainers label Nov 18, 2024
@bogdandrutu bogdandrutu merged commit 16ab931 into open-telemetry:main Nov 19, 2024
40 checks passed
@github-actions github-actions bot added this to the next release milestone Nov 19, 2024
djaglowski pushed a commit to djaglowski/opentelemetry-collector that referenced this pull request Nov 21, 2024
…ated by reflect.New (open-telemetry#11651)

<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

Make sure `flagValue.String()` can be called even when flagValue is
created by `reflect.New`

```go
import (
	"flag"
	"log"

	"go.opentelemetry.io/collector/featuregate"
)

func main() {
	fs := new(flag.FlagSet)

	featuregate.GlobalRegistry().RegisterFlags(fs)
	if err := fs.Parse([]string{"--help"}); err != nil {
		log.Fatalf("failed to parse flags: %v", err)
	}
}
```

Before this PR
```
Usage:
  -feature-gates value
        Comma-delimited list of feature gate identifiers. Prefix with '-' to disable the feature. '+' or no prefix will enable the feature.

panic calling String method on zero featuregate.flagValue for flag feature-gates: runtime error: invalid memory address or nil pointer dereference
failed to parse flags: flag: help requested
```
Note that there is a panic in the message.

After this PR 
```
Usage:
  -feature-gates value
        Comma-delimited list of feature gate identifiers. Prefix with '-' to disable the feature. '+' or no prefix will enable the feature.
failed to parse flags: flag: help requested
```

---------

Co-authored-by: Yang Song <songy23@users.noreply.github.com>
@ogaca-dd ogaca-dd deleted the features-gate-help-panic branch November 21, 2024 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-to-merge Code review completed; ready to merge by maintainers Skip Contrib Tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants