Skip to content

fix: return defaultValue on conversion failure in ConvertStringToTypeOrDefault#492

Open
immanuwell wants to merge 1 commit into
longhorn:masterfrom
immanuwell:fix/convert-string-to-type-or-default
Open

fix: return defaultValue on conversion failure in ConvertStringToTypeOrDefault#492
immanuwell wants to merge 1 commit into
longhorn:masterfrom
immanuwell:fix/convert-string-to-type-or-default

Conversation

@immanuwell
Copy link
Copy Markdown

@immanuwell immanuwell commented May 31, 2026

Which issue(s) this PR fixes:

Issue longhorn/longhorn#10000

What this PR does / why we need it:

ConvertStringToTypeOrDefault doc says "if conversion fails, return the default value" but the code returns the zero value (0 for int, false for bool) instead. So LONGHORN_SPDK_HUGE_PAGE_SIZE=garbage silently sets huge page size to 0 rather than the default 2048.

Also: typo lable-selectors -> label-selectors in a log field, and a misleading "does not exist" error message in a non-ENOENT stat error branch.

Special notes for your reviewer:

Reproduce:

ConvertStringToTypeOrDefault("garbage", 2048) // was: 0, now: 2048
ConvertStringToTypeOrDefault("garbage", true) // was: false, now: true

Additional documentation or context

…OrDefault

Signed-off-by: Immanuel Tikhonov <pchpr.00@list.ru>
Signed-off-by: immanuwell <pchpr.00@list.ru>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aligns ConvertStringToTypeOrDefault behavior with its documentation by returning the provided default value when parsing fails, preventing invalid env var values from silently becoming type zero-values (e.g., huge page size becoming 0). It also includes a small logging field typo fix and improves an error message for non-ENOENT stat failures.

Changes:

  • Return defaultValue on Atoi/ParseBool conversion errors in ConvertStringToTypeOrDefault.
  • Add unit tests covering empty/valid/invalid conversions for int and bool.
  • Fix label-selectors log field key typo and improve a misleading stat error message.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
pkg/utils/cmd.go Fix conversion-failure behavior to return defaultValue (per function doc).
pkg/utils/common_test.go Add tests validating correct defaults and parsing outcomes for int/bool.
pkg/utils/kubernetes/runtime.go Correct a log field key typo (label-selectors).
pkg/utils/longhorn/longhorn.go Improve error message when os.Stat fails for reasons other than non-existence.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/utils/common_test.go
Comment on lines +21 to +27
var got any
switch d := testCase.defaultValue.(type) {
case int:
got = ConvertStringToTypeOrDefault(testCase.input, d)
case bool:
got = ConvertStringToTypeOrDefault(testCase.input, d)
}
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