Skip to content

Commit 022a11a

Browse files
committed
fix(linux): Fix iterator in test backspace handling
Replace reverse iterator loop that holds a stale iterator across pop_back() calls with a pattern that directly accesses context.back() on each iteration. This avoids undefined behavior from iterator invalidation when mutating the vector. The previous code was similar to the code used in production (ldml_process.cpp, ldml_event_state::emit_backspace()). However, in production we use a list where this works, but here in the tests we use a vector where the behavior is undefined. Test-bot: skip
1 parent 6540886 commit 022a11a

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

core/tests/unit/ldml/ldml.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,21 +157,20 @@ apply_action(
157157
matched_text = true; // no text to match as context is empty.
158158
}
159159
// now, we need to simulate what ldml_processor::emit_backspace() is going to do.
160-
auto end = context.rbegin();
161-
while (end != context.rend()) {
162-
if (end->type == KM_CORE_CT_CHAR) {
163-
test_assert(!matched_text);
164-
test_assert_equal(end->character, ch); // expect popped char to be same as what's in context
165-
matched_text = true;
166-
context.pop_back();
167-
break; // exit on first real char
168-
}
169-
test_assert(end->type != KM_CORE_CT_END); // inappropriate here.
160+
while (!context.empty()) {
161+
if (context.back().type == KM_CORE_CT_CHAR) {
162+
test_assert(!matched_text);
163+
test_assert_equal(context.back().character, ch); // expect popped char to be same as what's in context
164+
matched_text = true;
170165
context.pop_back();
166+
break; // exit on first real char
171167
}
172-
test_assert(matched_text);
168+
test_assert(context.back().type != KM_CORE_CT_END); // inappropriate here.
169+
context.pop_back();
173170
}
171+
test_assert(matched_text);
174172
break;
173+
}
175174
case KM_CORE_IT_PERSIST_OPT:
176175
std::cout << " + TODO-LDML: persist_opt()" << std::endl;
177176
break;

0 commit comments

Comments
 (0)