Skip to content

CFF2 BLEND operator can pop from an empty argument stack — same root cause as #80, unpatched in CFF2 #218

Closed

Description

@scadastrangelove

#80 (fixed by f28f7a5) was a panic in ArgumentsStack::pop()'s debug_assert!(!self.is_empty())
being violated in the CFF (v1) seac/endchar path. That fix only touched cff1.rs's seac
handling; argstack.rs's pop() itself is unchanged, and the identical bug exists — unfixed — in
CFF2's BLEND operator (cff2.rs):

operator::BLEND => {
    ctx.had_blend = true;
    let n = u16::try_num_from(p.stack.pop())   // <-- no is_empty() check
        .ok_or(CFFError::InvalidNumberOfBlendOperands)?;
    ...

Unlike the adjacent VS_INDEX operator (which checks p.stack.len() != 1 before its pop),
BLEND pops unconditionally. debug_assert! is compiled out in release builds, but the
subsequent self.data[self.len] slice index is not — len -= 1 on an already-zero len
silently wraps to usize::MAX (release, no overflow-checks), and the mandatory bounds check on
self.data[usize::MAX] panics regardless of build profile.

This is a crash-only bug (a reachable panic, same class as #80), not a memory-safety issue — the
mandatory bounds check does its job and aborts cleanly rather than reading out of bounds.

Reproduction (ttf-parser 0.25.1, and current main @ 6e75b3c)

A CFF2 table whose CharStrings INDEX holds a single-byte charstring containing only the BLEND
opcode (16), with a valid (if degenerate) ItemVariationStore so update_scalars(0) in
parse_char_string's wrapper succeeds before dispatch:

$ cargo run --release
thread 'main' panicked at src/tables/cff/argstack.rs:40:9:
index out of bounds: the len is 513 but the index is 18446744073709551615

Full self-contained reproducer (builds a minimal CFF2-flavored sfnt, calls outline_glyph)
available on request — ~110 lines, constructs head/hhea/maxp/CFF2 tables byte-by-byte.

Fix

PR incoming: adds an is_empty() guard before the BLEND operand-count pop, mirroring VS_INDEX's
existing guard.


Discovered by the rust-in-peace security pipeline.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions