Skip to content

Commit d045d71

Browse files
committed
fix: ci_is_less OOB read
1 parent f5727ed commit d045d71

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/grammar/ci_string.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,16 @@ ci_is_less(
6262
{
6363
auto p1 = s0.data();
6464
auto p2 = s1.data();
65-
for(auto n = s0.size();n--;)
65+
auto n = s0.size() < s1.size()
66+
? s0.size() : s1.size();
67+
while(n--)
6668
{
6769
auto c1 = to_lower(*p1++);
6870
auto c2 = to_lower(*p2++);
6971
if(c1 != c2)
7072
return c1 < c2;
7173
}
72-
// equal
73-
return false;
74+
return s0.size() < s1.size();
7475
}
7576

7677
} // detail

test/unit/grammar/ci_string.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ class ascii_test
110110
static_assert(std::is_same<
111111
decltype(ci_less{}("a", "b")),
112112
bool>::value, "");
113+
114+
// ci_is_less with mismatched lengths
115+
// (OOB read regression)
116+
BOOST_TEST(ci_is_less("ab", "abc"));
117+
BOOST_TEST(! ci_is_less("abc", "ab"));
118+
BOOST_TEST(! ci_is_less("ABC", "ab"));
113119
}
114120

115121
void

0 commit comments

Comments
 (0)