Skip to content

texttopdf: don't highlight a comment token's first char across a page wrap - #273

Open
ManasBagul23 wants to merge 1 commit into
OpenPrinting:masterfrom
ManasBagul23:fix-texttopdf-comment-boundary-oob
Open

ManasBagul23 wants to merge 1 commit into
OpenPrinting:masterfrom
ManasBagul23:fix-texttopdf-comment-boundary-oob

Conversation

@ManasBagul23

Copy link
Copy Markdown
Contributor

Problem

When C/C++ pretty-printing recognizes the start of a comment (/* or
//), the highlighter marks the opening character bold/italic by writing
doc.Page[line][column - 1].attr — column - 1 because by the time this
runs, the loop has already advanced past that character to the current one
(lastch == '/', ch == '*' or '/').

If the / happened to be the very last character that fit on the current
line/page, the automatic wrap earlier in the same iteration resets
column to 0 (and, on a page boundary, calls write_page(), which
allocates/clears a fresh doc.Page buffer) before this highlight code
runs. column - 1 then becomes -1, writing two bytes immediately before
the new page's heap allocation.

Fixes #196

Fix

Skip the highlight when column is 0 — there is no previous cell on the
current line to mark in that case, since the / that started the comment
was wrapped onto a page/line that no longer exists at this point.

Verified

No C build toolchain available locally to compile-check. Verified by
tracing the ordering of the wrap block (which resets column/line/page
before this code runs) against this write, confirming column == 0 here
always means the only candidate previous cell was on the page just
flushed, not the current one.

… wrap

When C/C++ pretty-printing recognizes the start of a comment ('/*' or
'//'), the highlighter marks the opening character bold/italic by
writing doc.Page[line][column - 1].attr -- "column - 1" because by the
time this runs, the loop has already advanced past that character to
the current one (lastch == '/', ch == '*' or '/').

If the '/' happened to be the very last character that fit on the
current line/page, the automatic wrap earlier in the same iteration
resets "column" to 0 (and, on a page boundary, calls write_page(),
which allocates/clears a fresh doc.Page buffer) before this highlight
code runs. "column - 1" then becomes -1, writing two bytes immediately
before the new page's heap allocation (OpenPrinting#196).

Skip the highlight when column is 0 -- there is no previous cell on
the current line to mark in that case, since the '/' that started the
comment was wrapped onto a page/line that no longer exists at this
point.

Verified by tracing the ordering of the wrap block (which resets
column/line/page before this code runs) against this write, confirming
column == 0 here always means the only candidate previous cell was on
the page just flushed, not the current one. No C build toolchain
available locally to compile-check.

Fixes OpenPrinting#196
Copilot AI balanced review requested due to automatic review settings October 6, 2026 18:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

texttopdf: comment token across a page boundary writes before the page buffer

2 participants