Skip to content

Commit 2639de7

Browse files
committed
Fix is_within_function_body bug that was missing the final function body
1 parent 0fa4217 commit 2639de7

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

source/parse.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,6 +2299,12 @@ class parser
22992299
public:
23002300
auto is_within_function_body(source_position p) const
23012301
{
2302+
// Short circuit the empty case, so that the rest of the function
2303+
// can unconditionally decrement any non-.begin() iterator once
2304+
if (function_body_extents.empty()) {
2305+
return false;
2306+
}
2307+
23022308
// Ensure we are sorted
23032309
if (!is_function_body_extents_sorted) {
23042310
std::sort(
@@ -2308,18 +2314,22 @@ class parser
23082314
is_function_body_extents_sorted = true;
23092315
}
23102316

2311-
// Find the first entry that is beyond pos
2317+
// Find the first entry that is beyond pos, and back up one to
2318+
// the last that could be a match; this also ensures iter is
2319+
// dereferenceable, not .end()
23122320
auto iter = std::lower_bound(
23132321
function_body_extents.begin(),
23142322
function_body_extents.end(),
23152323
p.lineno+1
23162324
);
2325+
if (iter != function_body_extents.begin()) {
2326+
--iter;
2327+
}
23172328

23182329
// Now go backwards through the preceding entries until
23192330
// one includes pos or we move before pos
23202331
while (
2321-
iter != function_body_extents.end()
2322-
&& iter->last >= p.lineno
2332+
iter->last >= p.lineno
23232333
)
23242334
{
23252335
if (

0 commit comments

Comments
 (0)