diff options
| author | Douglas Rumbaugh <dbr4@psu.edu> | 2024-03-22 14:15:00 -0400 |
|---|---|---|
| committer | Douglas Rumbaugh <dbr4@psu.edu> | 2024-03-22 14:15:00 -0400 |
| commit | d0aebc685b245e51bf47cff8e28f811e43073d5e (patch) | |
| tree | ee0c044786ffafd6165bc2d833b36a75f5d6e68b /include | |
| parent | 3d80e9fa42273f7e0029ecb66fd13fd78fc4a694 (diff) | |
| download | dynamic-extension-d0aebc685b245e51bf47cff8e28f811e43073d5e.tar.gz | |
PGM.h: fixed an out of bounds array access on point lookup misses.
Diffstat (limited to 'include')
| -rw-r--r-- | include/shard/PGM.h | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/include/shard/PGM.h b/include/shard/PGM.h index e2752ef..ff9ce2d 100644 --- a/include/shard/PGM.h +++ b/include/shard/PGM.h @@ -147,14 +147,16 @@ public: return m_reccnt; } - // If the region to search is less than some pre-specified - // amount, perform a linear scan to locate the record. + /* + * If the region to search is less than some pre-specified + * amount, perform a linear scan to locate the record. + */ if (bound.hi - bound.lo < 256) { while (idx < bound.hi && m_data[idx].rec.key < key) { idx++; } } else { - // Otherwise, perform a binary search + /* Otherwise, perform a binary search */ idx = bound.lo; size_t max = bound.hi; @@ -169,10 +171,26 @@ public: } + /* + * the upper bound returned by PGM is one passed the end of the + * array. If we are at that point, we should just return "not found" + */ + if (idx == m_reccnt) { + return idx; + } + + /* + * We may have walked one passed the actual lower bound, so check + * the index before the current one to see if it is the actual bound + */ if (m_data[idx].rec.key > key && idx > 0 && m_data[idx-1].rec.key <= key) { return idx-1; } + /* + * Otherwise, check idx. If it is a valid bound, then return it, + * otherwise return "not found". + */ return (m_data[idx].rec.key >= key) ? idx : m_reccnt; } |