Skip to content

Commit 9479069

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

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
@@ -252,15 +252,20 @@ func (l deviceLib) getDeviceMajor(name string) (int, error) {
252252
return -1, fmt.Errorf("error parsing '%s': unexpected regex match: %v", procDevicesPath, matches)
253253
}
254254

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

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

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

0 commit comments

Comments
 (0)