Prevent occasional freeze on startup

If the console window has a full eight-digit handle my custom printf
would get stuck in a loop, causing CMD to seemingly freeze.  Do what I
really should have done in the first place and make it more robust.
This commit is contained in:
Jason Hood 2019-04-29 20:13:07 +10:00
parent 0dd40f6d74
commit 0fc890dddd

32
util.c
View File

@ -593,7 +593,7 @@ int ac_sprintf( char* buf, const char* fmt, ... )
} }
do do
{ {
*buf++ = (unsigned)(num / power) % 10 + '0'; *buf++ = num / power % 10 + '0';
power /= 10; power /= 10;
} while (power); } while (power);
} }
@ -609,7 +609,7 @@ int ac_sprintf( char* buf, const char* fmt, ... )
// * BUF is big enough; // * BUF is big enough;
// * FMT is valid; // * FMT is valid;
// * width is only for X, is only 2 and the number is not bigger than that; // * width is only for X, is only 2 and the number is not bigger than that;
// * X, d & u are 32-bit unsigned decimal, a digit less than maximum; // * X, d & u are 32-bit unsigned decimal;
// * c is not output if NUL; // * c is not output if NUL;
// * no other type is used; // * no other type is used;
// * return value is not used. // * return value is not used.
@ -638,9 +638,15 @@ int ac_wprintf( wchar_t* buf, const char* fmt, ... )
} }
else if (t == 'X') else if (t == 'X')
{ {
int bits = 4; int bits;
while (num >> bits) if (num & 0xF0000000)
bits += 4; bits = 32;
else
{
bits = 4;
while (num >> bits)
bits += 4;
}
do do
{ {
bits -= 4; bits -= 4;
@ -654,14 +660,20 @@ int ac_wprintf( wchar_t* buf, const char* fmt, ... )
} }
else // (t == 'd' || t == 'u') else // (t == 'd' || t == 'u')
{ {
int power = 10; unsigned power;
while (num / power) if (num >= 1000000000)
power *= 10; power = 1000000000;
else
{
power = 1;
while (num / (power * 10))
power *= 10;
}
do do
{ {
*buf++ = num / power % 10 + '0';
power /= 10; power /= 10;
*buf++ = (int)(num / power) % 10 + '0'; } while (power);
} while (power != 1);
} }
} }
} }