From 457977514d75a6c72086c2f4d89fb601280b8b90 Mon Sep 17 00:00:00 2001 From: Jes Cok Date: Mon, 12 Jun 2023 20:05:21 +0800 Subject: [PATCH] archive/tar: narrow trim range for formatter'formatString Trim s[:len(b)-1] rather than s[:len(b)], since s[len(b)-1] is '/'. --- src/archive/tar/strconv.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/archive/tar/strconv.go b/src/archive/tar/strconv.go index 601b9bc17d..ac8105efad 100644 --- a/src/archive/tar/strconv.go +++ b/src/archive/tar/strconv.go @@ -73,13 +73,8 @@ func (f *formatter) formatString(b []byte, s string) { // in the V7 path field as a directory even though the full path // recorded elsewhere (e.g., via PAX record) contains no trailing slash. if len(s) > len(b) && b[len(b)-1] == '/' { - i := len(b) - 2 // s[len(b)-1] == '/' - for ; i >= 0; i-- { - if s[i] != '/' { - break - } - } - b[i+1] = 0 // Replace trailing slash with NUL terminator + n := len(strings.TrimRight(s[:len(b)-1], "/")) + b[n] = 0 // Replace trailing slash with NUL terminator } }