fix(string): avoid undefined behavior in SStrToFloat

This commit is contained in:
phaneron 2024-07-05 19:15:27 -04:00
parent bbd07f7706
commit 08a8a33518
2 changed files with 7 additions and 3 deletions

View file

@ -607,7 +607,6 @@ float SStrToFloat(const char* string) {
int32_t v24 = 0; int32_t v24 = 0;
if (v23 < 10) { if (v23 < 10) {
int32_t v25 = 0;
int32_t v26 = -1; int32_t v26 = -1;
double v31; double v31;
@ -615,7 +614,7 @@ float SStrToFloat(const char* string) {
string++; string++;
if (v24 < 20) { if (v24 < 20) {
v31 = s_realDigit[0][v25 + v23]; v31 = s_realDigit[v24][v23];
} else { } else {
v31 = pow(v16, v26) * v23; v31 = pow(v16, v26) * v23;
} }
@ -625,7 +624,6 @@ float SStrToFloat(const char* string) {
v23 = *string - '0'; v23 = *string - '0';
v24++; v24++;
v26--; v26--;
v25 += 10;
} while (v23 < 10); } while (v23 < 10);
} }
} }

View file

@ -353,6 +353,12 @@ TEST_CASE("SStrToFloat", "[string]") {
auto result = SStrToFloat(string); auto result = SStrToFloat(string);
REQUIRE(result == 1.0f); REQUIRE(result == 1.0f);
} }
SECTION("converts string without causing UBsan exception") {
auto string = "0.82";
auto result = SStrToFloat(string);
REQUIRE(result == 0.82f);
}
} }
TEST_CASE("SStrToInt", "[string]") { TEST_CASE("SStrToInt", "[string]") {