Skip to content

Fix overflow in check_tile_extent() - #1635

Merged
joe-maley merged 1 commit into
devfrom
jpm/check_tile_extent_floor_overflow
May 5, 2020
Merged

joe-maley merged 1 commit into
devfrom
jpm/check_tile_extent_floor_overflow

Conversation

@joe-maley

Copy link
Copy Markdown
Contributor

Within Dimension::check_tile_extent<T>(), the following variable has a type
deduction from auto to uint64_t:
auto upper_floor =
((range - 1) / (*tile_extent)) * (*tile_extent) + domain[0];

If domain[0] is negative, it may overflow the upper_floor. A trivial example
would be a domain with a range of [-10, -5] with a tile extent size of 5. The
above calculation comes out to -5, which overflows to (MAXINT64 - 5).

If we force the type of upper_floor to uint64_t, it solves the above issue
but causes overflow if the tile extent is an unsigned type with a domain
that overflows the uint64_t.

This patch adds both a path for unsigned and signed extent tile types. The
upper_floor type must match the sign of the extent tile type.

This also uncovered a bug in the unit-cppapi-datetimes.cc where the domain
exceeds the expanded upper bound but the check was a false-negative.

Within `Dimension::check_tile_extent<T>()`, the following variable has a type
deduction from `auto` to `uint64_t`:
auto upper_floor =
          ((range - 1) / (*tile_extent)) * (*tile_extent) + domain[0];

If `domain[0]` is negative, it may overflow the upper_floor. A trivial example
would be a domain with a range of [-10, -5] with a tile extent size of 5. The
above calculation comes out to -5, which overflows to (MAXINT64 - 5).

If we force the type of `upper_floor` to uint64_t, it solves the above issue
but causes overflow if the tile extent is an unsigned type with a domain
that overflows the uint64_t.

This patch adds both a path for unsigned and signed extent tile types. The
upper_floor type must match the sign of the extent tile type.

This also uncovered a bug in the unit-cppapi-datetimes.cc where the domain
exceeds the expanded upper bound but the check was a false-negative.

@stavrospapadopoulos stavrospapadopoulos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great catch, thanks!

@joe-maley
joe-maley merged commit b894626 into dev May 5, 2020
@joe-maley
joe-maley deleted the jpm/check_tile_extent_floor_overflow branch May 5, 2020 18:11
Shelnutt2 pushed a commit that referenced this pull request May 13, 2020
Within `Dimension::check_tile_extent<T>()`, the following variable has a type
deduction from `auto` to `uint64_t`:
auto upper_floor =
          ((range - 1) / (*tile_extent)) * (*tile_extent) + domain[0];

If `domain[0]` is negative, it may overflow the upper_floor. A trivial example
would be a domain with a range of [-10, -5] with a tile extent size of 5. The
above calculation comes out to -5, which overflows to (MAXINT64 - 5).

If we force the type of `upper_floor` to uint64_t, it solves the above issue
but causes overflow if the tile extent is an unsigned type with a domain
that overflows the uint64_t.

This patch adds both a path for unsigned and signed extent tile types. The
upper_floor type must match the sign of the extent tile type.

This also uncovered a bug in the unit-cppapi-datetimes.cc where the domain
exceeds the expanded upper bound but the check was a false-negative.
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