Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert: Fix EqualValues to handle overflow/underflow #1531

Merged
merged 1 commit into from
Feb 14, 2024

Conversation

arjunmahishi
Copy link
Collaborator

@arjunmahishi arjunmahishi commented Feb 10, 2024

Summary

The underlying function ObjectsAreEqualValues did not handle overflow/underflow of values while converting one type to another for comparison. For example:

    EqualValues(t, int(270), int8(14))

would return true, even though the values are not equal. Because, when you convert int(270) to int8, it overflows and becomes 14 (270 % 256 = 14)

Changes

This commit fixes that by making sure that the conversion always happens from the smaller type to the larger type, and then comparing the values. Additionally, this commit also separates out the test cases of ObjectsAreEqualValues from TestObjectsAreEqual.

Related issues

Closes #1462

Copy link
Collaborator

@MovieStoreGuy MovieStoreGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor revision from me, and excuse as I become more familiar with helping to maintain the project.

assert/assertions.go Outdated Show resolved Hide resolved
assert/assertions_test.go Show resolved Hide resolved
MovieStoreGuy
MovieStoreGuy previously approved these changes Feb 12, 2024
@arjunmahishi arjunmahishi added bug pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require labels Feb 13, 2024
Copy link
Collaborator

@brackendawson brackendawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently complex isn't numeric, so this still behaves incorrectly:

	EqualValues(t, complex128(1e+100+1e+100i), complex64(complex(math.Inf(0), math.Inf(0))))

I'm open to not fixing this case. Also I wonder why complex isn't numeric.

assert/assertions.go Outdated Show resolved Hide resolved
The underlying function ObjectsAreEqualValues did not handle
overflow/underflow of values while converting one type to another
for comparison. For example:

    EqualValues(t, int(270), int8(14))

would return true, even though the values are not equal. Because, when
you convert int(270) to int8, it overflows and becomes 14 (270 % 256 = 14)

This commit fixes that by making sure that the conversion always happens
from the smaller type to the larger type, and then comparing the values.
Additionally, this commit also seperates out the test cases of
ObjectsAreEqualValues from TestObjectsAreEqual.

Fixes stretchr#1462
@brackendawson brackendawson merged commit 5911e38 into stretchr:master Feb 14, 2024
7 checks passed
@arjunmahishi arjunmahishi deleted the fix-equal-values branch February 14, 2024 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug pkg-assert Change related to package testify/assert pkg-require Change related to package testify/require
Projects
None yet
Development

Successfully merging this pull request may close these issues.

assert: EqualValues does not check for overflows
3 participants