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

Prevent "index out of bounds" error when sync::SegmentedCache was created with a non-power-of-two segments #117

Merged
merged 2 commits into from
Apr 12, 2022

Conversation

tatsuya6502
Copy link
Member

@tatsuya6502 tatsuya6502 commented Apr 12, 2022

This PR fixes sync::SegmentedCache to create the correct number of internal segments when the specified number of segments is not a power of two (e.g. 5).

When a segmented cache is created with such a number of segments (e.g. 5), the number of the actual internal segments should be rounded up to a nearest power of two (e.g. 8). However the old code only created 5 of segments and caused a panic with "index out of bounds" when writing to or reading from the cache.

let cache = SegmentedCache::new(100, 5); // max 100 entries, 5 segments
assert_eq!(cache.iter().count(), 0);
thread ... panicked at 'index out of bounds: the len is 5 but the index is 5', 
   src/sync/segment.rs:546:10

In addition to fixing the bug, add an unit test sync::segment::tests::non_power_of_two_segments to detect similar bugs in the future.

@tatsuya6502 tatsuya6502 self-assigned this Apr 12, 2022
@tatsuya6502 tatsuya6502 added the bug Something isn't working label Apr 12, 2022
@tatsuya6502 tatsuya6502 added this to the v0.8.2 milestone Apr 12, 2022
Copy link
Member Author

@tatsuya6502 tatsuya6502 left a comment

Choose a reason for hiding this comment

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

Merging.

@tatsuya6502 tatsuya6502 merged commit 8c4984d into master Apr 12, 2022
@tatsuya6502 tatsuya6502 deleted the fix-num-seg branch April 12, 2022 01:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant