You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
font_collection_num_fonts_overflow test fails on 32-bit architectures #127
---- 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.
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
+ }
}
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.
Activity
RazrFalcon commentedon Jul 17, 2023
Can confirm.
plugwash commentedon Jul 18, 2023
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.
RazrFalcon commentedon Jul 18, 2023
@plugwash yes, I know that this is because
usizeis 32bit here, but I want a global solution and not a local one. Will think about it.NoisyCoil commentedon Sep 18, 2024
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.
Disable font_collection_num_fonts_overflow test unless arch is 64-bit
Goldziher commentedon Aug 5, 2026
Fixed in
9234a21. You were right, and the diagnosis in this thread understated it.read_array32now checks the requested length against the remaining buffer before callingread_bytes, so an absurdnumFontsis rejected as malformed rather than tripping thedebug_assert.Face::parsereturnsMalformedFontin both profiles now.Two things worth recording. First, this was not only a 32-bit problem: the assert compares
offset + lenagainstu32::MAX, so on 64-bitu32::MAX * 4overshoots it comfortably and the same abort occurs. Second, the#[should_panic]attribute added ind68387ehad enshrined the debug-mode abort as expected behaviour, which meantcargo test --releasewas failing onmain— release correctly returnedMalformedFont, so the test did not panic andshould_panicfailed it. CI only ran debug, so nobody saw it.I removed the
#[should_panic]and added acargo test --releaseleg to CI (e94faad) so the two profiles can no longer silently diverge.The residual part of #178 — other
read_bytescallers that derive a length from font data and have not been audited — is now tracked separately in #240.