Skip to content

Commit b3b24de

Browse files
committed
Add checks for valid base in String::num_int64, uint64().
- Ensure String::num_int64, uint64 returns an empty string for bases less than 2 or greater than 36. - Added corresponding test cases to verify the behavior. - Error messages are printed when invalid bases are encountered. These messages are suppressed in the test output.
1 parent 44fa552 commit b3b24de

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

core/string/ustring.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,8 @@ String String::num(double p_num, int p_decimals) {
18501850
}
18511851

18521852
String String::num_int64(int64_t p_num, int base, bool capitalize_hex) {
1853+
ERR_FAIL_COND_V_MSG(base < 2 || base > 36, "", "Cannot convert to base " + itos(base) + ", since the value is " + (base < 2 ? "less than 2." : "greater than 36."));
1854+
18531855
bool sign = p_num < 0;
18541856

18551857
int64_t n = p_num;
@@ -1888,6 +1890,8 @@ String String::num_int64(int64_t p_num, int base, bool capitalize_hex) {
18881890
}
18891891

18901892
String String::num_uint64(uint64_t p_num, int base, bool capitalize_hex) {
1893+
ERR_FAIL_COND_V_MSG(base < 2 || base > 36, "", "Cannot convert to base " + itos(base) + ", since the value is " + (base < 2 ? "less than 2." : "greater than 36."));
1894+
18911895
uint64_t n = p_num;
18921896

18931897
int chars = 0;

tests/core/string/test_string.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,27 @@ TEST_CASE("[String] Number to string") {
460460
CHECK(String::num(-0.0) == "-0"); // Includes sign even for zero.
461461
CHECK(String::num(3.141593) == "3.141593");
462462
CHECK(String::num(3.141593, 3) == "3.142");
463+
CHECK(String::num(42.100023, 4) == "42.1"); // No trailing zeros.
463464
CHECK(String::num_scientific(30000000) == "3e+07");
465+
466+
// String::num_int64 tests.
464467
CHECK(String::num_int64(3141593) == "3141593");
468+
CHECK(String::num_int64(-3141593) == "-3141593");
465469
CHECK(String::num_int64(0xA141593, 16) == "a141593");
466470
CHECK(String::num_int64(0xA141593, 16, true) == "A141593");
467-
CHECK(String::num(42.100023, 4) == "42.1"); // No trailing zeros.
471+
ERR_PRINT_OFF;
472+
CHECK(String::num_int64(3141593, 1) == ""); // Invalid base < 2.
473+
CHECK(String::num_int64(3141593, 37) == ""); // Invalid base > 36.
474+
ERR_PRINT_ON;
475+
476+
// String::num_uint64 tests.
477+
CHECK(String::num_uint64(4294967295) == "4294967295");
478+
CHECK(String::num_uint64(0xF141593, 16) == "f141593");
479+
CHECK(String::num_uint64(0xF141593, 16, true) == "F141593");
480+
ERR_PRINT_OFF;
481+
CHECK(String::num_uint64(4294967295, 1) == ""); // Invalid base < 2.
482+
CHECK(String::num_uint64(4294967295, 37) == ""); // Invalid base > 36.
483+
ERR_PRINT_ON;
468484

469485
// String::num_real tests.
470486
CHECK(String::num_real(1.0) == "1.0");

0 commit comments

Comments
 (0)