Skip to content

πŸ› [Bug]: MemoryLock panics on zero-value usage β€” nil map writeΒ #4357

@pageton

Description

@pageton

Bug Description

MemoryLock has a nil keys map. If a user writes var lock idempotency.MemoryLock without calling NewMemoryLock(), the first call to Lock() panics with "assignment to entry in nil map".

How to Reproduce

Steps to reproduce the behavior:

  1. Use var lock idempotency.MemoryLock (zero value, no constructor)
  2. Call lock.Lock("key", func() error { ... })
  3. l.keys[key] = lock panics β€” nil map write

Affected Code

middleware/idempotency/locker.go:19-22:

type MemoryLock struct {
    keys map[string]*countedLock  // nil at zero value
    mu   sync.Mutex
}

Line 30:

l.keys[key] = lock  // PANIC if l.keys is nil

Expected Behavior

Either lazy-initialize the map in Lock():

func (l *MemoryLock) Lock(key string, fn func() error) error {
    l.mu.Lock()
    if l.keys == nil {
        l.keys = make(map[string]*countedLock)
    }
    // ...
}

Or make the struct unexported and only expose the constructor.

Fiber Version

v3 (latest main branch)

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

Status
Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions