Skip to content

Replace Panic with Error Return for Memory Management Failures #175

@APshenkin

Description

@APshenkin

Is your feature request related to a problem? Please describe.
Currently, memguard panics when encountering memory management failures, which creates several issues:

  1. Poor Error Handling: Panics force users to implement recovery mechanisms throughout their codebase
  2. Limited Control: Applications cannot make informed decisions about how to handle memory constraints
  3. Mobile Compatibility: Android's 64KB mlock limit makes panics common in production environments

Describe the solution you'd like
Proposed API

// New error-returning variants
func NewBuffer(size int) (*Buffer, error)
func NewEnclave(data []byte) (*Enclave, error)
// ... other functions

// Optional: Keep panic variants for backward compatibility
func MustNewBuffer(size int) *Buffer
func MustNewEnclave(data []byte) *Enclave

Error Types
Introduce specific error types for different failure scenarios:

// Specific error types
var (
    ErrInsufficientMemory = errors.New("memguard: insufficient lockable memory available")
    ErrMemoryLocked       = errors.New("memguard: memory locking failed")
    ErrMemoryProtection   = errors.New("memguard: memory protection failed")
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions