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

fix: enforce int64 for large integer values in tests #120

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

arthurbdiniz
Copy link

The bug arises from the use of generic integer types (int) in test cases involving large numerical values that exceed the range of a 32-bit integer.

In Go, the size of the int type depends on the platform, this behavior causes inconsistencies when handling large integers, such as -9223372036854775808 and 9007199254740993.

On 32-bit systems, these values cannot be represented correctly as int, leading to potential overflow or truncation errors. By explicitly using the int64 type in the affected test cases, the code ensures proper handling and representation of large integers across all platforms, avoiding platform-specific bugs and improving the reliability of the tests.

Reproduce

Run tests setting GOARCH to 386.

GOARCH=386 GOOS=linux go test -vet=off -v -p 2 sigs.k8s.io/yaml sigs.k8s.io/yaml/goyaml.v2 sigs.k8s.io/yaml/goyaml.v3


# sigs.k8s.io/yaml/goyaml.v2_test [sigs.k8s.io/yaml/goyaml.v2.test]
goyaml.v2/decode_test.go:134:33: cannot use -9223372036854775808 (untyped int constant) as int value in map literal (overflows)
# sigs.k8s.io/yaml [sigs.k8s.io/yaml.test]
./yaml_test.go:186:50: cannot use math.MaxInt64 (untyped int constant 9223372036854775807) as int value in argument to fmt.Sprintf (overflows)
./yaml_test.go:415:16: cannot use 9007199254740993 (untyped int constant) as int value in struct literal (overflows)
FAIL	sigs.k8s.io/yaml [build failed]
FAIL	sigs.k8s.io/yaml/goyaml.v2 [build failed]
# sigs.k8s.io/yaml/goyaml.v3_test [sigs.k8s.io/yaml/goyaml.v3.test]
goyaml.v3/decode_test.go:180:33: cannot use -9223372036854775808 (untyped int constant) as int value in map literal (overflows)
FAIL	sigs.k8s.io/yaml/goyaml.v3 [build failed]
FAIL

Changes

  • Updated test cases in goyaml.v2 and goyaml.v3 decode_test.go to explicitly use int64 for representing large integer values.
  • Adjusted yaml_test.go to:
    • Use int64 for math.MaxInt64 in marshaling tests.
    • Decode integers greater than 2^53 into int64 instead of int to ensure proper handling of large values.

These changes address potential issues with integer overflow and ensure compatibility across platforms with differing integer size representations.

- Updated test cases in `goyaml.v2` and `goyaml.v3` `decode_test.go` to
  explicitly use `int64` for representing large integer values.
- Adjusted `yaml_test.go` to:
  - Use `int64` for `math.MaxInt64` in marshaling tests.
  - Decode integers greater than `2^53` into `int64` instead of `int`
    to ensure proper handling of large values.

These changes address potential issues with integer overflow and ensure
compatibility across platforms with differing integer size representations.

Signed-off-by: Arthur Diniz <arthurbdiniz@gmail.com>
@k8s-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: arthurbdiniz
Once this PR has been reviewed and has the lgtm label, please assign jpbetz for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. label Nov 15, 2024
@k8s-ci-robot k8s-ci-robot requested review from dims and sttts November 15, 2024 17:08
Copy link

linux-foundation-easycla bot commented Nov 15, 2024

CLA Signed


The committers listed above are authorized under a signed CLA.

@k8s-ci-robot
Copy link

Welcome @arthurbdiniz!

It looks like this is your first PR to kubernetes-sigs/yaml 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/yaml has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Nov 15, 2024
Copy link
Author

@arthurbdiniz arthurbdiniz left a comment

Choose a reason for hiding this comment

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

Added an amended commit to my fork and for some reason it was not synced here.

master...arthurbdiniz:yaml:fix/tests-32bit-architectures

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants