Skip to content

Commit 6be5de4

Browse files
huww98gopherbot
authored andcommitted
internal/runtime/cgroup: simplify escapePath in test
Don't work on rune, kernel does not use utf-8 here. Can be verified like this: # mkdir "$(echo -e "\xff\x20")" # mount -t tmpfs tmpfs "$(echo -e "\xff\x20")" # tail -n 1 /proc/self/mountinfo | xxd 00000000: 3133 3334 2031 3030 2030 3a31 3632 202f 1334 100 0:162 / 00000010: 202f 726f 6f74 2fff 5c30 3430 2072 772c /root/.\040 rw, 00000020: 7265 6c61 7469 6d65 2073 6861 7265 643a relatime shared: 00000030: 3433 3520 2d20 746d 7066 7320 746d 7066 435 - tmpfs tmpf 00000040: 7320 7277 0a s rw. Change-Id: I7468b56eb26f14bc809f8f7580535e6562795c62 Reviewed-on: https://go-review.googlesource.com/c/go/+/723300 Reviewed-by: Michael Pratt <mpratt@google.com> Auto-Submit: Michael Pratt <mpratt@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
1 parent 481c6df commit 6be5de4

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

src/internal/runtime/cgroup/cgroup_test.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"internal/runtime/cgroup"
1010
"io"
11-
"strconv"
1211
"strings"
1312
"testing"
1413
)
@@ -380,21 +379,11 @@ func TestParseCPUMount(t *testing.T) {
380379
// That is, '\', ' ', '\t', and '\n' are converted to octal escape sequences,
381380
// like '\040' for space.
382381
func escapePath(s string) string {
383-
out := make([]rune, 0, len(s))
384-
for _, c := range s {
382+
out := make([]byte, 0, len(s))
383+
for _, c := range []byte(s) {
385384
switch c {
386385
case '\\', ' ', '\t', '\n':
387-
out = append(out, '\\')
388-
cs := strconv.FormatInt(int64(c), 8)
389-
if len(cs) <= 2 {
390-
out = append(out, '0')
391-
}
392-
if len(cs) <= 1 {
393-
out = append(out, '0')
394-
}
395-
for _, csc := range cs {
396-
out = append(out, csc)
397-
}
386+
out = fmt.Appendf(out, "\\%03o", c)
398387
default:
399388
out = append(out, c)
400389
}
@@ -444,6 +433,11 @@ b/c`,
444433
unescaped: `/a/\`,
445434
escaped: `/a/\134`,
446435
},
436+
{
437+
name: "non-utf8",
438+
unescaped: "/a/b\xff\x20/c",
439+
escaped: "/a/b\xff\\040/c",
440+
},
447441
}
448442

449443
t.Run("escapePath", func(t *testing.T) {

0 commit comments

Comments
 (0)