From 41aaf745ad8a4d4be7129fd4f8ea2cdc5521c4e4 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Mon, 27 Jan 2020 19:49:20 -0800 Subject: [PATCH] fix: fix a potential out of bounds issue in fuse We likely encountered a file that misreported its size. That or there was no bug here and we hit an issue somewhere else. Regardless, there's no reason not to simplify this code and this should fix the issue. --- fuse/readonly/readonly_unix.go | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/fuse/readonly/readonly_unix.go b/fuse/readonly/readonly_unix.go index 16863f31e45..9ac6a2018d3 100644 --- a/fuse/readonly/readonly_unix.go +++ b/fuse/readonly/readonly_unix.go @@ -257,13 +257,15 @@ func (s *Node) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadR if err != nil { return err } - - buf := resp.Data[:min(req.Size, int(int64(r.Size())-req.Offset))] + // Data has a capacity of Size + buf := resp.Data[:int(req.Size)] n, err := io.ReadFull(r, buf) - if err != nil && err != io.EOF { + resp.Data = buf[:n] + switch err { + case nil, io.EOF, io.ErrUnexpectedEOF: + default: return err } - resp.Data = resp.Data[:n] lm["res_size"] = n return nil // may be non-nil / not succeeded } @@ -287,10 +289,3 @@ type roNode interface { } var _ roNode = (*Node)(nil) - -func min(a, b int) int { - if a < b { - return a - } - return b -}