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