Skip to content

Guard against int64 overflow when building instances with huge profits#13

Merged
fontanf merged 1 commit into
masterfrom
fix/profit-overflow-checks
Jul 1, 2026
Merged

Guard against int64 overflow when building instances with huge profits#13
fontanf merged 1 commit into
masterfrom
fix/profit-overflow-checks

Conversation

@fontanf

@fontanf fontanf commented Jul 1, 2026

Copy link
Copy Markdown
Owner

Instances with profits near INT64_MAX could silently corrupt total_item_profit_ (signed overflow wraps to a negative bound) or overflow inside upper_bound_reverse's (capacity - weight) * profit term, since a state's weight can exceed the capacity by up to two item weights. Both now throw std::overflow_error at build time instead of corrupting state or crashing mid-algorithm.

InstanceFromFloatProfitsBuilder's multiplier selection also trusted a double-precision estimate that isn't accurate enough at this magnitude; it now verifies the actual rounded integer profits and backs off the multiplier if they would overflow.

Instances with profits near INT64_MAX could silently corrupt
total_item_profit_ (signed overflow wraps to a negative bound) or
overflow inside upper_bound_reverse's (capacity - weight) * profit
term, since a state's weight can exceed the capacity by up to two item
weights. Both now throw std::overflow_error at build time instead of
corrupting state or crashing mid-algorithm.

InstanceFromFloatProfitsBuilder's multiplier selection also trusted a
double-precision estimate that isn't accurate enough at this
magnitude; it now verifies the actual rounded integer profits and
backs off the multiplier if they would overflow.
@fontanf fontanf merged commit 605153f into master Jul 1, 2026
3 checks passed
@fontanf fontanf deleted the fix/profit-overflow-checks branch July 1, 2026 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant