Skip to content

Commit d0617be

Browse files
committed
Perform upper bound check in getDeviceMajor()
Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]>
1 parent 8764b6d commit d0617be

File tree

1 file changed

+10
-5
lines changed
  • cmd/compute-domain-kubelet-plugin

1 file changed

+10
-5
lines changed

cmd/compute-domain-kubelet-plugin/nvlib.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,20 @@ func (l deviceLib) getDeviceMajor(name string) (int, error) {
253253
return -1, fmt.Errorf("error parsing '%s': unexpected regex match: %v", procDevicesPath, matches)
254254
}
255255

256-
// Convert capture group content to integer. Error should be unreachable
257-
// (because capture group has matched [0-9]+ which should always convert to
258-
// integer).
259-
major, err := strconv.Atoi(matches[1])
256+
// Convert capture group content to integer. Perform upper bound check:
257+
// value must fit into 32-bit integer (it's then also guaranteed to fit into
258+
// a 32-bit unsigned integer, which is the type that must be passed to
259+
// unix.Mkdev()).
260+
major, err := strconv.ParseInt(matches[1], 10, 32)
260261
if err != nil {
261262
return -1, fmt.Errorf("int conversion failed for '%v': %w", matches[1], err)
262263
}
263264

264-
return major, nil
265+
// ParseInt() always returns an integer of explicit type `int64`. We have
266+
// performed an upper bound check so it's safe to convert this to `int`
267+
// (which is documented as "int is a signed integer type that is at least 32
268+
// bits in size", so in theory it could be smaller than int64).
269+
return int(major), nil
265270
}
266271

267272
func (l deviceLib) parseNVCapDeviceInfo(nvcapsFilePath string) (*nvcapDeviceInfo, error) {

0 commit comments

Comments
 (0)