77aed2
From 27354e9d9696ee2bc063910a6c9a6b27c5184a52 Mon Sep 17 00:00:00 2001
77aed2
From: Albert Astals Cid <aacid@kde.org>
77aed2
Date: Thu, 25 Aug 2022 00:14:22 +0200
77aed2
Subject: JBIG2Stream: Fix crash on broken file
77aed2
77aed2
https://github.com/jeffssh/CVE-2021-30860
77aed2
77aed2
Thanks to David Warren for the heads up
77aed2
77aed2
diff --git a/poppler/JBIG2Stream.cc b/poppler/JBIG2Stream.cc
77aed2
index 662276e5..9f70431d 100644
77aed2
--- a/poppler/JBIG2Stream.cc
77aed2
+++ b/poppler/JBIG2Stream.cc
77aed2
@@ -1976,7 +1976,11 @@ void JBIG2Stream::readTextRegionSeg(unsigned int segNum, bool imm, bool lossless
77aed2
     for (i = 0; i < nRefSegs; ++i) {
77aed2
         if ((seg = findSegment(refSegs[i]))) {
77aed2
             if (seg->getType() == jbig2SegSymbolDict) {
77aed2
-                numSyms += ((JBIG2SymbolDict *)seg)->getSize();
77aed2
+                const unsigned int segSize = ((JBIG2SymbolDict *)seg)->getSize();
77aed2
+                if (unlikely(checkedAdd(numSyms, segSize, &numSyms))) {
77aed2
+                    error(errSyntaxError, getPos(), "Too many symbols in JBIG2 text region");
77aed2
+                    return;
77aed2
+                }
77aed2
             } else if (seg->getType() == jbig2SegCodeTable) {
77aed2
                 codeTables->push_back(seg);
77aed2
             }