memguard icon indicating copy to clipboard operation
memguard copied to clipboard

LockedBuffer.Copy() crashes with access violation on Windows when buffer is valid and alive

Open xMinhx opened this issue 6 months ago • 4 comments

Describe the bug Calling (*memguard.LockedBuffer).Copy() results in a fatal access violation on Windows, even though the buffer is non-nil, alive (IsAlive() returns true), and accessed under a sync.RWMutex that guards against concurrent Destroy() or Swap().

Replacing .Copy() with copy(out, buf.Bytes()) avoids the issue.

To Reproduce Steps to reproduce the behavior:

Create a LockedBuffer using memguard.NewBufferFromBytes Call .Copy() into equally sized destination buffer

-> panic or access violation

buf := memguard.NewBufferFromBytes([]byte("test"))
defer buf.Destroy()

out := make([]byte, buf.Size())
buf.Copy(out)

Expected behaviour Copy() should copy memory from a valid, non-destroyed buffer. It should not crash when:

  • The buffer is alive
  • The buffer size is non-zero

System: OS and Kernel Versions: Microsoft Windows [Version 10.0.26100.3775] Memguard Version: v0.22.5 Go Version: go1.24.1 (Windows amd64)

Additional context

  • Tried with Mutex
  • No concurrent calls to .Destroy() or .Swap()
  • .IsAlive() returns true before .Copy()
  • .Bytes() works fine under same locking conditions
  • Switching to append([]byte(nil), buf.Bytes()...) avoids crash completely.

xMinhx avatar Apr 16 '25 21:04 xMinhx