From e1db1b215a90e0fc62c689a9a12be10aea258fcb Mon Sep 17 00:00:00 2001 From: ebraminio Date: Thu, 5 Nov 2026 00:29:07 +0330 Subject: [PATCH] [CFF] Fix trailing-zero exponent in BCD encoder (#6577) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Block 3 assumed exactly 3 trailing zeros (matching its "000" guard) and then walked backward from len - 4, incrementing exponent on each step. The two roles on one variable — start position AND final count — only coincided when the mantissa had 3 leading digits, so any "X...0000" with 4+ zeros was off by 10x or more: 123000 -> "123E3" (correct) 1230000 -> "123E5" (should be E4) 12340000 -> "1234E6" (should be E4) 1000000 -> "1E7" (should be E6) Real fonts only produce fractional BCD values (0.001, 0.039625), so this is latent, but it silently corrupts the output when triggered. Fix: count trailing zeros from s + len - 1. s[0] is guaranteed non-'0' by blocks 1-2 ("0.xxx"/"-0.xxx"), so the backward scan terminates before reaching s. Also moves encode_byte(OpCode_BCD) past the nibble-buffer allocation so OOM bails cleanly instead of emitting a marker with no digits. Tests: 12340000.0, 1000000.0 (block 3 with 4+ zeros); 0.0012 (block 1->6); 1.23e-5 (block 4, negative exp); 123456780.0 (block 5, newExponent==1); 0.000123, -0.000123 (block 1/2 -> 6 chain). --- src/hb-subset-cff-common.hh | 35 +++++++++++++++++++++++++---------- src/test-cff.cc | 24 +++++++++++++++++++++--- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/hb-subset-cff-common.hh b/src/hb-subset-cff-common.hh index 869875990..2fd8d9489 100644 --- a/src/hb-subset-cff-common.hh +++ b/src/hb-subset-cff-common.hh @@ -122,7 +122,6 @@ struct str_encoder_t // Sigh. BCD // https://learn.microsoft.com/en-us/typography/opentype/spec/cff2#table-5-nibble-definitions double v = n.to_real (); - encode_byte (OpCode_BCD); // Based on: // https://github.com/fonttools/fonttools/blob/0738c41dfbcbc213ab9263f486ef0cccc6eb5ce5/Lib/fontTools/misc/psCharStrings.py#L267-L316 @@ -139,9 +138,14 @@ struct str_encoder_t hb_locale_t clocale HB_UNUSED; hb_locale_t oldlocale HB_UNUSED; oldlocale = hb_uselocale (clocale = newlocale (LC_ALL_MASK, "C", NULL)); - snprintf (buf, sizeof (buf), "%.8G", v); + int n_printed = snprintf (buf, sizeof (buf), "%.8G", v); (void) hb_uselocale (((void) freelocale (clocale), oldlocale)); + /* Defensive: buf[16] exactly fits the widest %.8G output (15 chars + * + NUL); bail cleanly if snprintf ever errors or truncates. */ + if (unlikely (n_printed < 0 || (size_t) n_printed >= sizeof (buf))) + return; + char *s = buf; size_t len; char *comma = strchr (s, ','); @@ -156,14 +160,18 @@ struct str_encoder_t } else if ((len = strlen (s)) > 3 && !strcmp (s + len - 3, "000")) { - unsigned exponent = len - 3; - char *s2 = s + exponent - 1; - while (*s2 == '0' && exponent > 1) - { - s2--; - exponent++; - } - snprintf (s2 + 1, sizeof (buf) - (s2 + 1 - buf), "E%u", exponent); + /* Count trailing zeros from the end. s[0] is guaranteed non-'0' + * here (blocks 1-2 handle "0.xxx"/"-0.xxx" first, and %G never + * emits trailing fractional zeros), so the backward scan always + * terminates before reaching s. */ + unsigned exponent = 0; + char *s2 = s + len - 1; + while (*s2 == '0') + { + s2--; + exponent++; + } + snprintf (s2 + 1, sizeof (buf) - (s2 + 1 - buf), "E%u", exponent); } else { @@ -233,6 +241,13 @@ struct str_encoder_t if (nibbles.length % 2) nibbles.push (0x0F); + if (unlikely (nibbles.in_error ())) + { + buff.set_error (); + return; + } + encode_byte (OpCode_BCD); + unsigned count = nibbles.length; for (unsigned i = 0; i < count; i += 2) encode_byte ((nibbles[i] << 4) | nibbles[i+1]); diff --git a/src/test-cff.cc b/src/test-cff.cc index 8a4ccb923..06ae9fcc3 100644 --- a/src/test-cff.cc +++ b/src/test-cff.cc @@ -27,6 +27,7 @@ #include "hb-ot-cff1-table.hh" #include "hb-subset-cff-common.hh" #include "hb-cff-specializer.hh" +#include "hb-cff-interp-dict-common.hh" static void test_specialized_line_geometry () @@ -203,7 +204,8 @@ main (int argc, char **argv) { 9.399999999999999999, 3, { 0x1E, 0x9A, 0x4F } }, // 9.4 { 456.8, 4, { 0x1E, 0x45, 0x6A, 0x8F } }, // 456.8 { 98765.37e2, 5, { 0x1E, 0x98, 0x76, 0x53, 0x7F } }, // 9876537 - { 1234567890.0, 7, { 0x1E, 0x12, 0x34, 0x56, 0x79, 0xB2, 0xFF } }, // 12345679E2 + { 1234567890.0, 7, { 0x1E, 0x12, 0x34, 0x56, 0x79, 0xB2, 0xFF } }, // 12345679E2 (rounded) + { 1234567900.0, 7, { 0x1E, 0x12, 0x34, 0x56, 0x79, 0xB2, 0xFF } }, // 12345679E2 { 9.876537e-4, 7, { 0x1E, 0x98, 0x76, 0x53, 0x7C, 0x10, 0xFF } }, // 9876537E-10 { 9.876537e4, 6, { 0x1E, 0x98, 0x76, 0x5A, 0x37, 0xFF } }, // 98765.37 { 1e8, 3, { 0x1E, 0x1B, 0x8F } }, // 1E8 @@ -217,15 +219,31 @@ main (int argc, char **argv) { -0.01, 4, { 0x1E, 0xE1, 0xC2, 0xFF } }, // -1E-2 { 0.0123, 4, { 0x1E, 0x12, 0x3C, 0x4F } }, // 123E-4 { -0.0123, 5, { 0x1E, 0xE1, 0x23, 0xC4, 0xFF } }, // -123E-4 + { 0.0012, 4, { 0x1E, 0x12, 0xC4, 0xFF } }, + { 1.23e-5, 4, { 0x1E, 0x12, 0x3C, 0x7F } }, // 123E-7 + { 123456780.0, 6, { 0x1E, 0x12, 0x34, 0x56, 0x78, 0x0F } }, // 123456780 + { 0.000123, 4, { 0x1E, 0x12, 0x3C, 0x6F } }, // 123E-6 + { -0.000123, 5, { 0x1E, 0xE1, 0x23, 0xC6, 0xFF } }, // -123E-6 + /* Test cases for trailing zeros fix */ + { 5000.0, 3, { 0x1C, 0x13, 0x88 } }, // 5000 + { 12340000.0, 5, { 0x1E, 0x12, 0x34, 0xB4, 0xFF } }, // 1234E4 + { 1000000.0, 3, { 0x1E, 0x1B, 0x6F } }, // 1E6 }; - for (size_t t = 0; t < sizeof num_tp_tests / sizeof num_tp_tests[0]; t++) + for (auto &num_tp_test : num_tp_tests) { - struct num_tp_test num_tp_test = num_tp_tests[t]; number.set_real (num_tp_test.input); encoder.encode_num_tp (number); hb_always_assert (buff.length == num_tp_test.length); for (unsigned i = 0; i < buff.length; i++) hb_always_assert (buff[i] == num_tp_test.output[i]); + if (buff.length > 1 && buff[0] == 0x1E) /* OpCode_BCD = 30 = 0x1E */ + { + hb_ubytes_t bytes (buff.arrayZ + 1, buff.length - 1); + CFF::byte_str_ref_t str_ref (bytes); + double parsed = CFF::dict_opset_t::parse_bcd (str_ref); + bool almost_equal = fabs (parsed - num_tp_test.input) <= fabs (num_tp_test.input) * 1e-7; + hb_always_assert (almost_equal); + } encoder.reset (); } }