Skip to content

Commit d3dc88f

Browse files
committedDec 12, 2014
Settings: Fail on invalid sequence and throw exception for LuaSettings
1 parent b0c4fd6 commit d3dc88f

File tree

4 files changed

+89
-53
lines changed

4 files changed

+89
-53
lines changed
 

Diff for: ‎src/script/lua_api/l_settings.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ int LuaSettings::l_set(lua_State* L)
7373
std::string key = std::string(luaL_checkstring(L, 2));
7474
const char* value = luaL_checkstring(L, 3);
7575

76-
o->m_settings->set(key, value);
76+
if (!o->m_settings->set(key, value))
77+
throw LuaError("Invalid sequence found in setting parameters");
7778

78-
return 1;
79+
return 0;
7980
}
8081

8182
// remove(self, key) -> success

Diff for: ‎src/settings.cpp

+64-34
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,30 @@ Settings & Settings::operator = (const Settings &other)
6363
}
6464

6565

66+
bool Settings::checkNameValid(const std::string &name)
67+
{
68+
size_t pos = name.find_first_of("\t\n\v\f\r\b =\"{}#");
69+
if (pos != std::string::npos) {
70+
errorstream << "Invalid character '" << name[pos]
71+
<< "' found in setting name" << std::endl;
72+
return false;
73+
}
74+
return true;
75+
}
76+
77+
78+
bool Settings::checkValueValid(const std::string &value)
79+
{
80+
if (value.substr(0, 3) == "\"\"\"" ||
81+
value.find("\n\"\"\"") != std::string::npos) {
82+
errorstream << "Invalid character sequence '\"\"\"' found in"
83+
" setting value" << std::endl;
84+
return false;
85+
}
86+
return true;
87+
}
88+
89+
6690
std::string Settings::sanitizeName(const std::string &name)
6791
{
6892
std::string n(name);
@@ -704,112 +728,119 @@ bool Settings::getFlagStrNoEx(const std::string &name, u32 &val,
704728
* Setters *
705729
***********/
706730

707-
void Settings::setEntry(const std::string &name, const void *data,
731+
bool Settings::setEntry(const std::string &name, const void *data,
708732
bool set_group, bool set_default)
709733
{
710734
Settings *old_group = NULL;
711735

712-
std::string n = sanitizeName(name);
736+
if (!checkNameValid(name))
737+
return false;
738+
if (!set_group && !checkValueValid(*(const std::string *)data))
739+
return false;
713740

714741
{
715742
JMutexAutoLock lock(m_mutex);
716743

717-
SettingsEntry &entry = set_default ? m_defaults[n] : m_settings[n];
744+
SettingsEntry &entry = set_default ? m_defaults[name] : m_settings[name];
718745
old_group = entry.group;
719746

720-
entry.value = set_group ? "" : sanitizeValue(*(const std::string *)data);
747+
entry.value = set_group ? "" : *(const std::string *)data;
721748
entry.group = set_group ? *(Settings **)data : NULL;
722749
entry.is_group = set_group;
723750
}
724751

725752
delete old_group;
753+
754+
return true;
726755
}
727756

728757

729-
void Settings::set(const std::string &name, const std::string &value)
758+
bool Settings::set(const std::string &name, const std::string &value)
730759
{
731-
setEntry(name, &value, false, false);
760+
if (!setEntry(name, &value, false, false))
761+
return false;
732762

733763
doCallbacks(name);
764+
return true;
734765
}
735766

736767

737-
void Settings::setDefault(const std::string &name, const std::string &value)
768+
bool Settings::setDefault(const std::string &name, const std::string &value)
738769
{
739-
setEntry(name, &value, false, true);
770+
return setEntry(name, &value, false, true);
740771
}
741772

742773

743-
void Settings::setGroup(const std::string &name, Settings *group)
774+
bool Settings::setGroup(const std::string &name, Settings *group)
744775
{
745-
setEntry(name, &group, true, false);
776+
return setEntry(name, &group, true, false);
746777
}
747778

748779

749-
void Settings::setGroupDefault(const std::string &name, Settings *group)
780+
bool Settings::setGroupDefault(const std::string &name, Settings *group)
750781
{
751-
setEntry(name, &group, true, true);
782+
return setEntry(name, &group, true, true);
752783
}
753784

754785

755-
void Settings::setBool(const std::string &name, bool value)
786+
bool Settings::setBool(const std::string &name, bool value)
756787
{
757-
set(name, value ? "true" : "false");
788+
return set(name, value ? "true" : "false");
758789
}
759790

760791

761-
void Settings::setS16(const std::string &name, s16 value)
792+
bool Settings::setS16(const std::string &name, s16 value)
762793
{
763-
set(name, itos(value));
794+
return set(name, itos(value));
764795
}
765796

766797

767-
void Settings::setU16(const std::string &name, u16 value)
798+
bool Settings::setU16(const std::string &name, u16 value)
768799
{
769-
set(name, itos(value));
800+
return set(name, itos(value));
770801
}
771802

772803

773-
void Settings::setS32(const std::string &name, s32 value)
804+
bool Settings::setS32(const std::string &name, s32 value)
774805
{
775-
set(name, itos(value));
806+
return set(name, itos(value));
776807
}
777808

778809

779-
void Settings::setU64(const std::string &name, u64 value)
810+
bool Settings::setU64(const std::string &name, u64 value)
780811
{
781812
std::ostringstream os;
782813
os << value;
783-
set(name, os.str());
814+
return set(name, os.str());
784815
}
785816

786817

787-
void Settings::setFloat(const std::string &name, float value)
818+
bool Settings::setFloat(const std::string &name, float value)
788819
{
789-
set(name, ftos(value));
820+
return set(name, ftos(value));
790821
}
791822

792823

793-
void Settings::setV2F(const std::string &name, v2f value)
824+
bool Settings::setV2F(const std::string &name, v2f value)
794825
{
795826
std::ostringstream os;
796827
os << "(" << value.X << "," << value.Y << ")";
797-
set(name, os.str());
828+
return set(name, os.str());
798829
}
799830

800831

801-
void Settings::setV3F(const std::string &name, v3f value)
832+
bool Settings::setV3F(const std::string &name, v3f value)
802833
{
803834
std::ostringstream os;
804835
os << "(" << value.X << "," << value.Y << "," << value.Z << ")";
805-
set(name, os.str());
836+
return set(name, os.str());
806837
}
807838

808839

809-
void Settings::setFlagStr(const std::string &name, u32 flags,
840+
bool Settings::setFlagStr(const std::string &name, u32 flags,
810841
const FlagDesc *flagdesc, u32 flagmask)
811842
{
812-
set(name, writeFlagString(flags, flagdesc, flagmask));
843+
return set(name, writeFlagString(flags, flagdesc, flagmask));
813844
}
814845

815846

@@ -820,12 +851,11 @@ bool Settings::setStruct(const std::string &name, const std::string &format,
820851
if (!serializeStructToString(&structstr, format, value))
821852
return false;
822853

823-
set(name, structstr);
824-
return true;
854+
return set(name, structstr);
825855
}
826856

827857

828-
void Settings::setNoiseParams(const std::string &name,
858+
bool Settings::setNoiseParams(const std::string &name,
829859
const NoiseParams &np, bool set_default)
830860
{
831861
Settings *group = new Settings;
@@ -839,7 +869,7 @@ void Settings::setNoiseParams(const std::string &name,
839869
group->setFloat("lacunarity", np.lacunarity);
840870
group->setFlagStr("flags", np.flags, flagdesc_noiseparams, np.flags);
841871

842-
setEntry(name, &group, true, set_default);
872+
return setEntry(name, &group, true, set_default);
843873
}
844874

845875

Diff for: ‎src/settings.h

+17-15
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ class Settings {
112112
bool updateConfigObject(std::istream &is, std::ostream &os,
113113
const std::string &end, u32 tab_depth=0);
114114

115+
static bool checkNameValid(const std::string &name);
116+
static bool checkValueValid(const std::string &value);
115117
static std::string sanitizeName(const std::string &name);
116118
static std::string sanitizeValue(const std::string &value);
117119
static std::string getMultiline(std::istream &is, size_t *num_lines=NULL);
@@ -175,23 +177,23 @@ class Settings {
175177

176178
// N.B. Groups not allocated with new must be set to NULL in the settings
177179
// tree before object destruction.
178-
void setEntry(const std::string &name, const void *entry,
180+
bool setEntry(const std::string &name, const void *entry,
179181
bool set_group, bool set_default);
180-
void set(const std::string &name, const std::string &value);
181-
void setDefault(const std::string &name, const std::string &value);
182-
void setGroup(const std::string &name, Settings *group);
183-
void setGroupDefault(const std::string &name, Settings *group);
184-
void setBool(const std::string &name, bool value);
185-
void setS16(const std::string &name, s16 value);
186-
void setU16(const std::string &name, u16 value);
187-
void setS32(const std::string &name, s32 value);
188-
void setU64(const std::string &name, u64 value);
189-
void setFloat(const std::string &name, float value);
190-
void setV2F(const std::string &name, v2f value);
191-
void setV3F(const std::string &name, v3f value);
192-
void setFlagStr(const std::string &name, u32 flags,
182+
bool set(const std::string &name, const std::string &value);
183+
bool setDefault(const std::string &name, const std::string &value);
184+
bool setGroup(const std::string &name, Settings *group);
185+
bool setGroupDefault(const std::string &name, Settings *group);
186+
bool setBool(const std::string &name, bool value);
187+
bool setS16(const std::string &name, s16 value);
188+
bool setU16(const std::string &name, u16 value);
189+
bool setS32(const std::string &name, s32 value);
190+
bool setU64(const std::string &name, u64 value);
191+
bool setFloat(const std::string &name, float value);
192+
bool setV2F(const std::string &name, v2f value);
193+
bool setV3F(const std::string &name, v3f value);
194+
bool setFlagStr(const std::string &name, u32 flags,
193195
const FlagDesc *flagdesc, u32 flagmask);
194-
void setNoiseParams(const std::string &name, const NoiseParams &np,
196+
bool setNoiseParams(const std::string &name, const NoiseParams &np,
195197
bool set_default=false);
196198
// N.B. if setStruct() is used to write a non-POD aggregate type,
197199
// the behavior is undefined.

Diff for: ‎src/test.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,12 @@ struct TestSettings: public TestBase
531531
group2->setS16("num_oranges", 53);
532532
group2->setGroup("animals", group3);
533533
group2->set("animals", "cute"); //destroys group 3
534+
s.setGroup("groupy_thing", group2);
534535

535-
// the bad chars in here should be stripped
536-
s.setGroup("groupy \"_\" thing", group2);
536+
// Test set failure conditions
537+
UASSERT(s.set("Zoop = Poop\nsome_other_setting", "false") == false);
538+
UASSERT(s.set("sneaky", "\"\"\"\njabberwocky = false") == false);
539+
UASSERT(s.set("hehe", "asdfasdf\n\"\"\"\nsomething = false") == false);
537540

538541
// Test multiline settings
539542
UASSERT(group->get("ccc") == "testy\n testa ");

0 commit comments

Comments
 (0)
Please sign in to comment.