diff --git a/NOTICE b/NOTICE index 3abafa5a9..e0a9c643c 100644 --- a/NOTICE +++ b/NOTICE @@ -97,31 +97,3 @@ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -github.com/go-openapi/testify/_codegen/internal/imports -=========================== - -// SPDX-FileCopyrightText: Copyright (c) 2015 Ernesto Jiménez -// SPDX-License-Identifier: MIT - -The MIT License (MIT) - -Copyright (c) 2015 Ernesto Jiménez - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/README.md b/README.md index e4e868455..c77b21b73 100644 --- a/README.md +++ b/README.md @@ -198,13 +198,20 @@ some adaptations into this fork: * github.com/stretchr/testify#1772 - YAML library migration to maintained fork (go.yaml.in/yaml) * github.com/stretchr/testify#1797 - Codegen package consolidation and licensing * github.com/stretchr/testify#1356 - panic(nil) handling for Go 1.21+ +* github.com/stretchr/testify#1825 - Fix panic when using EqualValues with uncomparable types [merged] +* github.com/stretchr/testify#1818 - Fix panic on invalid regex in Regexp/NotRegexp assertions [merged] ### Planned merges #### Critical safety fixes (high priority) -* github.com/stretchr/testify#1825 - Fix panic when using EqualValues with uncomparable types -* github.com/stretchr/testify#1818 - Fix panic on invalid regex in Regexp/NotRegexp assertions +* Follow / adapt https://github.com/stretchr/testify/pull/1824 + +Not PRs, but reported issues in the original repo: + +* https://github.com/stretchr/testify/issues/1826 +* https://github.com/stretchr/testify/issues/1611 +* https://github.com/stretchr/testify/issues/1813 #### Leveraging internalized dependencies (go-spew, difflib) diff --git a/internal/assertions/object.go b/internal/assertions/object.go index 11c4cbd67..c760f95dc 100644 --- a/internal/assertions/object.go +++ b/internal/assertions/object.go @@ -25,9 +25,11 @@ func ObjectsAreEqual(expected, actual any) bool { if !ok { return false } + if exp == nil || act == nil { return exp == nil && act == nil } + return bytes.Equal(exp, act) } @@ -63,10 +65,25 @@ func ObjectsAreEqualValues(expected, actual any) bool { return false } + // Attempt conversion of expected to actual type. + // This handles more cases than just the ConvertibleTo check above. + if !expectedValue.CanConvert(actualType) { + // Types are not convertible, so they cannot be equal + // This prevents panics when calling [reflect.Value.Convert] + return false + } + + expectedConverted := expectedValue.Convert(actualType) + if !expectedConverted.CanInterface() { + // Cannot interface after conversion, so cannot be equal. + // This prevents panics when calling [reflect.Value.Interface]. + return false + } + if !isNumericType(expectedType) || !isNumericType(actualType) { - // Attempt comparison after type conversion + // Attempt comparison after type conversion. return reflect.DeepEqual( - expectedValue.Convert(actualType).Interface(), actual, + expectedConverted.Interface(), actual, ) } @@ -74,10 +91,23 @@ func ObjectsAreEqualValues(expected, actual any) bool { // to overflow or underflow. So, we need to make sure to always convert // the smaller type to a larger type before comparing. if expectedType.Size() >= actualType.Size() { - return actualValue.Convert(expectedType).Interface() == expected + if !actualValue.CanConvert(expectedType) { + // Cannot convert actual to the expected type, so cannot be equal. + // This is a hypothetical case to prevent panics when calling [reflect.Value.Convert]. + return false + } + + actualConverted := actualValue.Convert(expectedType) + if !actualConverted.CanInterface() { + // Cannot interface after conversion, so cannot be equal. + // This is a hypothetical case to prevent panics when calling [reflect.Value.Convert]. + return false + } + + return actualConverted.Interface() == expected } - return expectedValue.Convert(actualType).Interface() == actual + return expectedConverted.Interface() == actual } // isNumericType returns true if the type is one of: diff --git a/internal/assertions/object_test.go b/internal/assertions/object_test.go index 6de607dbc..5a50ea122 100644 --- a/internal/assertions/object_test.go +++ b/internal/assertions/object_test.go @@ -169,6 +169,7 @@ func objectEqualValuesCases() iter.Seq[objectEqualCase] { {3.14, complex128(1e+100 + 1e+100i), false}, {complex128(1e+10 + 1e+10i), complex64(1e+10 + 1e+10i), true}, {complex64(1e+10 + 1e+10i), complex128(1e+10 + 1e+10i), true}, + {[]int{1, 2}, (*[3]int)(nil), false}, // panics should be caught and treated as inequality (https://github.com/stretchr/testify/issues/1699) }) } diff --git a/internal/assertions/string.go b/internal/assertions/string.go index 3cb824771..0e8726554 100644 --- a/internal/assertions/string.go +++ b/internal/assertions/string.go @@ -22,7 +22,12 @@ func Regexp(t T, rx any, str any, msgAndArgs ...any) bool { h.Helper() } - match := matchRegexp(rx, str) + match, err := matchRegexp(rx, str) + if err != nil { + Fail(t, fmt.Sprintf("invalid regular expression %q: %v", rx, err), msgAndArgs...) + + return false + } if !match { Fail(t, fmt.Sprintf(`Expect "%v" to match "%v"`, str, rx), msgAndArgs...) @@ -44,7 +49,13 @@ func NotRegexp(t T, rx any, str any, msgAndArgs ...any) bool { if h, ok := t.(H); ok { h.Helper() } - match := matchRegexp(rx, str) + + match, err := matchRegexp(rx, str) + if err != nil { + Fail(t, fmt.Sprintf("invalid regular expression %q: %v", rx, err), msgAndArgs...) + + return false + } if match { Fail(t, fmt.Sprintf("Expect \"%v\" to NOT match \"%v\"", str, rx), msgAndArgs...) @@ -53,21 +64,28 @@ func NotRegexp(t T, rx any, str any, msgAndArgs ...any) bool { return !match } -// matchRegexp return true if a specified regexp matches a string. -func matchRegexp(rx any, str any) bool { +// matchRegexp returns whether the compiled regular expression matches the provided value. +// +// If rx is not a *[regexp.Regexp], rx is formatted with fmt.Sprint and compiled. +// When compilation fails, an error is returned instead of panicking. +func matchRegexp(rx any, str any) (bool, error) { var r *regexp.Regexp if rr, ok := rx.(*regexp.Regexp); ok { r = rr } else { - r = regexp.MustCompile(fmt.Sprint(rx)) + var err error + r, err = regexp.Compile(fmt.Sprint(rx)) + if err != nil { + return false, err + } } switch v := str.(type) { case []byte: - return r.Match(v) + return r.Match(v), nil case string: - return r.MatchString(v) + return r.MatchString(v), nil default: - return r.MatchString(fmt.Sprint(v)) + return r.MatchString(fmt.Sprint(v)), nil } } diff --git a/internal/assertions/string_test.go b/internal/assertions/string_test.go index bc805816f..aa7d7eb07 100644 --- a/internal/assertions/string_test.go +++ b/internal/assertions/string_test.go @@ -61,6 +61,37 @@ func TestStringRegexp(t *testing.T) { } } +// Verifies that invalid patterns no longer cause a panic when using Regexp/NotRegexp. +// Instead, the assertion should fail and return false. +func TestStringRegexp_InvalidPattern(t *testing.T) { + t.Parallel() + + const ( + invalidPattern = "\\C" + msg = "whatever" + ) + + t.Run("Regexp should not panic on invalid patterns", func(t *testing.T) { + result := NotPanics(t, func() { + mockT := new(testing.T) + False(t, Regexp(mockT, invalidPattern, msg)) + }) + if !result { + t.Failed() + } + }) + + t.Run("NoRegexp should not panic on invalid patterns", func(t *testing.T) { + result := NotPanics(t, func() { + mockT := new(testing.T) + False(t, NotRegexp(mockT, invalidPattern, msg)) + }) + if !result { + t.Failed() + } + }) +} + type stringEqualCase struct { equalWant string equalGot string