Fixes for various CodeQL findings#676
Closed
uniemimu wants to merge 1 commit into
Closed
Conversation
Plus a regexp cleanup. From copilot autofix. Signed-off-by: Ukri <ukri.niemimuukko@intel.com>
There was a problem hiding this comment.
Pull request overview
This PR addresses several CodeQL findings by tightening integer parsing/conversion semantics and correcting a regexp pattern used when filtering topology hint paths. These changes reduce the risk of unintended truncation/wraparound during numeric parsing and make the regexp match more precise.
Changes:
- Updated sysfs value parsing to parse integers using the target type’s bit size (e.g., int8/int16/uint32) before converting.
- Adjusted global NUMA node ID parsing to use
strconv.Atoiand avoid redundant conversions. - Escaped
.in a regexp sokubernetes.io~...is matched literally.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| pkg/sysfs/utils.go | Uses type-sized ParseInt/ParseUint calls to prevent unsafe narrowing conversions when parsing scalar and list integer values. |
| pkg/resmgr/cache/container.go | Fixes regexp to treat kubernetes.io as a literal string in ignored topology path detection. |
| pkg/cgroups/cgroupstats.go | Simplifies NUMA node directory ID parsing and removes an unnecessary cast when indexing the result map. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+196
to
200
| case *uint: | ||
| v, err := strconv.ParseUint(str, 0, 0) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid entry: '%s': %w", str, err) | ||
| } |
Comment on lines
+217
to
+229
| case *uint32: | ||
| v, err := strconv.ParseUint(str, 0, 32) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid entry: '%s': %w", str, err) | ||
| } | ||
| *value = uint32(v) | ||
|
|
||
| case *uint64: | ||
| v, err := strconv.ParseUint(str, 0, 64) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid entry: '%s': %w", str, err) | ||
| } | ||
| *value = v |
Comment on lines
+471
to
474
| node, err := strconv.Atoi(id) | ||
| if err != nil { | ||
| return map[int]GlobalNumaStats{}, fmt.Errorf("error parsing directory name") | ||
| } |
Contributor
Author
|
@kad Please create a PR of your version of the integer conversion fixes. This can be closed then. |
Collaborator
Collaborator
|
Closing in favor of #682. |
kad
added a commit
to kad/nri-plugins
that referenced
this pull request
Jun 15, 2026
CodeQL flagged integer conversion bugs here (PR containers#676 was the autofix attempt). The real problem: readSysfsEntry/writeSysfsEntry accepted interface{} and every integer parser called strconv.ParseInt with bitSize=0, meaning 64-bit, then cast the result down -- so parsing "300" into int8 silently gave you 44. Patching the casts doesn't help; the issue is the parse site itself. Replaced the whole interface{}-based layer with typed generic functions: readSysfsRaw, readSysfsString, readSysfsInt[T], readSysfsUint[T], readSysfsIDSet, readSysfsIntList[T], readSysfsEPP on the read side, and writeSysfsRaw, writeSysfsInt[T], writeSysfsUint[T] for writes. parseIntTo[T] and parseUintTo[T] use a type switch to get T's actual bit width and pass it straight to strconv.ParseInt/ParseUint. Out-of-range values now error. The unsafe import is gone. A few other things were broken in the old code: - parseValueList `break`-ed on empty tokens, stopping the whole parse instead of just skipping the empty slot - parseIDSet split on "-" without a limit, so "0-7" got three pieces when the separator also happened to be "-"; fixed with SplitN(..., 2) - formatValueList returned "" instead of the string it built -- removed along with formatValue and other dead helpers All 27 call sites in system.go updated. Added utils_test.go covering all new utility functions. Replaces: containers#676 Signed-off-by: Alexander Kanevskiy <alexander.kanevskiy@intel.com>
kad
added a commit
to kad/nri-plugins
that referenced
this pull request
Jun 15, 2026
CodeQL flagged integer conversion bugs here (PR containers#676 was the autofix attempt). The real problem: readSysfsEntry/writeSysfsEntry accepted interface{} and every integer parser called strconv.ParseInt with bitSize=0, meaning 64-bit, then cast the result down -- so parsing "300" into int8 silently gave you 44. Patching the casts doesn't help; the issue is the parse site itself. Replaced the whole interface{}-based layer with typed generic functions: readSysfsRaw, readSysfsString, readSysfsInt[T], readSysfsUint[T], readSysfsIDSet, readSysfsIntList[T], readSysfsEPP on the read side, and writeSysfsRaw, writeSysfsInt[T], writeSysfsUint[T] for writes. parseIntTo[T] and parseUintTo[T] use a type switch to get T's actual bit width and pass it straight to strconv.ParseInt/ParseUint. Out-of-range values now error. The unsafe import is gone. A few other things were broken in the old code: - parseValueList `break`-ed on empty tokens, stopping the whole parse instead of just skipping the empty slot - parseIDSet split on "-" without a limit, so "0-7" got three pieces when the separator also happened to be "-"; fixed with SplitN(..., 2) - formatValueList returned "" instead of the string it built -- removed along with formatValue and other dead helpers All 27 call sites in system.go updated. Added utils_test.go covering all new utility functions. Replaces: containers#676 Signed-off-by: Alexander Kanevskiy <alexander.kanevskiy@intel.com>
kad
added a commit
to kad/nri-plugins
that referenced
this pull request
Jun 15, 2026
CodeQL flagged integer conversion bugs here (PR containers#676 was the autofix attempt). The real problem: readSysfsEntry/writeSysfsEntry accepted interface{} and every integer parser called strconv.ParseInt with bitSize=0, meaning 64-bit, then cast the result down -- so parsing "300" into int8 silently gave you 44. Patching the casts doesn't help; the issue is the parse site itself. Replaced the whole interface{}-based layer with typed generic functions: readSysfsRaw, readSysfsString, readSysfsInt[T], readSysfsUint[T], readSysfsIDSet, readSysfsIntList[T], readSysfsEPP on the read side, and writeSysfsRaw, writeSysfsInt[T], writeSysfsUint[T] for writes. parseIntTo[T] and parseUintTo[T] use a type switch to get T's actual bit width and pass it straight to strconv.ParseInt/ParseUint. Out-of-range values now error. The unsafe import is gone. A few other things were broken in the old code: - parseValueList `break`-ed on empty tokens, stopping the whole parse instead of just skipping the empty slot - parseIDSet split on "-" without a limit, so "0-7" got three pieces when the separator also happened to be "-"; fixed with SplitN(..., 2) - formatValueList returned "" instead of the string it built -- removed along with formatValue and other dead helpers All 27 call sites in system.go updated. Added utils_test.go covering all new utility functions. Replaces: containers#676 Signed-off-by: Alexander Kanevskiy <alexander.kanevskiy@intel.com>
askervin
pushed a commit
that referenced
this pull request
Jun 17, 2026
CodeQL flagged integer conversion bugs here (PR #676 was the autofix attempt). The real problem: readSysfsEntry/writeSysfsEntry accepted interface{} and every integer parser called strconv.ParseInt with bitSize=0, meaning 64-bit, then cast the result down -- so parsing "300" into int8 silently gave you 44. Patching the casts doesn't help; the issue is the parse site itself. Replaced the whole interface{}-based layer with typed generic functions: readSysfsRaw, readSysfsString, readSysfsInt[T], readSysfsUint[T], readSysfsIDSet, readSysfsIntList[T], readSysfsEPP on the read side, and writeSysfsRaw, writeSysfsInt[T], writeSysfsUint[T] for writes. parseIntTo[T] and parseUintTo[T] use a type switch to get T's actual bit width and pass it straight to strconv.ParseInt/ParseUint. Out-of-range values now error. The unsafe import is gone. A few other things were broken in the old code: - parseValueList `break`-ed on empty tokens, stopping the whole parse instead of just skipping the empty slot - parseIDSet split on "-" without a limit, so "0-7" got three pieces when the separator also happened to be "-"; fixed with SplitN(..., 2) - formatValueList returned "" instead of the string it built -- removed along with formatValue and other dead helpers All 27 call sites in system.go updated. Added utils_test.go covering all new utility functions. Replaces: #676 Signed-off-by: Alexander Kanevskiy <alexander.kanevskiy@intel.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
CodeQL findings. Mostly fixes for incorrect conversion between integer types, but includes a regexp cleanup. From copilot autofix.