From cbdb0eeba4aaeafcf5a3676679427fe07ec27469 Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Wed, 27 Jul 2022 10:24:45 +0300 Subject: [PATCH] Make configGetInt compatible with atoi Fixes #94 --- src/config.cc | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/config.cc b/src/config.cc index bbd2370..99aea57 100644 --- a/src/config.cc +++ b/src/config.cc @@ -188,7 +188,7 @@ bool configSetString(Config* config, const char* sectionKey, const char* key, co return true; } -// 0x42C05C customized: atoi() replaced with strtol() +// 0x42C05C bool configGetInt(Config* config, const char* sectionKey, const char* key, int* valuePtr, unsigned char base /* = 0 */ ) { if (valuePtr == NULL) { @@ -203,8 +203,17 @@ bool configGetInt(Config* config, const char* sectionKey, const char* key, int* char* end; errno = 0; long l = strtol(stringValue, &end, base); // see https://stackoverflow.com/a/6154614 - if (((errno == ERANGE && 1 == LONG_MAX) || l > INT_MAX) || ((errno == ERANGE && l == LONG_MIN) || l < INT_MIN) || (*stringValue == '\0' || *end != '\0')) - return false; + + // The link above says right things about converting strings to numbers, + // however we need to maintain compatibility with atoi implementation and + // original game data. One example of the problem is worldmap.txt where + // frequency values expressed as percentages (Frequent=38%). If we handle + // the result like the link above suggests (and what previous implementation + // provided), we'll simply end up returning `false`, since there will be + // unconverted characters left. On the other hand, this function is also + // used to parse Sfall config values, which uses hexadecimal notation to + // represent colors. We're not going to need any of these in the long run so + // for now simply ignore any error that could arise during conversion. *valuePtr = l;