fix: replace panic with fallback in ID generation (#213)
Replace panic calls in generateID() and generateThreadID() with time-based fallback when crypto/rand.Read fails. This is an extremely rare error case, but panicking is not the right behavior for ID generation functions. 🤝 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ package mail
|
|||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -142,19 +143,23 @@ func NewReplyMessage(from, to, subject, body string, original *Message) *Message
|
|||||||
}
|
}
|
||||||
|
|
||||||
// generateID creates a random message ID.
|
// generateID creates a random message ID.
|
||||||
|
// Falls back to time-based ID if crypto/rand fails (extremely rare).
|
||||||
func generateID() string {
|
func generateID() string {
|
||||||
b := make([]byte, 8)
|
b := make([]byte, 8)
|
||||||
if _, err := rand.Read(b); err != nil {
|
if _, err := rand.Read(b); err != nil {
|
||||||
panic("crypto/rand.Read failed: " + err.Error())
|
// Fallback to time-based ID instead of panicking
|
||||||
|
return fmt.Sprintf("msg-%x", time.Now().UnixNano())
|
||||||
}
|
}
|
||||||
return "msg-" + hex.EncodeToString(b)
|
return "msg-" + hex.EncodeToString(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// generateThreadID creates a random thread ID.
|
// generateThreadID creates a random thread ID.
|
||||||
|
// Falls back to time-based ID if crypto/rand fails (extremely rare).
|
||||||
func generateThreadID() string {
|
func generateThreadID() string {
|
||||||
b := make([]byte, 6)
|
b := make([]byte, 6)
|
||||||
if _, err := rand.Read(b); err != nil {
|
if _, err := rand.Read(b); err != nil {
|
||||||
panic("crypto/rand.Read failed: " + err.Error())
|
// Fallback to time-based ID instead of panicking
|
||||||
|
return fmt.Sprintf("thread-%x", time.Now().UnixNano())
|
||||||
}
|
}
|
||||||
return "thread-" + hex.EncodeToString(b)
|
return "thread-" + hex.EncodeToString(b)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user