Fix handling negative screen filters values

This commit is contained in:
TuxSH 2023-03-27 21:35:51 +02:00
parent ef1072f996
commit dc7edbd44f
2 changed files with 35 additions and 9 deletions

View File

@ -142,29 +142,55 @@ static int parseDecIntOption(s64 *out, const char *val, s64 minval, s64 maxval)
static int parseDecFloatOption(s64 *out, const char *val, s64 minval, s64 maxval)
{
char *point = strchrnul(val, '.');
if (point == val) {
s64 sign = 1;// intPart < 0 ? -1 : 1;
switch (val[0]) {
case '\0':
return -1;
case '+':
++val;
break;
case '-':
sign = -1;
++val;
break;
default:
break;
}
// Reject "-" and "+"
if (val[0] == '\0') {
return -1;
}
char *point = strchrnul(val, '.');
// Parse integer part, then fractional part
s64 intPart = 0;
s64 fracPart = 0;
int rc = parseDecIntOptionImpl(&intPart, val, point - val, INT64_MIN, INT64_MAX);
int rc = 0;
if (point == val) {
// e.g. -.5
if (val[1] == '\0')
return -1;
}
else {
rc = parseDecIntOptionImpl(&intPart, val, point - val, INT64_MIN, INT64_MAX);
}
if (rc != 0) {
return -1;
}
s64 sign = intPart < 0 ? -1 : 1;
s64 intPartAbs = sign == -1 ? -intPart : intPart;
s64 res = 0;
bool of = __builtin_mul_overflow(intPartAbs, FLOAT_CONV_MULT, &res);
if (of) {
return -1;
}
s64 mul = FLOAT_CONV_MULT / 10;
// Check if there's a fractional part
@ -173,7 +199,7 @@ static int parseDecFloatOption(s64 *out, const char *val, s64 minval, s64 maxval
if (*pos < '0' || *pos > '9') {
return -1;
}
res += (*pos - '0') * mul;
mul /= 10;
}

View File

@ -179,8 +179,8 @@ int floatToString(char *out, float f, u32 precision, bool pad)
u64 mult = pow10[precision];
double f2 = fabs((double)f) * mult + 0.5;
const char *sign = f2 >= 0.0f ? "" : "-";
u64 f3 = (u64)f2;
const char *sign = (f >= 0.0f || f3 == 0) ? "" : "-";
u64 intPart = f3 / mult;
u64 fracPart = f3 % mult;
@ -194,7 +194,7 @@ int floatToString(char *out, float f, u32 precision, bool pad)
return n;
n += sprintf(out + n, ".%0*llu", (int)precision, fracPart);
int n2 = n - 1;
while (out[n2] == '0')
out[n2--] = '\0';