Skip to content

fix: avoid panic in layout when a section has no page definition - #9

Merged
Indosaram merged 1 commit into
Indosaram:mainfrom
chiliec:fix-layout-panic-missing-page-def
Sep 6, 2026
Merged

Indosaram merged 1 commit into
Indosaram:mainfrom
chiliec:fix-layout-panic-missing-page-def

Conversation

@chiliec

@chiliec chiliec commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Problem

LayoutEngine::calculate_layout() panics on a parsed document whose section has no PageDef record:

thread 'main' panicked at src/render/layout.rs:86:14:
Section must have page definition

layout_section unwraps section.page_def with .expect("Section must have page definition"). But a page definition is optional in the body-text stream — PageDef is only set for the PageDef (0x57) record, and Section::page_def defaults to None. Any valid HWP file that doesn't carry that record crashes every caller of the public layout API.

This is reproducible with the repository's own sample file styled_document.hwp, which parses cleanly but has no page definition:

let bytes = std::fs::read("styled_document.hwp").unwrap();
let doc = HwpReader::from_bytes(&bytes).unwrap(); // OK
LayoutEngine::new(&doc).calculate_layout();        // panics

(converted_output.hwp, which does carry a PageDef, lays out fine — 2 pages.)

Fix

Skip a section that has no page definition instead of unwrapping, so layout returns whatever it can rather than aborting the process.

Tests

  • New regression test tests/layout_missing_page_def_test.rs loads the shipped styled_document.hwp, asserts a section has no page_def, and checks calculate_layout() returns without panicking.
  • Verified RED→GREEN: the test panics on the old code and passes on the fix.
  • Full suite green: cargo test --all-features — 150 tests pass, 0 failures.

Validation commands:

cargo test --all-features
cargo test --test layout_missing_page_def_test

LayoutEngine::calculate_layout panicked via expect("Section must have
page definition") for any parsed document whose section lacks a PageDef
record. PageDef is optional in the body-text stream and Section::page_def
defaults to None, so a valid HWP file without that record (e.g. the
repo's own styled_document.hwp) crashed callers of the public layout API.

Skip sections without a page definition instead of unwrapping.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants