Tweak configGetInt() to read hex strings properly (#35)

This commit is contained in:
Wipe 2022-06-07 23:00:19 +02:00 committed by GitHub
parent 2c05b8683d
commit 893dc0e090
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 4 deletions

View File

@ -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;
} }

View File

@ -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);