Tweak configGetInt() to read hex strings properly (#35)
This commit is contained in:
parent
2c05b8683d
commit
893dc0e090
|
@ -5,6 +5,7 @@
|
|||
#include "platform_compat.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
@ -178,8 +179,8 @@ bool configSetString(Config* config, const char* sectionKey, const char* key, co
|
|||
return true;
|
||||
}
|
||||
|
||||
// 0x42C05C
|
||||
bool configGetInt(Config* config, const char* sectionKey, const char* key, int* valuePtr)
|
||||
// 0x42C05C customized: atoi() replaced with strtol()
|
||||
bool configGetInt(Config* config, const char* sectionKey, const char* key, int* valuePtr, unsigned char base /* = 0 */ )
|
||||
{
|
||||
if (valuePtr == NULL) {
|
||||
return false;
|
||||
|
@ -190,7 +191,13 @@ bool configGetInt(Config* config, const char* sectionKey, const char* key, int*
|
|||
return false;
|
||||
}
|
||||
|
||||
*valuePtr = atoi(stringValue);
|
||||
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;
|
||||
|
||||
*valuePtr = l;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ void configFree(Config* config);
|
|||
bool configParseCommandLineArguments(Config* config, int argc, char** argv);
|
||||
bool configGetString(Config* config, const char* sectionKey, const char* key, char** valuePtr);
|
||||
bool configSetString(Config* config, const char* sectionKey, const char* key, const char* value);
|
||||
bool configGetInt(Config* config, const char* sectionKey, const char* key, int* valuePtr);
|
||||
bool configGetInt(Config* config, const char* sectionKey, const char* key, int* valuePtr, unsigned char base = 0);
|
||||
bool configGetIntList(Config* config, const char* section, const char* key, int* arr, int count);
|
||||
bool configSetInt(Config* config, const char* sectionKey, const char* key, int value);
|
||||
bool configRead(Config* config, const char* filePath, bool isDb);
|
||||
|
|
Loading…
Reference in New Issue