Skip to content

Commit 92d5d80

Browse files
committed
fix: 修复 optbox 的 boolopt 和增加脚本文件大小限制;
1 parent c591400 commit 92d5d80

6 files changed

Lines changed: 113 additions & 16 deletions

File tree

src/class/aspreprocesser.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -942,14 +942,35 @@ void AsPreprocesser::clearAll() {
942942
}
943943

944944
int AsPreprocesser::loadScriptSection(const QString &filename) {
945-
// Open the script file
945+
if (modifiedScripts.size() >= 100) {
946+
engine->WriteMessage(filename.toUtf8(), 0, 0, asMSGTYPE_ERROR,
947+
"Too many included files");
948+
return -1;
949+
}
950+
951+
QFileInfo finfo(filename);
946952
QFile f(filename);
953+
if (!finfo.isFile() || f.isSequential()) {
954+
QString msg = QStringLiteral("Failed to open file '") +
955+
finfo.absoluteFilePath() + QStringLiteral("'");
956+
engine->WriteMessage(filename.toUtf8(), 0, 0, asMSGTYPE_ERROR,
957+
msg.toUtf8());
958+
return -1;
959+
}
960+
961+
// Open the script file
962+
if (f.size() > 1024 * 1024) {
963+
QString msg = QStringLiteral("Failed to open huge script file '") +
964+
finfo.absoluteFilePath() + QStringLiteral("'");
965+
engine->WriteMessage(filename.toUtf8(), 0, 0, asMSGTYPE_ERROR,
966+
msg.toUtf8());
967+
return -1;
968+
}
947969

948970
if (!f.open(QFile::ReadOnly)) {
949971
// Write a message to the engine's message callback
950972
QString msg = QStringLiteral("Failed to open script file '") +
951-
QFileInfo(filename).absoluteFilePath() +
952-
QStringLiteral("'");
973+
finfo.absoluteFilePath() + QStringLiteral("'");
953974
engine->WriteMessage(filename.toUtf8(), 0, 0, asMSGTYPE_ERROR,
954975
msg.toUtf8());
955976
return -1;

src/class/fmtlibext.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,15 @@ struct formatter<CScriptDictionaryView> {
439439
void *val_ptr;
440440
int val_type;
441441
};
442+
443+
auto size = dict->GetSize();
444+
if (size == 0) {
445+
out = fmt::format_to(out, "{{}}");
446+
return out;
447+
}
448+
442449
std::vector<E> entries;
443-
entries.reserve(dict->GetSize());
450+
entries.reserve(size);
444451

445452
for (auto it = dict->begin(); it != dict->end(); ++it) {
446453
auto key = it.GetKey();

src/control/scripteditor.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include "class/angellsp.h"
3434
#include "class/ascompletion.h"
3535

36+
constexpr auto SCRIPT_LIMIT = 1024 * 1024;
37+
3638
ScriptEditor::ScriptEditor(QWidget *parent)
3739
: ads::CDockWidget(nullptr, QString(), parent) {
3840
this->setFeatures(
@@ -102,6 +104,10 @@ bool ScriptEditor::openFile(const QString &filename) {
102104
}
103105

104106
QFile f(filename);
107+
if (f.size() > SCRIPT_LIMIT) {
108+
return false;
109+
}
110+
105111
if (!f.open(QFile::ReadOnly | QFile::Text)) {
106112
return false;
107113
}
@@ -148,7 +154,12 @@ bool ScriptEditor::save(const QString &path) {
148154
if (!f.open(QFile::WriteOnly | QFile::Text)) {
149155
return false;
150156
}
151-
f.write(m_editor->toPlainText().toUtf8());
157+
158+
auto data = m_editor->toPlainText().toUtf8();
159+
if (data.size() > SCRIPT_LIMIT) {
160+
return false;
161+
}
162+
f.write(data);
152163
doc->setModified(false);
153164
}
154165
return true;
@@ -158,7 +169,11 @@ bool ScriptEditor::save(const QString &path) {
158169
if (!f.open(QFile::WriteOnly | QFile::Text)) {
159170
return false;
160171
}
161-
f.write(m_editor->toPlainText().toUtf8());
172+
auto data = m_editor->toPlainText().toUtf8();
173+
if (data.size() > SCRIPT_LIMIT) {
174+
return false;
175+
}
176+
f.write(data);
162177

163178
m_editor->setWindowFilePath(path);
164179
processTitle();

src/dialog/framelessdialogbase.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ void FramelessDialogBase::buildUpContent(QWidget *content) {
7777
#ifdef QT_DEBUG
7878
m_isBuilt = true;
7979
#endif
80+
81+
adjustSize();
8082
}
8183

8284
void FramelessDialogBase::showEvent(QShowEvent *event) {

src/scriptaddon/scriptoptbox.cpp

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,11 @@ class OptBox {
163163
dic->Set(w->objectName(), &b, asTYPEID_BOOL);
164164
} break;
165165
case ControlType::BoolOptItems: {
166-
bool b = w->property(PROPERTY_VALUE).toBool();
167-
dic->Set(w->objectName(), &b, asTYPEID_BOOL);
166+
QString v = w->property(PROPERTY_VALUE).toString();
167+
auto engine = ctx->GetEngine();
168+
auto type = static_cast<asITypeInfo *>(engine->GetUserData(
169+
AsUserDataType::UserData_StringTypeInfo));
170+
dic->Set(w->objectName(), &v, type->GetTypeId());
168171
} break;
169172
case ControlType::Int: {
170173
int v = w->property("value").toInt();
@@ -418,55 +421,88 @@ class OptBox {
418421
auto layout = new QHBoxLayout(w);
419422
layout->setParent(w);
420423
auto data = obj.value("data").toArray();
424+
QStringList usedNames;
425+
usedNames.reserve(data.size());
421426
for (auto p = data.begin(); p != data.end(); p++) {
422427
if (!p->isObject()) {
423428
qWarning("[optbox::createWidget] Invalid data for 'bools'");
424429
continue;
425430
}
426431
auto obj = p->toObject();
427-
auto cb = new QCheckBox;
432+
auto cb = new QCheckBox(w);
428433
cb->setText(obj.value("text").toString());
429434
auto dv = obj.value("value").toBool();
430435
cb->setProperty(PROPERTY_DEFAULT, dv);
431-
cb->setObjectName(obj.value("name").toString());
436+
auto name = obj.value("name").toString();
437+
if (name.isEmpty()) {
438+
qWarning("[optbox::createWidget] Cannot create widget "
439+
"without a name.");
440+
return {};
441+
}
442+
if (_usedObjNames.contains(name) || usedNames.contains(name)) {
443+
qWarning("[optbox::createWidget] Cannot create widget with "
444+
"a used name.");
445+
return {};
446+
}
447+
usedNames.append(name);
448+
cb->setObjectName(name);
432449
cb->setToolTip(obj.value("tooltip").toString());
433450
cb->setChecked(dv);
434451
_data.insert(cb, ControlType::Bool);
435452
layout->addWidget(cb);
436453
}
454+
_usedObjNames.append(usedNames);
437455
ret.widget = w;
438456
} break;
439457
case ControlType::BoolOptItems: {
440458
auto w = new QWidget;
441-
auto layout = QHBoxLayout(w);
459+
auto layout = new QHBoxLayout(w);
460+
layout->setParent(w);
442461
auto dv = obj.value("value").toString();
443-
w->setProperty(PROPERTY_DEFAULT, dv);
444462
auto group = QButtonGroup(w);
445463
group.setExclusive(true);
446464
auto data = obj.value("data").toArray();
465+
QStringList usedNames;
466+
usedNames.reserve(data.size());
447467
for (auto p = data.begin(); p != data.end(); p++) {
448468
if (!p->isObject()) {
449469
qWarning(
450470
"[optbox::createWidget] Invalid data for 'boolopt'");
451471
continue;
452472
}
453473
auto obj = p->toObject();
454-
auto rb = new QRadioButton;
474+
auto rb = new QRadioButton(w);
455475
rb->setText(obj.value("text").toString());
456476
auto name = obj.value("name").toString();
477+
if (name.isEmpty()) {
478+
qWarning("[optbox::createWidget] Cannot create widget "
479+
"without a name.");
480+
return {};
481+
}
482+
if (usedNames.contains(name)) {
483+
qWarning("[optbox::createWidget] Cannot create widget with "
484+
"a used name.");
485+
return {};
486+
}
487+
usedNames.append(name);
488+
if (dv.isEmpty()) {
489+
dv = name;
490+
}
457491
rb->setObjectName(name);
458492
rb->setToolTip(obj.value("tooltip").toString());
459-
if (name == dv) {
460-
rb->setChecked(true);
461-
}
462493
QObject::connect(
463494
rb, &QRadioButton::toggled, w, [rb, w](bool b) {
464495
if (b) {
465496
w->setProperty(PROPERTY_VALUE, rb->objectName());
466497
}
467498
});
499+
if (name == dv) {
500+
rb->setChecked(true);
501+
}
502+
layout->addWidget(rb);
468503
group.addButton(rb);
469504
}
505+
w->setProperty(PROPERTY_DEFAULT, dv);
470506
ret.widget = w;
471507
_data.insert(w, type);
472508
} break;

test/optbox_json.as

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ int main() {
1515
obj.set("name", "leString");
1616
objs.append(obj);
1717

18+
obj.set("type", "boolopt");
19+
obj.set("title", "Testopt");
20+
obj.set("name", "optTestGroup");
21+
22+
json::array subarr;
23+
json::object subobj;
24+
subobj.set("name", "optA");
25+
subobj.set("text", "A");
26+
subarr.append(subobj);
27+
subobj.set("name", "optB");
28+
subobj.set("text", "B");
29+
subarr.append(subobj);
30+
obj.set("data", subarr);
31+
32+
objs.append(obj);
33+
1834
json::object entryobj;
1935
entryobj.set("type", "group");
2036
entryobj.set("title", "Test");

0 commit comments

Comments
 (0)