|
| 1 | +// Copyright The Conforma Contributors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// SPDX-License-Identifier: Apache-2.0 |
| 16 | + |
| 17 | +package utils |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "math" |
| 22 | +) |
| 23 | + |
| 24 | +// SafeAdd safely adds two integers, returning an error if overflow would occur |
| 25 | +func SafeAdd(a, b int) (int, error) { |
| 26 | + if a > 0 && b > math.MaxInt-a { |
| 27 | + return 0, fmt.Errorf("integer overflow: %d + %d would exceed MaxInt", a, b) |
| 28 | + } |
| 29 | + if a < 0 && b < math.MinInt-a { |
| 30 | + return 0, fmt.Errorf("integer overflow: %d + %d would exceed MinInt", a, b) |
| 31 | + } |
| 32 | + return a + b, nil |
| 33 | +} |
| 34 | + |
| 35 | +// SafeMultiply safely multiplies two integers, returning an error if overflow would occur |
| 36 | +func SafeMultiply(a, b int) (int, error) { |
| 37 | + if a == 0 || b == 0 { |
| 38 | + return 0, nil |
| 39 | + } |
| 40 | + |
| 41 | + // Check for overflow |
| 42 | + if a > 0 && b > 0 { |
| 43 | + if a > math.MaxInt/b { |
| 44 | + return 0, fmt.Errorf("integer overflow: %d * %d would exceed MaxInt", a, b) |
| 45 | + } |
| 46 | + } else if a < 0 && b < 0 { |
| 47 | + if a < math.MaxInt/b { |
| 48 | + return 0, fmt.Errorf("integer overflow: %d * %d would exceed MaxInt", a, b) |
| 49 | + } |
| 50 | + } else if a > 0 && b < 0 { |
| 51 | + if b < math.MinInt/a { |
| 52 | + return 0, fmt.Errorf("integer overflow: %d * %d would exceed MinInt", a, b) |
| 53 | + } |
| 54 | + } else if a < 0 && b > 0 { |
| 55 | + if a < math.MinInt/b { |
| 56 | + return 0, fmt.Errorf("integer overflow: %d * %d would exceed MinInt", a, b) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return a * b, nil |
| 61 | +} |
| 62 | + |
| 63 | +// SafeCapacity calculates a safe capacity for slice allocation |
| 64 | +// It prevents integer overflow and provides reasonable fallbacks |
| 65 | +func SafeCapacity(base int, multiplier int) int { |
| 66 | + capacity, err := SafeMultiply(base, multiplier) |
| 67 | + if err != nil { |
| 68 | + // Fallback to a reasonable maximum |
| 69 | + return math.MaxInt / 4 |
| 70 | + } |
| 71 | + |
| 72 | + // Additional safety check for extremely large values |
| 73 | + const maxReasonableCapacity = 10000000 // 10 million |
| 74 | + if capacity > maxReasonableCapacity { |
| 75 | + return maxReasonableCapacity |
| 76 | + } |
| 77 | + |
| 78 | + return capacity |
| 79 | +} |
| 80 | + |
| 81 | +// SafeSliceCapacity calculates safe capacity for slice allocation with multiple length additions |
| 82 | +func SafeSliceCapacity(lengths ...int) int { |
| 83 | + total := 0 |
| 84 | + for _, length := range lengths { |
| 85 | + sum, err := SafeAdd(total, length) |
| 86 | + if err != nil { |
| 87 | + // Fallback to reasonable maximum |
| 88 | + return math.MaxInt / 4 |
| 89 | + } |
| 90 | + total = sum |
| 91 | + } |
| 92 | + |
| 93 | + // Additional safety check |
| 94 | + const maxReasonableCapacity = 10000000 // 10 million |
| 95 | + if total > maxReasonableCapacity { |
| 96 | + return maxReasonableCapacity |
| 97 | + } |
| 98 | + |
| 99 | + return total |
| 100 | +} |
| 101 | + |
| 102 | +// ValidateSliceLength validates that a slice length is within reasonable bounds |
| 103 | +func ValidateSliceLength(length int, maxAllowed int) error { |
| 104 | + if length < 0 { |
| 105 | + return fmt.Errorf("negative slice length: %d", length) |
| 106 | + } |
| 107 | + if length > maxAllowed { |
| 108 | + return fmt.Errorf("slice length %d exceeds maximum allowed %d", length, maxAllowed) |
| 109 | + } |
| 110 | + return nil |
| 111 | +} |
0 commit comments