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

Fix potential OOB read in localIjToCell #684

Merged
merged 1 commit into from
Sep 12, 2022
Merged
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
8 changes: 8 additions & 0 deletions src/apps/testapps/testCellToLocalIj.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,12 @@ SUITE(h3ToLocalIj) {
"Invalid mode fail for cellToLocalIj");
}
}

TEST(invalid_negativeIj) {
H3Index index = 0x200f202020202020;
CoordIJ ij = {.i = -14671840, .j = -2147483648};
H3Index out;
t_assert(H3_EXPORT(localIjToCell)(index, &ij, 0, &out) == E_FAILED,
"Negative I and J components fail");
}
}
9 changes: 5 additions & 4 deletions src/h3lib/lib/coordijk.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,12 @@ void _ijkNormalize(CoordIJK *c) {
}

/**
* Determines the H3 digit corresponding to a unit vector in ijk coordinates.
* Determines the H3 digit corresponding to a unit vector or the zero vector
* in ijk coordinates.
*
* @param ijk The ijk coordinates; must be a unit vector.
* @return The H3 digit (0-6) corresponding to the ijk unit vector, or
* INVALID_DIGIT on failure.
* @param ijk The ijk coordinates; must be a unit vector or zero vector.
* @return The H3 digit (0-6) corresponding to the ijk unit vector, zero vector,
* or INVALID_DIGIT (7) on failure.
*/
Direction _unitIjkToDigit(const CoordIJK *ijk) {
CoordIJK c = *ijk;
Expand Down
6 changes: 3 additions & 3 deletions src/h3lib/lib/localij.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,12 @@ H3Error localIjkToCell(H3Index origin, const CoordIJK *ijk, H3Index *out) {

// check for res 0/base cell
if (res == 0) {
if (ijk->i > 1 || ijk->j > 1 || ijk->k > 1) {
// out of range input
const Direction dir = _unitIjkToDigit(ijk);
if (dir == INVALID_DIGIT) {
// out of range input - not a unit vector or zero vector
return E_FAILED;
}

const Direction dir = _unitIjkToDigit(ijk);
const int newBaseCell = _getBaseCellNeighbor(originBaseCell, dir);
if (newBaseCell == INVALID_BASE_CELL) {
// Moving in an invalid direction off a pentagon.
Expand Down