diff --git a/conversions.go b/conversions.go index 876e134..597ab8d 100644 --- a/conversions.go +++ b/conversions.go @@ -448,7 +448,7 @@ func (c *Connection) kernelResponse( // the header, because on OS X the kernel otherwise returns EINVAL when we // attempt to write an error response with a length that extends beyond the // header. - m.Shrink(uintptr(m.Len() - int(buffer.OutMessageInitialSize))) + m.ShrinkTo(buffer.OutMessageInitialSize) } // Otherwise, fill in the rest of the response. @@ -522,7 +522,7 @@ func (c *Connection) kernelResponseForOp( // convertInMessage already set up the destination buffer to be at the end // of the out message. We need only shrink to the right size based on how // much the user read. - m.Shrink(uintptr(m.Len() - (int(buffer.OutMessageInitialSize) + o.BytesRead))) + m.ShrinkTo(buffer.OutMessageInitialSize + uintptr(o.BytesRead)) case *fuseops.ReleaseDirHandleOp: // Empty response @@ -539,7 +539,7 @@ func (c *Connection) kernelResponseForOp( // convertInMessage already set up the destination buffer to be at the end // of the out message. We need only shrink to the right size based on how // much the user read. - m.Shrink(uintptr(m.Len() - (int(buffer.OutMessageInitialSize) + o.BytesRead))) + m.ShrinkTo(buffer.OutMessageInitialSize + uintptr(o.BytesRead)) case *fuseops.WriteFileOp: out := (*fusekernel.WriteOut)(m.Grow(unsafe.Sizeof(fusekernel.WriteOut{}))) diff --git a/internal/buffer/out_message.go b/internal/buffer/out_message.go index aedc1e7..7392cf0 100644 --- a/internal/buffer/out_message.go +++ b/internal/buffer/out_message.go @@ -90,13 +90,14 @@ func (b *OutMessage) GrowNoZero(size uintptr) (p unsafe.Pointer) { return } -// Throw away the last n bytes. Panics if n is out of range. -func (b *OutMessage) Shrink(n uintptr) { - if n > b.offset-OutMessageInitialSize { - panic(fmt.Sprintf("Shrink(%d) out of range for offset %d", n, b.offset)) +// Shrink to the supplied size. Panic if the size is greater than Len() or less +// than OutMessageInitialSize. +func (b *OutMessage) ShrinkTo(n uintptr) { + if n < OutMessageInitialSize || n > b.offset { + panic(fmt.Sprintf("ShrinkTo(%d) out of range for offset %d", n, b.offset)) } - b.offset -= n + b.offset = n } // Equivalent to growing by the length of p, then copying p over the new