Skip to content

Commit dde4b1a

Browse files
committed
user: fix the parameter error
The parameters passed to `GetExecUser` is not correct. Consider the following code: ``` package main import ( "fmt" "io" "os" ) func main() { passwd, err := os.Open("/etc/passwd1") if err != nil { passwd = nil } else { defer passwd.Close() } err = GetUserPasswd(passwd) if err != nil { fmt.Printf("%#v\n", err) } } func GetUserPasswd(r io.Reader) error { if r == nil { return fmt.Errorf("nil source for passwd-formatted data") } else { fmt.Printf("r = %#v\n", r) } return nil } ``` If the file `/etc/passwd1` is not exist, we expect to return `nil source for passwd-formatted data` error, and in fact, the func `GetUserPasswd` return nil. The same logic exists in runc code. this patch fix it. Signed-off-by: Wang Long <long.wanglong@huawei.com>
1 parent a9610f2 commit dde4b1a

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

libcontainer/user/user.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,16 @@ type ExecUser struct {
199199
// files cannot be opened for any reason, the error is ignored and a nil
200200
// io.Reader is passed instead.
201201
func GetExecUserPath(userSpec string, defaults *ExecUser, passwdPath, groupPath string) (*ExecUser, error) {
202-
passwd, err := os.Open(passwdPath)
203-
if err != nil {
204-
passwd = nil
205-
} else {
206-
defer passwd.Close()
202+
var passwd, group io.Reader
203+
204+
if passwdFile, err := os.Open(passwdPath); err == nil {
205+
passwd = passwdFile
206+
defer passwdFile.Close()
207207
}
208208

209-
group, err := os.Open(groupPath)
210-
if err != nil {
211-
group = nil
212-
} else {
213-
defer group.Close()
209+
if groupFile, err := os.Open(groupPath); err == nil {
210+
group = groupFile
211+
defer groupFile.Close()
214212
}
215213

216214
return GetExecUser(userSpec, defaults, passwd, group)
@@ -433,9 +431,11 @@ func GetAdditionalGroups(additionalGroups []string, group io.Reader) ([]int, err
433431
// that opens the groupPath given and gives it as an argument to
434432
// GetAdditionalGroups.
435433
func GetAdditionalGroupsPath(additionalGroups []string, groupPath string) ([]int, error) {
436-
group, err := os.Open(groupPath)
437-
if err == nil {
438-
defer group.Close()
434+
var group io.Reader
435+
436+
if groupFile, err := os.Open(groupPath); err == nil {
437+
group = groupFile
438+
defer groupFile.Close()
439439
}
440440
return GetAdditionalGroups(additionalGroups, group)
441441
}

0 commit comments

Comments
 (0)