From a97a016381b36eebbe07ee2863fd9b108867cd18 Mon Sep 17 00:00:00 2001 From: "Wilson E. Alvarez" Date: Thu, 21 Oct 2021 12:46:28 -0400 Subject: [PATCH] Add error messages to String::bin_to_int64, and accept capital B in prefix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: RĂ©mi Verschelde --- core/ustring.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/core/ustring.cpp b/core/ustring.cpp index 2c294e7a878..5c06f08ad79 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -1792,9 +1792,7 @@ int64_t String::hex_to_int64(bool p_with_prefix) const { int64_t String::bin_to_int64(bool p_with_prefix) const { int len = length(); - if (len == 0 || (p_with_prefix && len < 3)) { - return 0; - } + ERR_FAIL_COND_V_MSG(p_with_prefix ? len < 3 : len == 0, 0, String("Invalid binary notation length in string ") + (p_with_prefix ? "with" : "without") + " prefix \"" + *this + "\"."); const CharType *s = ptr(); @@ -1805,9 +1803,7 @@ int64_t String::bin_to_int64(bool p_with_prefix) const { } if (p_with_prefix) { - if (s[0] != '0' || s[1] != 'b') { - return 0; - } + ERR_FAIL_COND_V_MSG(s[0] != '0' || LOWERCASE(s[1]) != 'b', 0, "Invalid binary notation prefix in string \"" + *this + "\"."); s += 2; } @@ -1819,7 +1815,7 @@ int64_t String::bin_to_int64(bool p_with_prefix) const { if (c == '0' || c == '1') { n = c - '0'; } else { - return 0; + ERR_FAIL_V_MSG(0, "Invalid binary notation character \"" + chr(*s) + "\" in string \"" + *this + "\"."); } // Check for overflow/underflow, with special case to ensure INT64_MIN does not result in error bool overflow = ((binary > INT64_MAX / 2) && (sign == 1 || (sign == -1 && binary != (INT64_MAX >> 1) + 1))) || (sign == -1 && binary == (INT64_MAX >> 1) + 1 && c > '0');