matcha
matcha copied to clipboard
BUG: Potential integer overflow in POP3 offset/limit handling on 32-bit systems
Describe the bug In backend/pop3/pop3.go lines 107-112, uint32 offset/limit values are cast to int without overflow checking. On 32-bit systems, if offset > 2^31-1, int(offset) becomes negative, causing array index bugs.
To reproduce
- Examine backend/pop3/pop3.go lines 107-112:
// POP3 messages are 1-indexed. We want newest first (highest ID first).
start := len(msgs) - int(offset) // uint32 → int
if start <= 0 {
return []backend.Email{}, nil
}
end := start - int(limit) // uint32 → int
if end < 0 {
end = 0
}
- On 32-bit arch, if offset = 0x80000000 (2147483648):
int(offset)= -2147483648 (negative!)start = len(msgs) - (-2147483648)= overflow
Expected behavior Check for overflow before casting, or use int64 for intermediate calculations, or validate offset/limit are within reasonable bounds.
Additional context
- Good first issue: straightforward bounds validation
- Low severity: rare (most systems are 64-bit, POP3 unlikely to have 2B+ messages)
- But still a correctness bug - could cause panic or wrong messages returned
- Function signature:
FetchEmails(_ context.Context, _ string, limit, offset uint32)
Suggested fix:
// Validate offset and limit are within reasonable bounds
if offset > uint32(len(msgs)) {
return []backend.Email{}, nil
}
if limit > uint32(len(msgs)) {
limit = uint32(len(msgs))
}
start := len(msgs) - int(offset)
if start <= 0 {
return []backend.Email{}, nil
}
end := start - int(limit)
if end < 0 {
end = 0
}
OS 32-bit systems (ARM, x86)
This has had no activity for 30 days. It will be closed in 14 days unless updated. Comment or remove the stale label to keep it open.
Closing due to inactivity. Reopen if still relevant.