Skip to content

Commit 3f376a0

Browse files
sapierkwolekr
sapier
authored andcommittedFeb 5, 2014
Fix settings to honor numeric conversion errors
Rename try* non exceptioning functions to *NoEx
1 parent 2927a32 commit 3f376a0

8 files changed

+79
-43
lines changed
 

‎src/emerge.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,15 @@ EmergeManager::EmergeManager(IGameDef *gamedef) {
102102
// if unspecified, leave a proc for the main thread and one for
103103
// some other misc thread
104104
int nthreads = 0;
105-
if (!g_settings->tryGetS16("num_emerge_threads", nthreads))
105+
if (!g_settings->getS16NoEx("num_emerge_threads", nthreads))
106106
nthreads = porting::getNumberOfProcessors() - 2;
107107
if (nthreads < 1)
108108
nthreads = 1;
109109

110110
qlimit_total = g_settings->getU16("emergequeue_limit_total");
111-
if (!g_settings->tryGetU16("emergequeue_limit_diskonly", qlimit_diskonly))
111+
if (!g_settings->getU16NoEx("emergequeue_limit_diskonly", qlimit_diskonly))
112112
qlimit_diskonly = nthreads * 5 + 1;
113-
if (!g_settings->tryGetU16("emergequeue_limit_generate", qlimit_generate))
113+
if (!g_settings->getU16NoEx("emergequeue_limit_generate", qlimit_generate))
114114
qlimit_generate = nthreads + 1;
115115

116116
for (int i = 0; i != nthreads; i++)
@@ -352,13 +352,13 @@ void EmergeManager::loadParamsFromSettings(Settings *settings) {
352352
std::string seed_str;
353353
const char *setname = (settings == g_settings) ? "fixed_map_seed" : "seed";
354354

355-
if (settings->tryGet(setname, seed_str))
355+
if (settings->getNoEx(setname, seed_str))
356356
params.seed = read_seed(seed_str.c_str());
357357

358-
settings->tryGet("mg_name", params.mg_name);
359-
settings->tryGetS16("water_level", params.water_level);
360-
settings->tryGetS16("chunksize", params.chunksize);
361-
settings->tryGetFlagStr("mg_flags", params.flags, flagdesc_mapgen);
358+
settings->getNoEx("mg_name", params.mg_name);
359+
settings->getS16NoEx("water_level", params.water_level);
360+
settings->getS16NoEx("chunksize", params.chunksize);
361+
settings->getFlagStrNoEx("mg_flags", params.flags, flagdesc_mapgen);
362362

363363
delete params.sparams;
364364
params.sparams = createMapgenParams(params.mg_name);

‎src/exceptions.h

+5
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ class SettingNotFoundException : public BaseException {
8585
SettingNotFoundException(std::string s): BaseException(s) {}
8686
};
8787

88+
class NumericException : public BaseException {
89+
public:
90+
NumericException(std::string s): BaseException(s) {}
91+
};
92+
8893
class InvalidFilenameException : public BaseException {
8994
public:
9095
InvalidFilenameException(std::string s): BaseException(s) {}

‎src/main.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -1671,9 +1671,10 @@ int main(int argc, char *argv[])
16711671
//infostream<<"Main: password hash: '"<<password<<"'"<<std::endl;
16721672

16731673
address = menudata.address;
1674-
int newport = stoi(menudata.port);
1675-
if(newport != 0)
1676-
port = newport;
1674+
try {
1675+
port = stoi(menudata.port);
1676+
}
1677+
catch (NumericException&e) {}
16771678

16781679
simple_singleplayer_mode = menudata.simple_singleplayer_mode;
16791680

‎src/mapgen_indev.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ MapgenIndevParams::MapgenIndevParams() {
164164
void MapgenIndevParams::readParams(Settings *settings) {
165165
MapgenV6Params::readParams(settings);
166166

167-
settings->tryGetS16("mgindev_float_islands", float_islands);
167+
settings->getS16NoEx("mgindev_float_islands", float_islands);
168168

169169
settings->getNoiseIndevParams("mgindev_np_terrain_base", npindev_terrain_base);
170170
settings->getNoiseIndevParams("mgindev_np_terrain_higher", npindev_terrain_higher);

‎src/mapgen_v6.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ MapgenV6Params::MapgenV6Params() {
112112

113113

114114
void MapgenV6Params::readParams(Settings *settings) {
115-
settings->tryGetFlagStr("mgv6_spflags", spflags, flagdesc_mapgen_v6);
116-
settings->tryGetFloat("mgv6_freq_desert", freq_desert);
117-
settings->tryGetFloat("mgv6_freq_beach", freq_beach);
115+
settings->getFlagStrNoEx("mgv6_spflags", spflags, flagdesc_mapgen_v6);
116+
settings->getFloatNoEx("mgv6_freq_desert", freq_desert);
117+
settings->getFloatNoEx("mgv6_freq_beach", freq_beach);
118118

119119
settings->getNoiseParams("mgv6_np_terrain_base", np_terrain_base);
120120
settings->getNoiseParams("mgv6_np_terrain_higher", np_terrain_higher);

‎src/mapgen_v7.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ MapgenV7Params::MapgenV7Params() {
126126

127127

128128
void MapgenV7Params::readParams(Settings *settings) {
129-
settings->tryGetFlagStr("mgv7_spflags", spflags, flagdesc_mapgen_v7);
129+
settings->getFlagStrNoEx("mgv7_spflags", spflags, flagdesc_mapgen_v7);
130130

131131
settings->getNoiseParams("mgv7_np_terrain_base", np_terrain_base);
132132
settings->getNoiseParams("mgv7_np_terrain_alt", np_terrain_alt);

‎src/settings.h

+31-11
Original file line numberDiff line numberDiff line change
@@ -745,17 +745,19 @@ class Settings
745745
}
746746

747747
//////////// Try to get value, no exception thrown
748-
bool tryGet(std::string name, std::string &val)
748+
bool getNoEx(std::string name, std::string &val)
749749
{
750750
try {
751751
val = get(name);
752752
return true;
753753
} catch (SettingNotFoundException &e) {
754754
return false;
755+
} catch (NumericException &e) {
756+
return false;
755757
}
756758
}
757759

758-
bool tryGetFlagStr(std::string name, u32 &val, FlagDesc *flagdesc)
760+
bool getFlagStrNoEx(std::string name, u32 &val, FlagDesc *flagdesc)
759761
{
760762
try {
761763
val = getFlagStr(name, flagdesc);
@@ -765,93 +767,111 @@ class Settings
765767
}
766768
}
767769

768-
bool tryGetFloat(std::string name, float &val)
770+
bool getFloatNoEx(std::string name, float &val)
769771
{
770772
try {
771773
val = getFloat(name);
772774
return true;
773775
} catch (SettingNotFoundException &e) {
774776
return false;
777+
} catch (NumericException &e) {
778+
return false;
775779
}
776780
}
777781

778-
bool tryGetU16(std::string name, int &val)
782+
bool getU16NoEx(std::string name, int &val)
779783
{
780784
try {
781785
val = getU16(name);
782786
return true;
783787
} catch (SettingNotFoundException &e) {
784788
return false;
789+
} catch (NumericException &e) {
790+
return false;
785791
}
786792
}
787793

788-
bool tryGetU16(std::string name, u16 &val)
794+
bool getU16NoEx(std::string name, u16 &val)
789795
{
790796
try {
791797
val = getU16(name);
792798
return true;
793799
} catch (SettingNotFoundException &e) {
794800
return false;
801+
} catch (NumericException &e) {
802+
return false;
795803
}
796804
}
797805

798-
bool tryGetS16(std::string name, int &val)
806+
bool getS16NoEx(std::string name, int &val)
799807
{
800808
try {
801809
val = getU16(name);
802810
return true;
803811
} catch (SettingNotFoundException &e) {
804812
return false;
813+
} catch (NumericException &e) {
814+
return false;
805815
}
806816
}
807817

808-
bool tryGetS16(std::string name, s16 &val)
818+
bool getS16NoEx(std::string name, s16 &val)
809819
{
810820
try {
811821
val = getS16(name);
812822
return true;
813823
} catch (SettingNotFoundException &e) {
814824
return false;
825+
} catch (NumericException &e) {
826+
return false;
815827
}
816828
}
817829

818-
bool tryGetS32(std::string name, s32 &val)
830+
bool getS32NoEx(std::string name, s32 &val)
819831
{
820832
try {
821833
val = getS32(name);
822834
return true;
823835
} catch (SettingNotFoundException &e) {
824836
return false;
837+
} catch (NumericException &e) {
838+
return false;
825839
}
826840
}
827841

828-
bool tryGetV3F(std::string name, v3f &val)
842+
bool getV3FNoEx(std::string name, v3f &val)
829843
{
830844
try {
831845
val = getV3F(name);
832846
return true;
833847
} catch (SettingNotFoundException &e) {
834848
return false;
849+
} catch (NumericException &e) {
850+
return false;
835851
}
836852
}
837853

838-
bool tryGetV2F(std::string name, v2f &val)
854+
bool getV2FNoEx(std::string name, v2f &val)
839855
{
840856
try {
841857
val = getV2F(name);
842858
return true;
843859
} catch (SettingNotFoundException &e) {
844860
return false;
861+
} catch (NumericException &e) {
862+
return false;
845863
}
846864
}
847865

848-
bool tryGetU64(std::string name, u64 &val)
866+
bool getU64NoEx(std::string name, u64 &val)
849867
{
850868
try {
851869
val = getU64(name);
852870
return true;
853871
} catch (SettingNotFoundException &e) {
854872
return false;
873+
} catch (NumericException &e) {
874+
return false;
855875
}
856876
}
857877

‎src/util/string.h

+26-16
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2626
#include <cstring>
2727
#include <vector>
2828
#include <sstream>
29+
#include "exceptions.h"
2930

3031
struct FlagDesc {
3132
const char *name;
@@ -145,17 +146,31 @@ inline std::string trim(const std::string &s)
145146
return s.substr(front, back - front);
146147
}
147148

149+
inline s32 mystoi(const std::string &s)
150+
{
151+
char* endptr = NULL;
152+
s32 retval = strtol(s.c_str(),&endptr,10);
153+
154+
if ((endptr == NULL) || (*endptr != 0) || (endptr == s.c_str()))
155+
throw NumericException("string to convert");
156+
157+
return retval;
158+
}
159+
148160
inline bool is_yes(const std::string &s)
149161
{
150162
std::string s2 = lowercase(trim(s));
151-
if(s2 == "y" || s2 == "yes" || s2 == "true" || atoi(s2.c_str()) != 0)
152-
return true;
163+
try {
164+
if(s2 == "y" || s2 == "yes" || s2 == "true" || mystoi(s2) != 0)
165+
return true;
166+
}
167+
catch(NumericException&e) {}
153168
return false;
154169
}
155170

156171
inline s32 mystoi(const std::string &s, s32 min, s32 max)
157172
{
158-
s32 i = atoi(s.c_str());
173+
s32 i = mystoi(s);
159174
if(i < min)
160175
i = min;
161176
if(i > max)
@@ -173,25 +188,20 @@ inline s64 stoi64(const std::string &s) {
173188
// MSVC2010 includes it's own versions of these
174189
//#if !defined(_MSC_VER) || _MSC_VER < 1600
175190

176-
inline s32 mystoi(const std::string &s)
177-
{
178-
return atoi(s.c_str());
179-
}
180-
181191
inline s32 mystoi(const std::wstring &s)
182192
{
183-
return atoi(wide_to_narrow(s).c_str());
193+
return mystoi(wide_to_narrow(s).c_str());
184194
}
185195

186196
inline float mystof(const std::string &s)
187197
{
188-
// This crap causes a segfault in certain cases on MinGW
189-
/*float f;
190-
std::istringstream ss(s);
191-
ss>>f;
192-
return f;*/
193-
// This works in that case
194-
return atof(s.c_str());
198+
char* endptr = NULL;
199+
float retval = strtof(s.c_str(),&endptr);
200+
201+
if ((endptr == NULL) || (*endptr != 0) || (endptr == s.c_str()))
202+
throw NumericException("string to convert");
203+
204+
return retval;
195205
}
196206

197207
//#endif

0 commit comments

Comments
 (0)
Please sign in to comment.