8000 Fix possible signed integer overflow in ijToIjk by isaacbrodsky · Pull Request #733 · uber/h3 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix possible signed integer overflow in ijToIjk #733

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The public API of this library consists of the functions declared in file

### Fixed
- Fixed possible signed integer overflow in `h3NeighborRotations` (#707)
- Fixed possible signed integer overflow in `localIjToCell` (#706)
- Fixed possible signed integer overflow in `localIjToCell` (#706, #733)

### Changed
- `assert` on defensive code blocks that are not already covered. (#720)
Expand Down
15 changes: 15 additions & 0 deletions src/apps/testapps/testCellToLocalIj.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,19 @@ SUITE(h3ToLocalIj) {
t_assert(H3_EXPORT(localIjToCell)(origin, &ij, 0, &out) == E_FAILED,
"High magnitude J and I components fail");
}

TEST(localIjToCell_overflow_particularCases) {
H3Index origin;
setH3Index(&origin, 2, 2, CENTER_DIGIT);
CoordIJ ij = {.i = 553648127, .j = -2145378272};
H3Index out;
t_assert(H3_EXPORT(localIjToCell)(origin, &ij, 0, &out) == E_FAILED,
"Particular high magnitude J and I components fail (1)");

setH3Index(&origin, 2, 2, CENTER_DIGIT);
ij.i = INT32_MAX - 10;
ij.j = -11;
t_assert(H3_EXPORT(localIjToCell)(origin, &ij, 0, &out) == E_FAILED,
"Particular high magnitude J and I components fail (2)");
}
}
6 changes: 5 additions & 1 deletion src/h3lib/lib/coordijk.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,13 +555,17 @@ H3Error ijToIjk(const CoordIJ *ij, CoordIJK *ijk) {
// positive signed integer minus another positive signed integer will
// not overflow.
if (max < INT32_MIN - min) {
// max - min would overflow
// max + min would overflow
return E_FAILED;
}
if (min == INT32_MIN) {
// 0 - INT32_MIN would overflow
return E_FAILED;
}
if (max > INT32_MAX + min) {
// max - min would overflow
return E_FAILED;
}
}

_ijkNormalize(ijk);
Expand Down
0