From 037065910fa5fca39cb1321db8de9c8fb93dde89 Mon Sep 17 00:00:00 2001 From: Misa Date: Thu, 6 Aug 2020 21:28:51 -0700 Subject: [PATCH] Add UtilityClass::Int() This is simply a wrapper function around SDL_atoi(), because SDL_atoi() could call libc atoi(), and whether or not invalid input passed into the libc atoi() is undefined behavior depends on the given libc. So it's safer to just add a wrapper function that checks that the string given isn't bogus. --- desktop_version/src/UtilityClass.cpp | 10 ++++++++++ desktop_version/src/UtilityClass.h | 2 ++ 2 files changed, 12 insertions(+) diff --git a/desktop_version/src/UtilityClass.cpp b/desktop_version/src/UtilityClass.cpp index 8d27de2a..7b6695ad 100644 --- a/desktop_version/src/UtilityClass.cpp +++ b/desktop_version/src/UtilityClass.cpp @@ -99,6 +99,16 @@ std::string UtilityClass::String( int _v ) return(os.str()); } +int UtilityClass::Int(const char* str) +{ + if (!is_number(str)) + { + return 0; + } + + return SDL_atoi(str); +} + std::string UtilityClass::GCString(std::vector buttons) { std::string retval = ""; diff --git a/desktop_version/src/UtilityClass.h b/desktop_version/src/UtilityClass.h index 923f8d64..d5eed8d6 100644 --- a/desktop_version/src/UtilityClass.h +++ b/desktop_version/src/UtilityClass.h @@ -37,6 +37,8 @@ public: static std::string String(int _v); + static int Int(const char* str); + static std::string GCString(std::vector buttons); std::string twodigits(int t);