-
Notifications
You must be signed in to change notification settings - Fork 667
fix: do not exit code 1 with invalid glibc #6342
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os/exec" | ||
| "regexp" | ||
| "runtime" | ||
| "strings" | ||
| "sync" | ||
| ) | ||
|
|
||
| var ( | ||
| cachedVersion string | ||
| versionDetectOnce sync.Once | ||
| versionRegex = regexp.MustCompile(`(\d+\.\d+)`) | ||
| ) | ||
|
|
||
| type GlibcParsers func(string) (string, error) | ||
|
|
||
| // DefaultGlibcVersion attempts to detect the glibc version on Linux systems | ||
| // The detection is performed only once and cached for subsequent calls | ||
| func DefaultGlibcVersion() string { | ||
| versionDetectOnce.Do(func() { | ||
| cachedVersion = GetGlibcDetails(ParserGlibcVersion()) | ||
| }) | ||
| return cachedVersion | ||
| } | ||
|
|
||
| // GetGlibcDetails attempts to detect the glibc version on Linux systems | ||
| func GetGlibcDetails(parser GlibcParsers) string { | ||
| if runtime.GOOS != "linux" { | ||
| return "" | ||
| } | ||
|
|
||
| // Method 1: Try ldd --version | ||
| if out, err := exec.Command("ldd", "--version").Output(); err == nil { | ||
| lines := strings.Split(string(out), "\n") | ||
| if len(lines) > 0 { | ||
| if result, err := parser(lines[0]); err == nil { | ||
| return result | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Method 2: Try getconf GNU_LIBC_VERSION | ||
| if out, err := exec.Command("getconf", "GNU_LIBC_VERSION").Output(); err == nil { | ||
| if result, err := parser(string(out)); err == nil { | ||
| return result | ||
| } | ||
| } | ||
|
|
||
| return "" | ||
| } | ||
|
|
||
| func ParserGlibcFull() GlibcParsers { | ||
| return func(details string) (string, error) { | ||
| return strings.TrimSpace(details), nil | ||
| } | ||
| } | ||
|
|
||
| func ParserGlibcVersion() GlibcParsers { | ||
| return func(details string) (string, error) { | ||
| if matches := versionRegex.FindStringSubmatch(details); len(matches) > 1 { | ||
| return matches[1], nil | ||
| } | ||
|
|
||
| return "", fmt.Errorf("failed to parse glibc version") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "runtime" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func Test_ParserGlibcVersion(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| input string | ||
| expectedVer string | ||
| expectError bool | ||
| }{ | ||
| { | ||
| name: "Standard ldd output format", | ||
| input: "ldd (GNU libc) 2.31", | ||
| expectedVer: "2.31", | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "Standard ldd output format with extra info", | ||
| input: "ldd (Ubuntu GLIBC 2.35-0ubuntu3.8) 2.35", | ||
| expectedVer: "2.35", | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "getconf format", | ||
| input: "glibc 2.28", | ||
| expectedVer: "2.28", | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "Version at start of line", | ||
| input: "2.27 (GNU libc)", | ||
| expectedVer: "2.27", | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "Multi-digit minor version", | ||
| input: "ldd (GNU libc) 2.117", | ||
| expectedVer: "2.117", | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "Invalid format - no version", | ||
| input: "musl libc (x86_64)", | ||
| expectedVer: "", | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "Invalid format - empty string", | ||
| input: "", | ||
| expectedVer: "", | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "Invalid format - only text", | ||
| input: "some random text", | ||
| expectedVer: "", | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "Invalid format - single number", | ||
| input: "version 2", | ||
| expectedVer: "", | ||
| expectError: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| parser := ParserGlibcVersion() | ||
| result, err := parser(tc.input) | ||
|
|
||
| if tc.expectError { | ||
| assert.Error(t, err) | ||
| assert.Equal(t, "", result) | ||
| } else { | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, tc.expectedVer, result) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_ParserGlibcFull(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| input string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "Standard ldd output", | ||
| input: "ldd (GNU libc) 2.31", | ||
| expected: "ldd (GNU libc) 2.31", | ||
| }, | ||
| { | ||
| name: "Output with leading whitespace", | ||
| input: " ldd (GNU libc) 2.31", | ||
| expected: "ldd (GNU libc) 2.31", | ||
| }, | ||
| { | ||
| name: "Output with trailing whitespace", | ||
| input: "ldd (GNU libc) 2.31 \n", | ||
| expected: "ldd (GNU libc) 2.31", | ||
| }, | ||
| { | ||
| name: "Output with both leading and trailing whitespace", | ||
| input: " \t ldd (GNU libc) 2.31 \n\t ", | ||
| expected: "ldd (GNU libc) 2.31", | ||
| }, | ||
| { | ||
| name: "Empty string", | ||
| input: "", | ||
| expected: "", | ||
| }, | ||
| { | ||
| name: "Only whitespace", | ||
| input: " \n\t ", | ||
| expected: "", | ||
| }, | ||
| { | ||
| name: "Multiline output - only first matters", | ||
| input: "ldd (GNU libc) 2.31\nCopyright info\nMore text", | ||
| expected: "ldd (GNU libc) 2.31\nCopyright info\nMore text", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| parser := ParserGlibcFull() | ||
| result, err := parser(tc.input) | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.Equal(t, tc.expected, result) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_GetGlibcDetails_NonLinux(t *testing.T) { | ||
| if runtime.GOOS == "linux" { | ||
| t.Skip("Test only applicable on non-Linux") | ||
| } | ||
|
|
||
| parser := ParserGlibcVersion() | ||
| result := GetGlibcDetails(parser) | ||
|
|
||
| assert.Equal(t, "", result, "Should return empty string on non-Linux") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Let's be a bit paranoid and check if both versions are actually valid using isValid()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just because the doc of Compare() says the following