Skip to content

Commit 122e572

Browse files
zhifengzhuangdaniel.zhuangjcupittkleisauke
authored
pdfiumload: guard against NULL-bitmaps (#4599)
* pdfiumload: guard against `NULL`-bitmaps * Update libvips/foreign/pdfiumload.c Co-authored-by: Kleis Auke Wolthuizen <github@kleisauke.nl> --------- Co-authored-by: daniel.zhuang <zhuangzengfeng@gmail.com> Co-authored-by: John Cupitt <jcupitt@gmail.com> Co-authored-by: Kleis Auke Wolthuizen <github@kleisauke.nl>
1 parent 2e78894 commit 122e572

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ master
1010
- tiffsave: always apply resolution unit conversion [kleisauke]
1111
- cache: suppress invalidation errors in release builds [kleisauke]
1212
- dzsave: IIIF: use named region of 'full' when no crop takes place [lovell]
13+
- pdfload: fix potential crash with pdfium < 6633 [zhifengzhuang]
1314

1415
5/6/25 8.17.0
1516

libvips/foreign/pdfiumload.c

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -584,47 +584,53 @@ vips_foreign_load_pdf_generate(VipsRegion *out_region,
584584

585585
top = r->top;
586586
while (top < VIPS_RECT_BOTTOM(r)) {
587-
VipsRect rect;
588587
FPDF_BITMAP bitmap;
589588

589+
/* Is the rect within this page? It might not be if the output is more
590+
* than one tile wide and this page is narrower.
591+
*/
592+
VipsRect rect;
590593
vips_rect_intersectrect(r, &pdf->pages[i], &rect);
594+
if (rect.width > 0 &&
595+
rect.height > 0) {
591596

592-
if (vips_foreign_load_pdf_get_page(pdf, pdf->page_no + i))
593-
return -1;
597+
if (vips_foreign_load_pdf_get_page(pdf, pdf->page_no + i))
598+
return -1;
594599

595-
vips__worker_lock(&vips_pdfium_mutex);
600+
vips__worker_lock(&vips_pdfium_mutex);
596601

597-
/* 4 means RGBA.
598-
*/
599-
bitmap = FPDFBitmap_CreateEx(rect.width, rect.height, 4,
600-
VIPS_REGION_ADDR(out_region, rect.left, rect.top),
601-
VIPS_REGION_LSKIP(out_region));
602+
/* 4 means RGBA.
603+
*/
604+
bitmap = FPDFBitmap_CreateEx(rect.width, rect.height, 4,
605+
VIPS_REGION_ADDR(out_region, rect.left, rect.top),
606+
VIPS_REGION_LSKIP(out_region));
602607

603-
/* Only paint the background if there's no transparency.
604-
*/
605-
if (!FPDFPage_HasTransparency(pdf->page)) {
606-
FPDF_DWORD ink = *((guint32 *) pdf->ink);
608+
/* Only paint the background if there's no transparency.
609+
*/
610+
if (!FPDFPage_HasTransparency(pdf->page)) {
611+
FPDF_DWORD ink = *((guint32 *) pdf->ink);
607612

608-
FPDFBitmap_FillRect(bitmap,
609-
0, 0, rect.width, rect.height, ink);
610-
}
613+
FPDFBitmap_FillRect(bitmap,
614+
0, 0, rect.width, rect.height, ink);
615+
}
611616

612-
// pdfium writes bgra by default, we need rgba
613-
FPDF_RenderPageBitmap(bitmap, pdf->page,
614-
pdf->pages[i].left - rect.left,
615-
pdf->pages[i].top - rect.top,
616-
pdf->pages[i].width, pdf->pages[i].height,
617-
0, FPDF_ANNOT | FPDF_REVERSE_BYTE_ORDER);
617+
// pdfium writes bgra by default, we need rgba
618+
FPDF_RenderPageBitmap(bitmap, pdf->page,
619+
pdf->pages[i].left - rect.left,
620+
pdf->pages[i].top - rect.top,
621+
pdf->pages[i].width, pdf->pages[i].height,
622+
0, FPDF_ANNOT | FPDF_REVERSE_BYTE_ORDER);
618623

619-
FPDF_FFLDraw(pdf->form, bitmap, pdf->page,
620-
pdf->pages[i].left - rect.left,
621-
pdf->pages[i].top - rect.top,
622-
pdf->pages[i].width, pdf->pages[i].height,
623-
0, FPDF_ANNOT | FPDF_REVERSE_BYTE_ORDER);
624+
FPDF_FFLDraw(pdf->form, bitmap, pdf->page,
625+
pdf->pages[i].left - rect.left,
626+
pdf->pages[i].top - rect.top,
627+
pdf->pages[i].width, pdf->pages[i].height,
628+
0, FPDF_ANNOT | FPDF_REVERSE_BYTE_ORDER);
624629

625-
FPDFBitmap_Destroy(bitmap);
630+
FPDFBitmap_Destroy(bitmap);
626631

627-
g_mutex_unlock(&vips_pdfium_mutex);
632+
g_mutex_unlock(&vips_pdfium_mutex);
633+
}
628634

629635
top += rect.height;
630636
i += 1;

0 commit comments

Comments
 (0)