Skip to content

BeNumerically finds -1 equal to uint64(math.MaxUint64) and uint(5) not greater than -3 #925

Closed

Description

@DRMacIver

When one operand is a negative signed integer and the other is an unsigned integer, BeNumerically gives the wrong answer: -1 compares == to uint64(math.MaxUint64) in either order, and uint(5) does not compare > to -3, though int(5) does:

package main

import (
	"fmt"
	"math"

	"github.com/onsi/gomega"
)

func main() {
	ok, err := gomega.BeNumerically("==", uint64(math.MaxUint64)).Match(-1)
	fmt.Println("-1 == uint64(MaxUint64):", ok, err)
	ok, err = gomega.BeNumerically("==", -1).Match(uint64(math.MaxUint64))
	fmt.Println("uint64(MaxUint64) == -1:", ok, err)
	ok, err = gomega.BeNumerically(">", -3).Match(uint(5))
	fmt.Println("uint(5) > -3:           ", ok, err)
	ok, err = gomega.BeNumerically(">", -3).Match(5)
	fmt.Println("int(5) > -3:            ", ok, err)
}

Output:

-1 == uint64(MaxUint64): true <nil>
uint64(MaxUint64) == -1: true <nil>
uint(5) > -3:            false <nil>
int(5) > -3:             true <nil>

I expected the first two to be false and the third true, as the last line gives for int(5). The doc comment says the specific type of number is irrelevant, so I would expect a signed and an unsigned integer to be compared by value.

Tested on v1.43.1, which is also the current master (47f0f87).

BTW, this was found by an automated program that writes property-based tests for various open source projects using hegel (but it has been reviewed by hand before reporting). We've also potentially found (but not yet hand validated) 13 other bugs in gomega. You can see the tests at https://github.com/hegeldev/hegel-zoo/tree/main/targets/go/gomega. Let us know if you would like us to file the other bugs found and/or contribute the tests. NB the tests are currently LLM generated and probably not yet suitable for inclusion as is, but we're happy to help get them into a better state if you want them.

Activity

  1. onsi commented on Sep 23, 2026

    @onsi
    Owner

    cool - yeah please file all the bugs and i'll get them fixed.

  2. DRMacIver commented on Sep 24, 2026

    @DRMacIver
    Author

    Done! There ended up being slightly fewer than the initial report claimed because, as expected, several of the previously found "bugs" seem bogus. I've reported all of the found issues that look legit.

  3. added a commit that references this issue on Sep 24, 2026
  4. onsi commented on Sep 24, 2026

    @onsi
    Owner

    I've delegated fixing this to Claude and checked the code it generated. Thanks for reporting!

    Integer comparisons in BeNumerically now go through math/big, so signed and unsigned integers are compared by their exact value whatever their signedness: -1 no longer equals uint64(math.MaxUint64) and uint(5) is now > -3. (26e3c6b)

    This will be released in v1.44.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions