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

Fail fast on integer overflow #549

Merged
merged 4 commits into from
Jan 6, 2022
Merged

Conversation

iamdanfox
Copy link
Contributor

Before this PR

In PDS-235827, we've encountered some version strings which are constructed by doing x.y.yymmddhhmm. It turns out this was fine last year, but now produces numbers like 1.1.2201051644 which is larger than Integer.MAX_VALUE. The consequence is a bit scary - in the latest version of sls-version-java we'll actually tolerate these, but internally when we store the .patchVersionNumber(groups.groupAsInt(3));, the giant integer overflows to becoem a negative number so they SORT WRONG.

After this PR

==COMMIT_MSG==
OrderableSlsVersion.valueOf will now refuse to interpret a version string containing a number greater than Integer.MAX_VALUE as a valid orderable version.
==COMMIT_MSG==

The reason I want to fail hard rather than try to relax this is because the abstract SlsVersion class imposes these fun methods which all mention integers, and I have no interest in causing an ABI break just for this.

    public abstract int getMajorVersionNumber();

    public abstract int getMinorVersionNumber();

    public abstract int getPatchVersionNumber();

    public abstract OptionalInt firstSequenceVersionNumber();

    public abstract OptionalInt secondSequenceVersionNumber();

Possible downsides?

@changelog-app
Copy link

changelog-app bot commented Jan 6, 2022

Generate changelog in changelog/@unreleased

Type

  • Feature
  • Improvement
  • Fix
  • Break
  • Deprecation
  • Manual task
  • Migration

Description

OrderableSlsVersion.valueOf will now refuse to interpret a version string containing a number greater than Integer.MAX_VALUE as a valid orderable version. Previously they would be parsed, but have surprising sorting behaviour.

Check the box to generate changelog(s)

  • Generate changelog entry

@policy-bot policy-bot bot requested a review from xRuiAlves January 6, 2022 18:08
Comment on lines 50 to 53
throw new SafeIllegalStateException(
"Can't parse segment as integer as it overflowed",
SafeArg.of("string", string),
SafeArg.of("segment", string.substring(groupStart, groupEnd)));
Copy link
Contributor

Choose a reason for hiding this comment

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

pedantic nit: We may want to extract this to a integerOverflow(String,int,int); function to cut negative test data out of hot paths for better inlining

Copy link
Contributor

@carterkozak carterkozak left a comment

Choose a reason for hiding this comment

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

Thanks!

@bulldozer-bot bulldozer-bot bot merged commit 323ce7a into develop Jan 6, 2022
@bulldozer-bot bulldozer-bot bot deleted the dfox/integer-overflow branch January 6, 2022 18:19
@svc-autorelease
Copy link
Collaborator

Released 1.3.0

@iamdanfox iamdanfox restored the dfox/integer-overflow branch January 6, 2022 18:22
@iamdanfox iamdanfox deleted the dfox/integer-overflow branch January 6, 2022 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants