safeAllocationCapacity returns the summed capacity hint when it fits in int. When the total would overflow, it falls back to 0 so callers can skip preallocation without changing correctness.
(parts ...int)
| 12 | // When the total would overflow, it falls back to 0 so callers can skip |
| 13 | // preallocation without changing correctness. |
| 14 | func safeAllocationCapacity(parts ...int) int { |
| 15 | total := 0 |
| 16 | for _, part := range parts { |
| 17 | if part < 0 || total > math.MaxInt-part { |
| 18 | allocationLog.Printf("Capacity hint overflow or negative part (part=%d, running total=%d), falling back to 0", part, total) |
| 19 | return 0 |
| 20 | } |
| 21 | total += part |
| 22 | } |
| 23 | return total |
| 24 | } |