Skip to content

tex.ch: fix 7 integer overflow vulnerabilities in tex.web arithmetic #83

@dangerzig

Description

@dangerzig

Summary

A systematic review of tex.web (Version 3.141592653) identified 7 integer overflow vulnerabilities under the 32-bit Pascal semantics assumed by the WEB source. Most are currently masked on 64-bit web2c platforms but represent latent incorrectness. All patches use TeX's own overflow-safe primitives.

Bug Section Description Fix
DZ1 S457 Magnification ratio overflow xn_over_d
DZ2 S762 Delimiter factor overflow xn_over_d
DZ4 S859 Demerits accumulation overflow mult_and_add
DZ5 S890 Penalty accumulation overflow mult_and_add
DZ6 S986, S1008-1010 Page insertion arithmetic overflow (5 sites) xn_over_d
DZ7 S1005 Page cost overflow mult_and_add
DZ8 S1236 \advance has no overflow protection inline check

DZ8 is the only bug demonstrable on current 64-bit TeX Live:

\newcount\big \big=2000000000
\advance\big by 1000000000
\message{\the\big}  % prints -1294967296 (silent wraparound)

After the patch, this correctly reports "Arithmetic overflow".

Note: DZ8 cannot use mult_and_add because mult_and_add's own overflow check (max_answer+y) itself overflows when max_answer = max_int. An inline check is used instead.

Companion issue

The hyphenation exception leak (DZ3) is filed separately as #82.

Trip test impact

Two categories of expected differences in trip.log:

  1. DZ6 changes rounding order (divide-then-multiply to multiply-then-divide), producing slightly more accurate page goal and insertion-split values throughout.
  2. DZ3 (in tex.ch: fix hyphenation exception slot and memory leak (S941) #82) reduces string pool memory by ~5 words at every Memory usage report.

tripin.log and trip.typ are identical. Full diffs are in the attached trip-test-diffs.txt.

Context

DZ1-DZ3 were reported to Professor Knuth by letter (March 2026) and to tex-k@tug.org. DZ4-DZ8 were found during the same review. The complete changefile patch was sent to the list for review in the thread "Three potential bugs in tex.web (Version 3.141592653)".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions