-
Notifications
You must be signed in to change notification settings - Fork 99
Make /proc/devices parser more robust (fix #297) #300
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
Conversation
6e663f3 to
129764f
Compare
|
We get this code quality warning:
Fair enough. We have type (see https://github.com/NVIDIA/k8s-dra-driver-gpu/security/code-scanning/2) |
|
I have btw visualized and tested the regular expression here: https://regex101.com/r/WaHkdK/1 I've also given it a bit of a challenging probe, and with the green highlight we see that it extracts the data in question despite a number of distractions: |
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.
Pull Request Overview
This PR improves the robustness of parsing the /proc/devices file by replacing line‐by‐line scanning with a regular expression-based search for the device major number in the "Character devices" section.
- Switch from scanning file lines to reading the full file into memory.
- Use a regular expression to extract the desired major number.
|
Tested on Luna, which has limited signal: IMEX daemon not meant to start up, and code path is not visted: |
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.
Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
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.
Pull Request Overview
This PR strengthens the parsing of the /proc/devices file by switching from a line-by-line scan to a regex-based search, thereby improving the detection of the device major number in the "Character devices" section.
- Replaces iterative scanning with a regex that targets the specific block between "Character devices:" and "Block devices:"
- Refines error messages and adds an upper bound validation on the device major number
| // Search for an "<integer> <name>" occurrence in the "Character devices" | ||
| // section of the /proc/devices file. Return the integer. |
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.
Funcs¶
A function’s doc comment should explain what the function returns or, for functions called for side effects, what it does. Named parameters and results can be referred to directly in the comment, without any special syntax like backquotes. (A consequence of this convention is that names like a, which might be mistaken for ordinary words, are typically avoided.) For example:
package strconv
// Quote returns a double-quoted Go string literal representing s.
// The returned string uses Go escape sequences (\t, \n, \xFF, \u0100)
// for control characters and non-printable characters as defined by IsPrint.
func Quote(s string) string {
...
}
And:
package os
// Exit causes the current program to exit with the given status code.
// Conventionally, code zero indicates success, non-zero an error.
// The program terminates immediately; deferred functions are not run.
//
// For portability, the status code should be in the range [0, 125].
func Exit(code int) {
...
}
see more at https://tip.golang.org/doc/comment
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.
OK, what is the specific suggestion here?
A function’s doc comment should explain what the function returns or, for functions called for side effects, what it does.
The comment does that, I think.
What is the change you would like to see?
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.
the comment must start with the func name
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.
the comment must start with the func name
Okay. I changed this, squashed, force-pushed.
Note that in terms of communication and 'truth-seeking', I have remarks:
That aspect was not mentioned in the text you quoted above :) It's also not explicitly said in the doc you linked. I get it, the examples support that idea.
https://stackoverflow.com/questions/35591301/function-name-in-comment is about that topic. There, they quote
The first sentence should be a one-sentence summary that starts with the name being declared.
which at some point was part of https://go.dev/doc/effective_go#commentary.
It is not there anymore, today. Probably the removal was a deliberate choice.
Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]>
Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]>
9479069 to
d0617be
Compare
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.
Pull Request Overview
This PR improves the robustness of parsing /proc/devices when searching for a device’s major number, addressing the issue outlined in #297.
- Replaces a line-by-line scanner with reading the entire file and applying a regex.
- Uses a regular expression to more precisely extract the device major number between "Character devices:" and "Block devices:".
Comments suppressed due to low confidence (1)
cmd/compute-domain-kubelet-plugin/nvlib.go:240
- The regex pattern assumes the presence of a 'Block devices:' section to delimit the match. This could lead to parsing failures on systems where the /proc/devices file does not include that section; consider making this part optional or adding a fallback error message that guides further debugging.
"([0-9]+) " + regexp.QuoteMeta(name) + ".*Block devices:"
|
Thanks, @ArangoGutierrez. Let's get this in and get going with testing. |

This is a patch for #297 which tries to take precise control of looking for the match in the expected section of the /proc/devices file contents.
I tested this function in isolation on a 'challenging' node, and it returns data as expected: