From 8d472ebf2b654e0b97b21b40a4333184fc2c0736 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Fri, 29 Aug 2025 02:15:48 +0900 Subject: [PATCH] container/inittmpfs: unwrap out of bounds error This eliminates generic WrapErr from tmpfs. Signed-off-by: Ophestra --- container/errors.go | 4 ++++ container/errors_test.go | 3 +++ container/inittmpfs.go | 10 ++++++++-- container/inittmpfs_test.go | 11 +++++++++-- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/container/errors.go b/container/errors.go index 1a828e0..8d8ad49 100644 --- a/container/errors.go +++ b/container/errors.go @@ -24,6 +24,10 @@ func messageFromError(err error) (string, bool) { return m, ok } + if m, ok := messagePrefix[TmpfsSizeError]("", err); ok { + return m, ok + } + return zeroString, false } diff --git a/container/errors_test.go b/container/errors_test.go index eae5ee7..f99812b 100644 --- a/container/errors_test.go +++ b/container/errors_test.go @@ -39,6 +39,9 @@ func TestMessageFromError(t *testing.T) { {"state", OpStateError("overlay"), "impossible overlay state reached", true}, + {"tmpfs", TmpfsSizeError(-1), + "tmpfs size -1 out of bounds", true}, + {"unsupported", errUnique, zeroString, false}, } for _, tc := range testCases { diff --git a/container/inittmpfs.go b/container/inittmpfs.go index 0d50e46..2835004 100644 --- a/container/inittmpfs.go +++ b/container/inittmpfs.go @@ -3,14 +3,20 @@ package container import ( "encoding/gob" "fmt" - "io/fs" "math" "os" + "strconv" . "syscall" ) func init() { gob.Register(new(MountTmpfsOp)) } +type TmpfsSizeError int + +func (e TmpfsSizeError) Error() string { + return "tmpfs size " + strconv.Itoa(int(e)) + " out of bounds" +} + // Tmpfs appends an [Op] that mounts tmpfs on container path [MountTmpfsOp.Path]. func (f *Ops) Tmpfs(target *Absolute, size int, perm os.FileMode) *Ops { *f = append(*f, &MountTmpfsOp{SourceTmpfsEphemeral, target, MS_NOSUID | MS_NODEV, size, perm}) @@ -36,7 +42,7 @@ func (t *MountTmpfsOp) Valid() bool { return t != func (t *MountTmpfsOp) early(*setupState, syscallDispatcher) error { return nil } func (t *MountTmpfsOp) apply(_ *setupState, k syscallDispatcher) error { if t.Size < 0 || t.Size > math.MaxUint>>1 { - return msg.WrapErr(fs.ErrInvalid, fmt.Sprintf("size %d out of bounds", t.Size)) + return TmpfsSizeError(t.Size) } return k.mountTmpfs(t.FSName, toSysroot(t.Path.String()), t.Flags, t.Size, t.Perm) } diff --git a/container/inittmpfs_test.go b/container/inittmpfs_test.go index 8110f44..97a591f 100644 --- a/container/inittmpfs_test.go +++ b/container/inittmpfs_test.go @@ -1,17 +1,24 @@ package container import ( - "io/fs" "os" "syscall" "testing" ) func TestMountTmpfsOp(t *testing.T) { + t.Run("size error", func(t *testing.T) { + tmpfsSizeError := TmpfsSizeError(-1) + want := "tmpfs size -1 out of bounds" + if got := tmpfsSizeError.Error(); got != want { + t.Errorf("Error: %q, want %q", got, want) + } + }) + checkOpBehaviour(t, []opBehaviourTestCase{ {"size oob", new(Params), &MountTmpfsOp{ Size: -1, - }, nil, nil, nil, msg.WrapErr(fs.ErrInvalid, "size -1 out of bounds")}, + }, nil, nil, nil, TmpfsSizeError(-1)}, {"success", new(Params), &MountTmpfsOp{ FSName: "ephemeral",