commit 058a91395876681d1578b12dc6e9076b2ea71b44
parent 85b25faa80157b15023a82759b585a4b7c63e0cf
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 12:13:54 +0000
Match legacy fixed-point printf formatting on Linux
Diffstat:
2 files changed, 42 insertions(+), 0 deletions(-)
diff --git a/decompiled/runtime/guest_format.c b/decompiled/runtime/guest_format.c
@@ -118,6 +118,31 @@ static void format_output(struct output_buffer *output, const char *format, uint
if (bits == 16) value = (uint16_t)value;
append_value(output, native, width, precision, (unsigned long long)value);
}
+ } else if (type == 'f' && bits == 32) {
+ double value = MEMD(arguments);
+ arguments += 8;
+ if (!isfinite(value)) unsupported(start);
+ *end++ = type;
+ *end = 0;
+ append_value(output, native, width, precision, value);
+
+ /* MSVCRT rounds halfway away from zero; libc rounds to even. A
+ * decimal halfway value has an odd significand times 2^(-digits-1).
+ * Multiplication by 5^digits leaves its low two bits unchanged. */
+ uint64_t bits;
+ memcpy(&bits, &value, sizeof(bits));
+ int exponent = (bits >> 52) & 0x7ff;
+ uint64_t significand = bits & 0xfffffffffffffull;
+ if (exponent) significand |= 0x10000000000000ull;
+ exponent = exponent ? exponent - 1075 : -1074;
+ if (significand) {
+ while (!(significand & 1)) { significand >>= 1; ++exponent; }
+ if ((int64_t)exponent + (precision < 0 ? 6 : precision) == -1 && (significand & 3) == 1) {
+ size_t digit = output->length;
+ while (output->bytes[digit - 1] < '0' || output->bytes[digit - 1] > '9') --digit;
+ ++output->bytes[digit - 1]; /* The rounded-down final digit is even, so no carry. */
+ }
+ }
} else if ((type == 's' || type == 'c') && !wide && bits != 64) {
uint32_t value = MEM32(arguments);
arguments += 4;
diff --git a/decompiled/tests/guest_format.c b/decompiled/tests/guest_format.c
@@ -119,6 +119,23 @@ int main(void)
compare_case("[%p][%-12p]", (uint32_t[16]){0, 0xabcd}, "[00000000][0000ABCD ]");
compare_case("[%#p][%+#012p]", (uint32_t[16]){0, 0}, "[00000000][ 00000000]");
assert(cases == 288 && digest == 0x3896b505);
+ compare_case("%.f%%", (uint32_t[16]){0, 0x40590000}, "100%");
+ compare_case("%f %d", (uint32_t[16]){0, 0x3ff40000, 7}, "1.250000 7");
+ compare_case("[%08.2f][%+.1f]", (uint32_t[16]){0, 0x3ff40000, 0, 0x40040000}, "[00001.25][+2.5]");
+ compare_case("%.0f %.0f", (uint32_t[16]){0, 0x40040000, 0, 0xc0040000}, "3 -3");
+ compare_case("%.1f", (uint32_t[16]){0, 0x3ff40000}, "1.3");
+ compare_case("%f", (uint32_t[16]){0, 0x80000000}, "-0.000000");
+ compare_case("%*.*f", (uint32_t[16]){(uint32_t)-8, 2, 0, 0x3ff40000}, "1.25 ");
+ compare_case("[%#6.0f]", (uint32_t[16]){0, 0x40040000}, "[ 3.]");
+ compare_case("%.17f", (uint32_t[16]){0, 0x3ed00000}, "0.00000381469726563");
+ double fraction = 2.675;
+ uint32_t floating[16] = {0};
+ memcpy(floating, &fraction, sizeof(fraction));
+ compare_case("%.2f", floating, "2.67");
+ compare_case("%.0f %.0f %.0f %.0f", (uint32_t[16]){0, 0x400c0000, 0, 0x40120000,
+ 0, 0xc00c0000, 0, 0xc0120000}, "4 5 -4 -5");
+ compare_case("%f", (uint32_t[16]){0, 0x3f800000}, "0.007813");
+ assert(cases == 480 && digest == 0x401f5575);
printf("printf: %u cases, digest %08x, guest arguments, truncation and canaries passed\n", cases, digest);
return 0;
}