Skip to content

Commit

Permalink
Fix possible invalid access to std::optional
Browse files Browse the repository at this point in the history
  • Loading branch information
wengxt committed Jun 1, 2024
1 parent 70f294e commit 6cc81dd
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/rimestate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,19 @@ void RimeState::selectSchema(const std::string &schema) {

void RimeState::keyEvent(KeyEvent &event) {
auto *ic = event.inputContext();
std::optional<std::string> compose;
// For key-release, composeResult will always be empty string, which feed
// into engine directly.
std::string composeResult;
if (!event.key().states().testAny(
KeyStates{KeyState::Ctrl, KeyState::Super}) &&
!event.isRelease()) {
compose =
auto compose =
engine_->instance()->processComposeString(&ic_, event.key().sym());
if (!compose) {
event.filterAndAccept();
return;
}
composeResult = *compose;
}

auto *api = engine_->api();
Expand All @@ -168,20 +171,20 @@ void RimeState::keyEvent(KeyEvent &event) {
// IBUS_RELEASE_MASK
intStates |= (1 << 30);
}
if (!compose->empty()) {
if (!composeResult.empty()) {
event.filterAndAccept();
auto length = utf8::lengthValidated(*compose);
auto length = utf8::lengthValidated(composeResult);
bool result = false;
if (length == 1) {
auto c = utf8::getChar(*compose);
auto c = utf8::getChar(composeResult);
auto sym = Key::keySymFromUnicode(c);
if (sym != FcitxKey_None) {
result = api->process_key(session, sym, intStates);
}
}
if (!result) {
commitPreedit(ic);
ic->commitString(*compose);
ic->commitString(composeResult);
clear();
}
} else {
Expand Down

0 comments on commit 6cc81dd

Please sign in to comment.