From 994788ff1846b66a78235ad389289133042989e4 Mon Sep 17 00:00:00 2001 From: Dofes <91889957+Dofes@users.noreply.github.com> Date: Sat, 12 Oct 2024 22:23:29 +0800 Subject: [PATCH] fix: fix custom form empty dropdown crash --- src-server/ll/api/form/CustomForm.cpp | 18 ++++++++++++++++-- src-test/server/FormTest.cpp | 4 +++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src-server/ll/api/form/CustomForm.cpp b/src-server/ll/api/form/CustomForm.cpp index 7233eb96a7..453d0fc325 100644 --- a/src-server/ll/api/form/CustomForm.cpp +++ b/src-server/ll/api/form/CustomForm.cpp @@ -118,7 +118,14 @@ class Dropdown : public CustomFormElement { } [[nodiscard]] CustomFormElementResult parseResult(nlohmann::ordered_json const& data) const override { - return mOptions[data.get()]; + if (!data.is_number_integer()) { + return std::string{}; + } + int index = data.get(); + if (index < 0 || index >= static_cast(mOptions.size())) { + return std::string{}; + } + return mOptions[index]; } }; @@ -220,7 +227,14 @@ class StepSlider : public CustomFormElement { } [[nodiscard]] CustomFormElementResult parseResult(nlohmann::ordered_json const& data) const override { - return mSteps[data.get()]; + if (!data.is_number_integer()) { + return std::string{}; + } + int index = data.get(); + if (index < 0 || index >= static_cast(mSteps.size())) { + return std::string{}; + } + return mSteps[index]; } }; diff --git a/src-test/server/FormTest.cpp b/src-test/server/FormTest.cpp index 3986c28569..6a0719fbaa 100644 --- a/src-test/server/FormTest.cpp +++ b/src-test/server/FormTest.cpp @@ -31,7 +31,8 @@ void registerFormTestCommand() { [](CommandOrigin const& ori, CommandOutput&, TestFormParam const& param) { switch (param.type) { case TestFormParam::FormType::custom: { - ll::form::CustomForm form; + ll::form::CustomForm form; + std::vector names; form.setTitle("CustomForm") .appendLabel("label") .appendInput("input1", "input") @@ -39,6 +40,7 @@ void registerFormTestCommand() { .appendSlider("slider", "slider", 0, 100, 1) .appendStepSlider("stepSlider", "stepSlider", {"a", "b", "c"}) .appendDropdown("dropdown", "dropdown", {"a", "b", "c"}) + .appendDropdown("emptydropdown", "empty dropdown", names) .sendTo( *(Player*)ori.getEntity(), [](Player&, ll::form::CustomFormResult const& data, ll::form::FormCancelReason) {