From d13f7aa0ae1ad6ec86d66953cc84851977580c8c Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Wed, 9 Aug 2023 17:46:00 -0700 Subject: [PATCH] archive/tar: correct value passed to Uname method For #50102 Change-Id: I28b5579611b07952b6379bc4603daf29a86a3be0 Reviewed-on: https://go-review.googlesource.com/c/go/+/518056 Auto-Submit: Ian Lance Taylor Run-TryBot: Joseph Tsai TryBot-Result: Gopher Robot Reviewed-by: Tianon Gravi (Andrew) Reviewed-by: Michael Knyszek Run-TryBot: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Reviewed-by: Ian Lance Taylor Reviewed-by: qiulaidongfeng <2645477756@qq.com> Reviewed-by: Joseph Tsai --- src/archive/tar/common.go | 2 +- src/archive/tar/tar_test.go | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/archive/tar/common.go b/src/archive/tar/common.go index f141548db8..8af17efa51 100644 --- a/src/archive/tar/common.go +++ b/src/archive/tar/common.go @@ -726,7 +726,7 @@ func FileInfoHeader(fi fs.FileInfo, link string) (*Header, error) { if err != nil { return nil, err } - h.Uname, err = iface.Uname(h.Gid) + h.Uname, err = iface.Uname(h.Uid) if err != nil { return nil, err } diff --git a/src/archive/tar/tar_test.go b/src/archive/tar/tar_test.go index 6bb27ec3e8..49d31bb757 100644 --- a/src/archive/tar/tar_test.go +++ b/src/archive/tar/tar_test.go @@ -849,6 +849,11 @@ func Benchmark(b *testing.B) { } +const ( + testUid = 10 + testGid = 20 +) + type fileInfoNames struct{} func (f *fileInfoNames) Name() string { @@ -876,14 +881,29 @@ func (f *fileInfoNames) Sys() any { } func (f *fileInfoNames) Uname(uid int) (string, error) { - return "Uname", nil + if uid == testUid { + return "Uname", nil + } + return "", nil } func (f *fileInfoNames) Gname(gid int) (string, error) { - return "Gname", nil + if gid == testGid { + return "Gname", nil + } + return "", nil } func TestFileInfoHeaderUseFileInfoNames(t *testing.T) { + origLoadUidAndGid := loadUidAndGid + defer func() { + loadUidAndGid = origLoadUidAndGid + }() + loadUidAndGid = func(fi fs.FileInfo, uid, gid *int) { + *uid = testUid + *gid = testGid + } + info := &fileInfoNames{} header, err := FileInfoHeader(info, "") if err != nil {