Skip to content

Commit

Permalink
Merge pull request #558 from cyphar/idtools-u32-errors
Browse files Browse the repository at this point in the history
idtools: return errors for >u32 specification
  • Loading branch information
tych0 authored Nov 28, 2024
2 parents d2ba031 + e597e6a commit 3974679
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
didn't occur before. #437
- Quite a few changes were made to CI to try to avoid issues with fragility.
#452
- umoci will now return an explicit error if you pass invalid uid or gid values
to `--uid-map` and `--gid-map` rather than silently truncating the value.

## [0.4.7] - 2021-04-05 ##

Expand Down
20 changes: 13 additions & 7 deletions pkg/idtools/idtools.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,23 @@ func ToContainer(hostID int, idMap []rspec.LinuxIDMapping) (int, error) {
return -1, errors.Errorf("host id %d cannot be mapped to a container id", hostID)
}

// Helper to return a uint32 from strconv.ParseUint type-safely.
func parseUint32(str string) (uint32, error) {
val, err := strconv.ParseUint(str, 10, 32)
return uint32(val), err
}

// ParseMapping takes a mapping string of the form "container:host[:size]" and
// returns the corresponding rspec.LinuxIDMapping. An error is returned if not
// enough fields are provided or are otherwise invalid. The default size is 1.
func ParseMapping(spec string) (rspec.LinuxIDMapping, error) {
parts := strings.Split(spec, ":")

var err error
var hostID, contID, size int
var hostID, contID, size uint32
switch len(parts) {
case 3:
size, err = strconv.Atoi(parts[2])
size, err = parseUint32(parts[2])
if err != nil {
return rspec.LinuxIDMapping{}, errors.Wrap(err, "invalid size in mapping")
}
Expand All @@ -80,19 +86,19 @@ func ParseMapping(spec string) (rspec.LinuxIDMapping, error) {
return rspec.LinuxIDMapping{}, errors.Errorf("invalid number of fields in mapping '%s': %d", spec, len(parts))
}

contID, err = strconv.Atoi(parts[0])
contID, err = parseUint32(parts[0])
if err != nil {
return rspec.LinuxIDMapping{}, errors.Wrap(err, "invalid containerID in mapping")
}

hostID, err = strconv.Atoi(parts[1])
hostID, err = parseUint32(parts[1])
if err != nil {
return rspec.LinuxIDMapping{}, errors.Wrap(err, "invalid hostID in mapping")
}

return rspec.LinuxIDMapping{
HostID: uint32(hostID),
ContainerID: uint32(contID),
Size: uint32(size),
HostID: hostID,
ContainerID: contID,
Size: size,
}, nil
}

0 comments on commit 3974679

Please sign in to comment.