C client: Relying on undefined behaviour is a bad idea

This commit is contained in:
UnknownShadow200 2018-09-04 10:33:23 +10:00
parent 6fcb90e03f
commit 7b93ad44fe
4 changed files with 25 additions and 23 deletions

View File

@ -140,13 +140,11 @@ bool KeyBind_IsPressed(KeyBind binding) { return Key_States[KeyBind_Keys[binding
void KeyBind_Load(void) { void KeyBind_Load(void) {
Int32 i; Int32 i;
char nameBuffer[STRING_SIZE]; for (i = 0; i < KeyBind_Count; i++) {
char nameBuffer[STRING_SIZE] = { 0 };
String name = String_FromArray(nameBuffer); String name = String_FromArray(nameBuffer);
for (i = 0; i < KeyBind_Count; i++) {
name.length = 0;
String_Format1(&name, "key-%c", KeyBind_Names[i]); String_Format1(&name, "key-%c", KeyBind_Names[i]);
Key mapping = Options_GetEnum(name.buffer, KeyBind_Defaults[i], Key_Names, Key_Count); Key mapping = Options_GetEnum(name.buffer, KeyBind_Defaults[i], Key_Names, Key_Count);
if (mapping != Key_Escape) KeyBind_Keys[i] = mapping; if (mapping != Key_Escape) KeyBind_Keys[i] = mapping;
} }
@ -157,13 +155,12 @@ void KeyBind_Save(void) {
Int32 i; Int32 i;
char nameBuffer[STRING_SIZE]; char nameBuffer[STRING_SIZE];
String name = String_FromArray(nameBuffer); String name = String_FromArray(nameBuffer);
for (i = 0; i < KeyBind_Count; i++) { for (i = 0; i < KeyBind_Count; i++) {
name.length = 0; name.length = 0;
String_Format1(&name, "key-%c", KeyBind_Names[i]);
String_Format1(&name, "key-%c", KeyBind_Names[i]);
String value = String_FromReadonly(Key_Names[KeyBind_Keys[i]]); String value = String_FromReadonly(Key_Names[KeyBind_Keys[i]]);
Options_Set(name.buffer, &value); Options_SetString(&name, &value);
} }
} }
@ -328,7 +325,7 @@ void Hotkeys_UserRemovedHotkey(Key trigger, UInt8 flags) {
String key = String_FromArray(keyBuffer); String key = String_FromArray(keyBuffer);
String_Format2(&key, "hotkey-%c&%b", Key_Names[trigger], &flags); String_Format2(&key, "hotkey-%c&%b", Key_Names[trigger], &flags);
Options_Set(key.buffer, NULL); Options_SetString(&key, NULL);
} }
void Hotkeys_UserAddedHotkey(Key trigger, UInt8 flags, bool moreInput, STRING_PURE String* text) { void Hotkeys_UserAddedHotkey(Key trigger, UInt8 flags, bool moreInput, STRING_PURE String* text) {
@ -339,5 +336,5 @@ void Hotkeys_UserAddedHotkey(Key trigger, UInt8 flags, bool moreInput, STRING_PU
String_Format2(&key, "hotkey-%c&%b", Key_Names[trigger], &flags); String_Format2(&key, "hotkey-%c&%b", Key_Names[trigger], &flags);
String_Format2(&value, "%t&%s", &moreInput, text); String_Format2(&value, "%t&%s", &moreInput, text);
Options_Set(key.buffer, &value); Options_SetString(&key, &value);
} }

View File

@ -138,6 +138,10 @@ void Options_SetInt(const char* keyRaw, Int32 value) {
void Options_Set(const char* keyRaw, STRING_PURE String* value) { void Options_Set(const char* keyRaw, STRING_PURE String* value) {
String key = String_FromReadonly(keyRaw); String key = String_FromReadonly(keyRaw);
Options_SetString(&key, value);
}
void Options_SetString(STRING_PURE String* key, STRING_PURE String* value) {
UInt32 i; UInt32 i;
if (value == NULL || value->buffer == NULL) { if (value == NULL || value->buffer == NULL) {
i = Options_Find(&key); i = Options_Find(&key);

View File

@ -73,18 +73,19 @@ extern const char* FpsLimit_Names[FpsLimit_Count];
StringsBuffer Options_Keys; StringsBuffer Options_Keys;
StringsBuffer Options_Values; StringsBuffer Options_Values;
bool Options_HasAnyChanged(void); NOINLINE_ bool Options_HasAnyChanged(void);
void Options_Free(void); NOINLINE_ void Options_Free(void);
void Options_Get(const char* key, STRING_TRANSIENT String* value, const char* defValue); NOINLINE_ void Options_Get(const char* key, STRING_TRANSIENT String* value, const char* defValue);
Int32 Options_GetInt(const char* key, Int32 min, Int32 max, Int32 defValue); NOINLINE_ Int32 Options_GetInt(const char* key, Int32 min, Int32 max, Int32 defValue);
bool Options_GetBool(const char* key, bool defValue); NOINLINE_ bool Options_GetBool(const char* key, bool defValue);
Real32 Options_GetFloat(const char* key, Real32 min, Real32 max, Real32 defValue); NOINLINE_ Real32 Options_GetFloat(const char* key, Real32 min, Real32 max, Real32 defValue);
UInt32 Options_GetEnum(const char* key, UInt32 defValue, const char** names, UInt32 namesCount); NOINLINE_ UInt32 Options_GetEnum(const char* key, UInt32 defValue, const char** names, UInt32 namesCount);
void Options_SetBool(const char* keyRaw, bool value); NOINLINE_ void Options_SetBool(const char* keyRaw, bool value);
void Options_SetInt(const char* keyRaw, Int32 value); NOINLINE_ void Options_SetInt(const char* keyRaw, Int32 value);
void Options_Set(const char* keyRaw, STRING_PURE String* value); NOINLINE_ void Options_Set(const char* keyRaw, STRING_PURE String* value);
void Options_Load(void); NOINLINE_ void Options_SetString(STRING_PURE String* key, STRING_PURE String* value);
void Options_Save(void); NOINLINE_ void Options_Load(void);
NOINLINE_ void Options_Save(void);
#endif #endif

View File

@ -73,7 +73,7 @@ ReturnCode ReturnCode_SocketWouldBlock = EWOULDBLOCK;
*---------------------------------------------------------Memory----------------------------------------------------------* *---------------------------------------------------------Memory----------------------------------------------------------*
*#########################################################################################################################*/ *#########################################################################################################################*/
static void Platform_AllocFailed(const char* place) { static void Platform_AllocFailed(const char* place) {
char logBuffer[STRING_SIZE + 20]; char logBuffer[STRING_SIZE + 20] = { 0 };
String log = String_FromArray(logBuffer); String log = String_FromArray(logBuffer);
String_Format1(&log, "Failed allocating memory for: %c", place); String_Format1(&log, "Failed allocating memory for: %c", place);
ErrorHandler_Fail(log.buffer); ErrorHandler_Fail(log.buffer);