internal/restic: Fix UID/GID parsing

The helper function uidGidInt used strconv.ParseInt instead of
ParseUint, so it silently ignored some invalid user/group IDs.

Also, improve the error message. "Invalid UID" is more informative than
having "ParseInt" twice (*strconv.NumError displays the function name).

Finally, the user.User struct can be passed by pointer to get reduce
code size.
This commit is contained in:
greatroar 2022-10-14 17:40:49 +02:00
parent e0b743c64d
commit 0e155fd9a6
4 changed files with 10 additions and 14 deletions

View file

@ -126,7 +126,7 @@ func (l *Lock) fillUserInfo() error {
}
l.Username = usr.Username
l.UID, l.GID, err = uidGidInt(*usr)
l.UID, l.GID, err = uidGidInt(usr)
return err
}

View file

@ -9,25 +9,21 @@ import (
"strconv"
"syscall"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/errors"
)
// uidGidInt returns uid, gid of the user as a number.
func uidGidInt(u user.User) (uid, gid uint32, err error) {
var ui, gi int64
ui, err = strconv.ParseInt(u.Uid, 10, 32)
func uidGidInt(u *user.User) (uid, gid uint32, err error) {
ui, err := strconv.ParseUint(u.Uid, 10, 32)
if err != nil {
return uid, gid, errors.Wrap(err, "ParseInt")
return 0, 0, errors.Errorf("invalid UID %q", u.Uid)
}
gi, err = strconv.ParseInt(u.Gid, 10, 32)
gi, err := strconv.ParseUint(u.Gid, 10, 32)
if err != nil {
return uid, gid, errors.Wrap(err, "ParseInt")
return 0, 0, errors.Errorf("invalid GID %q", u.Gid)
}
uid = uint32(ui)
gid = uint32(gi)
return
return uint32(ui), uint32(gi), nil
}
// checkProcess will check if the process retaining the lock

View file

@ -8,7 +8,7 @@ import (
)
// uidGidInt always returns 0 on Windows, since uid isn't numbers
func uidGidInt(u user.User) (uid, gid uint32, err error) {
func uidGidInt(u *user.User) (uid, gid uint32, err error) {
return 0, 0, nil
}

View file

@ -154,7 +154,7 @@ func (sn *Snapshot) fillUserInfo() error {
sn.Username = usr.Username
// set userid and groupid
sn.UID, sn.GID, err = uidGidInt(*usr)
sn.UID, sn.GID, err = uidGidInt(usr)
return err
}