Debian Codegraph @

podofo/test/unit/CMapTest.cpp

raw
1 // SPDX-FileCopyrightText: 2025 Francesco Pretto <[email protected]>
2 // SPDX-License-Identifier: MIT-0
3
4 #include <PdfTest.h>
5
6 #include <podofo/private/XMPUtils.h>
7
8 using namespace std;
9 using namespace PoDoFo;
10
11 // UB: getCodeFromVariant did `code << i * 8` with int (32-bit).
12 // When a PdfString operand in a CMap section had raw length >= 5, the shift
13 // exponent reached 32+ which is undefined behavior (caught by UBSan as
14 // "shift exponent 32 is too large for 32-bit type 'int'").
15 TEST_CASE("TestCMapShiftOverflow")
16 {
17 // A beginbfchar section where the source code is a 5-byte hex string.
18 // getCodeFromVariant iterates over each byte with `code << i*8`;
19 // at i==4 the shift is 32 which is UB for a 32-bit int.
20 string_view bfcharInput =
21 "1 beginbfchar\n"
22 "<0102030405> <0041>\n"
23 "endbfchar\n"sv;
24
25 SpanStreamDevice device1(bfcharInput);
26 REQUIRE_NOTHROW(PdfCMapEncoding::Parse(device1));
27
28 // A begincidrange section with a 5-byte hex string source code.
29 string_view cidrangeInput =
30 "1 begincidrange\n"
31 "<0102030405> <0102030405> 0\n"
32 "endcidrange\n"sv;
33
34 SpanStreamDevice device2(cidrangeInput);
35 REQUIRE_NOTHROW(PdfCMapEncoding::Parse(device2));
36 }
37
38 // Bug: getCodeFromVariant used arithmetic right-shift on a signed int64_t
39 // to count code bytes: `do { codeSize++; num >>= 8; } while (num != 0)`.
40 // When the parsed number was negative (e.g. -1), arithmetic right-shift
41 // propagates the sign bit, so num stays -1 forever → infinite loop.
42 TEST_CASE("TestCMapNegativeCodeNoHang")
43 {
44 // A beginbfrange with -1 as the source-code-lo operand.
45 // Before the fix this hangs in getCodeFromVariant's byte-counting loop.
46 string_view input =
47 "-1 beginbfrange\n"
48 "-1 -1 (A)\n"
49 "endbfrange\n"sv;
50
51 SpanStreamDevice device(input);
52 REQUIRE_NOTHROW(PdfCMapEncoding::Parse(device));
53 }
54
55 // Bug: PdfCharCodeMap::PushRange called std::prev(inserted.first) when a
56 // duplicate range key was re-inserted with a larger size. If the duplicate
57 // was the first element in the set (m_Ranges.begin()), std::prev walked
58 // before begin() - undefined behavior that crashed under UBSan as a
59 // misaligned pointer access in libc++'s __tree.
60 // Trigger: two beginbfrange sections with the same srcCodeLo where the
61 // second has a larger span, and no smaller key exists in the map.
62 TEST_CASE("TestCMapDuplicateRangeAtBegin")
63 {
64 // Two ranges starting at <01> - the second is wider (size 3 vs 2).
65 // PushRange will find the duplicate key, enter the "update size" branch,
66 // and call std::prev on begin().
67 string_view input =
68 "1 beginbfrange\n"
69 "<01> <02> <0041>\n"
70 "endbfrange\n"
71 "1 beginbfrange\n"
72 "<01> <03> <0041>\n"
73 "endbfrange\n"sv;
74
75 SpanStreamDevice device(input);
76 REQUIRE_NOTHROW(PdfCMapEncoding::Parse(device));
77 }
78
79 TEST_CASE("TestCodeSpaceRange")
80 {
81 // Testing the begincodespacerange section for the same CMap tested in the
82 // Adobe CMap specification, pages 48-50:
83 // https://adobe-type-tools.github.io/font-tech-notes/pdfs/5014.CIDFont_Spec.pdf
84
85 auto cmap = PdfCMapEncoding::Parse(TestUtils::GetTestInputFilePath("83pv-RKSJ-H"));
86 auto ranges = cmap.GetCharMap().GetCodeSpaceRanges();
87 REQUIRE(ranges.size() == 5);
88 REQUIRE((ranges[0].CodeLo == 32 && ranges[0].CodeHi == 128 && ranges[0].CodeSpaceSize == 1));
89 REQUIRE((ranges[1].CodeLo == 33088 && ranges[1].CodeHi == 40956 && ranges[1].CodeSpaceSize == 2));
90 REQUIRE((ranges[2].CodeLo == 160 && ranges[2].CodeHi == 223 && ranges[2].CodeSpaceSize == 1));
91 REQUIRE((ranges[3].CodeLo == 57408 && ranges[3].CodeHi == 61180 && ranges[3].CodeSpaceSize == 2));
92 REQUIRE((ranges[4].CodeLo == 253 && ranges[4].CodeHi == 255 && ranges[4].CodeSpaceSize == 1));
93 }