Skip to content

Conversation

Gezi-lzq
Copy link
Contributor

cherry-pick 201c6eb

  • fix(log): Prevent potential offset overflow in ElasticLogSegment

This commit addresses an issue where a log segment could accommodate more than Integer.MAX_VALUE records, leading to a potential integer overflow when calculating relative offsets.

The root cause was that the check offset - baseOffset <= Integer.MAX_VALUE allowed a relative offset to be exactly Integer.MAX_VALUE. Since offsets are 0-based, this allows for Integer.MAX_VALUE + 1 records, which cannot be represented by a standard Integer.

This fix implements the following changes:

  1. In ElasticLogSegment, the offset validation is changed from <= to < Integer.MAX_VALUE to ensure the relative offset strictly fits within an Integer's bounds.
  2. In LogCleaner, a new segment grouping method groupSegmentsBySizeV2 is introduced for ElasticUnifiedLog. This method uses the same stricter offset check to prevent incorrectly grouping segments that would exceed the offset limit.
  3. The corresponding unit tests in LogCleanerTest have been updated to reflect these new boundaries and validate the fix.

Fixes: #2718

* fix(log): Prevent potential offset overflow in ElasticLogSegment

This commit addresses an issue where a log segment could accommodate more than Integer.MAX_VALUE records, leading to a potential integer overflow when calculating relative offsets.

The root cause was that the check `offset - baseOffset <= Integer.MAX_VALUE` allowed a relative offset to be exactly `Integer.MAX_VALUE`. Since offsets are 0-based, this allows for `Integer.MAX_VALUE + 1` records, which cannot be represented by a standard Integer.

This fix implements the following changes:
1.  In `ElasticLogSegment`, the offset validation is changed from `<=` to `< Integer.MAX_VALUE` to ensure the relative offset strictly fits within an Integer's bounds.
2.  In `LogCleaner`, a new segment grouping method `groupSegmentsBySizeV2` is introduced for `ElasticUnifiedLog`. This method uses the same stricter offset check to prevent incorrectly grouping segments that would exceed the offset limit.
3.  The corresponding unit tests in `LogCleanerTest` have been updated to reflect these new boundaries and validate the fix.

Fixes: #2718

* fix(logCleaner): unify segment grouping logic

* fix(logCleaner): extract offset range check for segment grouping to prevent overflow in ElasticLogSegment

* style(logCleaner): fix indentation in segment grouping condition for readability

* style(logCleaner): fix line break in offset range check for readability

* chore: add AutoMQ inject

* style(logCleaner): remove unnecessary blank line after segment grouping

* fix(stream): validate record batch count to prevent negative values in append
@Gezi-lzq Gezi-lzq merged commit 92b6f7e into 1.5 Jul 30, 2025
6 checks passed
@Gezi-lzq Gezi-lzq deleted the pick-2720-5 branch July 30, 2025 04:42
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.

2 participants