Skip to content

font_collection_num_fonts_overflow test fails on 32-bit architectures #127

Closed

Description

@jamessan
---- font_collection_num_fonts_overflow stdout ----
thread 'font_collection_num_fonts_overflow' panicked at 'attempt to multiply with overflow', src/parser.rs:768:19
stack backtrace:
   0: rust_begin_unwind
             at /usr/src/rustc-1.66.0/library/std/src/panicking.rs:575:5
   1: core::panicking::panic_fmt
             at /usr/src/rustc-1.66.0/library/core/src/panicking.rs:65:14
   2: core::panicking::panic
             at /usr/src/rustc-1.66.0/library/core/src/panicking.rs:115:5
   3: ttf_parser::parser::Stream::read_array32
             at ./src/parser.rs:768:19
   4: ttf_parser::RawFace::parse
             at ./src/lib.rs:669:27
   5: ttf_parser::Face::parse
             at ./src/lib.rs:926:24
   6: tables::font_collection_num_fonts_overflow
             at ./tests/tables/main.rs:136:9
   7: tables::font_collection_num_fonts_overflow::{{closure}}
             at ./tests/tables/main.rs:125:1
   8: core::ops::function::FnOnce::call_once
             at /usr/src/rustc-1.66.0/library/core/src/ops/function.rs:251:5
   9: core::ops::function::FnOnce::call_once
             at /usr/src/rustc-1.66.0/library/core/src/ops/function.rs:251:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Activity

  1. added
    bug
    on Jul 17, 2023
  2. RazrFalcon commented on Jul 17, 2023

    @RazrFalcon
    Collaborator

    Can confirm.

  3. plugwash commented on Jul 18, 2023

    @plugwash

    The issue is that the code takes a 32-bit number, converts it to a usize and then multiplies it by SIZE. Afaict SIZE is always a small number so on 64-bit systems this can never overflow. However on 32-bit systems it can and does overflow.

    The change below makes the test pass, but I'd like some feedback whether this is the correct approach before I go and add it as a distribution patch.

         pub fn read_array32<T: FromData>(&mut self, count: u32) -> Option<LazyArray
    -        let len = usize::num_from(count) * T::SIZE;
    -        self.read_bytes(len).map(LazyArray32::new)
    +        if let Some(len) = usize::num_from(count).checked_mul(T::SIZE) {
    +            self.read_bytes(len).map(LazyArray32::new)
    +        } else {
    +            None
    +        }
         }
    
  4. RazrFalcon commented on Jul 18, 2023

    @RazrFalcon
    Collaborator

    @plugwash yes, I know that this is because usize is 32bit here, but I want a global solution and not a local one. Will think about it.

  5. NoisyCoil commented on Sep 18, 2024

    @NoisyCoil

    I think this issue can be closed at this point? I recently upgraded to v0.24.1 in Debian and the refactored tests passed with no patches.

  6. Goldziher commented on Aug 5, 2026

    @Goldziher
    Collaborator

    Fixed in 9234a21. You were right, and the diagnosis in this thread understated it.

    read_array32 now checks the requested length against the remaining buffer before calling read_bytes, so an absurd numFonts is rejected as malformed rather than tripping the debug_assert. Face::parse returns MalformedFont in both profiles now.

    Two things worth recording. First, this was not only a 32-bit problem: the assert compares offset + len against u32::MAX, so on 64-bit u32::MAX * 4 overshoots it comfortably and the same abort occurs. Second, the #[should_panic] attribute added in d68387e had enshrined the debug-mode abort as expected behaviour, which meant cargo test --release was failing on main — release correctly returned MalformedFont, so the test did not panic and should_panic failed it. CI only ran debug, so nobody saw it.

    I removed the #[should_panic] and added a cargo test --release leg to CI (e94faad) so the two profiles can no longer silently diverge.

    The residual part of #178 — other read_bytes callers that derive a length from font data and have not been audited — is now tracked separately in #240.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions