Bug 708393

Summary: Non-Detection of unbalanced "q"/"Q" graphics operators
Product: MuPDF Reporter: Jorj <jorj.x.mckie>
Component: mupdfAssignee: MuPDF bugs <mupdf-bugs>
Status: RESOLVED FIXED    
Severity: normal CC: robin.watts, sebastian.rasmussen
Priority: P2    
Version: unspecified   
Hardware: All   
OS: All   
Customer: Word Size: ---

Description Jorj 2025-03-28 14:00:41 UTC
pymupdf issue: https://github.com/pymupdf/PyMuPDF/issues/4415
problem file: https://github.com/user-attachments/files/19503231/test.pdf

Page 0 has an array of /Contents objects
"/Contents [ 10 0 R 9 0 R 8 0 R 7 0 R ]"

with concatenated content:
 q
1 0 0 1 0 841.67999267 cm
1 0 0 -1 0 0 cm
 q
0 0 m
0 841.67999267 l
594.7199707 841.67999267 l
594.7199707 0 l
h
W
n
/OC /OCG_5a60b95a061a41958a6919570b57d483 BDC
/DeviceRGB CS
0 0 0 SC
0.72000002 w
1 J
1 j
[] 0 d
135.7000122 612.1833496 m
298.27334594 419.47000122 l
S
0 0 0 SC
444.87686157 422.20999145 m
444.87686157 502.84192504 378.90527954 568.81350708 298.27334594 568.81350708 c
217.64141235 568.81350708 151.66983032 502.84192504 151.66983032 422.20999145 c
151.66983032 341.57805786 217.64141235 275.60647583 298.27334594 275.60647583 c
378.90527954 275.60647583 444.87686157 341.57805786 444.87686157 422.20999145 c
S
EMC
   Q

So the q/Q-counts are clearly unbalanced (2, 1), none-the-less function "pdf_count_q_balance" returns (0, 0).
Comment 1 Robin Watts 2025-03-28 19:07:57 UTC
This is because the pdf interpreter mechanism automatically inserts extra pops as required. I'll think ways to fix it.
Comment 2 Robin Watts 2025-03-28 19:27:39 UTC
Fix in testing:

https://cgit.ghostscript.com/cgi-bin/cgit.cgi/user/robin/mupdf.git/commit/?h=pending&id=a9f6fc77d794f1cefac811c6e73c3352dfa5750b

If I don't get to commit it before I go on holiday, then maybe the others can?
Comment 3 Jorj 2025-03-29 16:05:03 UTC
Confirming that the issue has been correctly fixed, see comment https://github.com/pymupdf/PyMuPDF/issues/4415#issuecomment-2763595019
Comment 4 Sebastian Rasmussen 2025-04-01 12:11:58 UTC
Fixed in

commit b660c49195081d8bc2766079d261dbbb1eeb08ef
Author: Robin Watts <Robin.Watts@artifex.com>
Date:   Fri Mar 28 19:23:42 2025 +0000

    Bug 708393: Fix pdf_count_q_balance.
    
    pdf_count_q_balance relies on counting q and Q operations
    to detect mismatches.
    
    Unfortunately, it does this by running the standard PDF
    interpretation mechanisms, and these attempt to autocorrect
    for missing Q's at the end of a page. This autocorrection
    affects our reading.
    
    Insert a new processor event that happens before the auto
    correction, and use that so that pdf_count_q_balance can get
    the uncorrected values.