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 "platform_compat.h"
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -178,8 +179,8 @@ bool configSetString(Config* config, const char* sectionKey, const char* key, co
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 0x42C05C
|
// 0x42C05C customized: atoi() replaced with strtol()
|
||||||
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 */ )
|
||||||
{
|
{
|
||||||
if (valuePtr == NULL) {
|
if (valuePtr == NULL) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -190,7 +191,13 @@ bool configGetInt(Config* config, const char* sectionKey, const char* key, int*
|
||||||
return false;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ void configFree(Config* config);
|
||||||
bool configParseCommandLineArguments(Config* config, int argc, char** argv);
|
bool configParseCommandLineArguments(Config* config, int argc, char** argv);
|
||||||
bool configGetString(Config* config, const char* sectionKey, const char* key, char** valuePtr);
|
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 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 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 configSetInt(Config* config, const char* sectionKey, const char* key, int value);
|
||||||
bool configRead(Config* config, const char* filePath, bool isDb);
|
bool configRead(Config* config, const char* filePath, bool isDb);
|
||||||
|
|
Loading…
Reference in New Issue