Repository navigation
Fix integer overflow when clamping large size_t options (#462) - #1371
Open
xhon-pelushi wants to merge 2 commits into
Open
xhon-pelushi wants to merge 2 commits into
xhon-pelushi wants to merge 2 commits into
Conversation
ClipToRange() cast the pointee to the bound type V before comparing. The bounds are int literals (e.g. 1 << 30), so V is deduced as int, and a size_t option of 2 GiB or more became negative under the cast: write_buffer_size and max_file_size were then clamped to the minimum instead of the maximum, and values >= 4 GiB wrapped modulo 2^32. Compare in the pointee's type T instead, casting the bounds to T. Options at or below the limits are unaffected, and int options such as max_open_files still clamp as before. Adds DBTest.SanitizeOptionsOverflow. Fixes google#462
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
SanitizeOptions()clamps size options withClipToRange():write_buffer_size/max_file_size/block_sizearesize_t, but the bound literals areint, soVisintandstatic_cast<V>(*ptr)truncates. Awrite_buffer_sizeof 2 GiB casts to a negativeint, so it is clamped to the minimum (64 KiB) rather than the 1 GiB maximum; values >= 4 GiB wrap modulo 2^32 (#462).Fix
Compare in the pointee's type
T, casting the bounds up toT:All the min/max bounds here are positive, so casting them to
size_tis well-defined, andintoptions (max_open_files) are unchanged.Test
DBTest.SanitizeOptionsOverflowcallsSanitizeOptions()with 2 GiB and 4 GiB sizes and asserts they clamp to the documented maxima (1 GiB / 4 MiB), plus a negativemax_open_filesclamping to the minimum. It fails onmain(2 GiB clamps to the minimum) and passes with this change.Fixes #462
The regression test is adapted from the earlier PR #1356, which the author self-closed.