Debian Codegraph @

debian/patches/0012-fix-legacy-dict-detection-on-big-endian.patch

raw
1 From: Frank Lin <[email protected]>
2 Date: Tue, 4 Aug 2026 13:02:41 -0700
3 Subject: Fix legacy 64-bit OCD layout detection on big-endian platforms
4
5 The BinaryDict and DartsDict deserializers detected the legacy 64-bit
6 layout by checking whether probe bytes [4..7] are zero, which only holds
7 on little-endian hosts; on big-endian platforms (s390x, ppc64, sparc64,
8 hppa) legacy files were misread as fixed-width and loaded as empty
9 dictionaries. Replace the byte-offset idiom at all three sites with a
10 shared LooksLikeLegacy64Field() helper that loads the probe as a native
11 uint64_t and checks its high 32 bits, which is correct on both byte
12 orders and compiles to the same behavior on little-endian. Verified with
13 BinaryDictTest and DartsDictTest on arm64 and on emulated s390x.
14
15 This fixes the following test failures on all big-endian Debian
16 architectures (s390x, powerpc, ppc64, sparc64, hppa):
17
18 BinaryDictTest.AcceptsLegacy64BitLayout
19 BinaryDictTest.RejectsHugeValueTotalLength
20 DartsDictTest.LoadsLegacy64bitFormat
21 DartsDictTest.Legacy64PrefixSearchBeyond64Matches
22 DartsDictTest.Legacy64SerializeToFileThrows
23
24 Applied-Upstream: https://github.com/BYVoid/OpenCC/commit/8aeb5e69cbd9c7938f02989f68766723a749242c
25 Last-Update: 2026-08-04
26 ---
27 diff --git a/src/BinaryDict.cpp b/src/BinaryDict.cpp
28 --- a/src/BinaryDict.cpp
29 +++ b/src/BinaryDict.cpp
30 @@ -71,14 +71,12 @@ uint64_t ReadField(const char* data, size_t size, size_t* offset,
31 // non-zero whenever the dictionary has entries, and an empty dictionary
32 // is exactly 12 bytes long.
33 // - Legacy 64-bit layout: the first field is an 8-byte numItems whose high
34 -// 4 bytes are zero for any realistic dictionary.
35 +// 32 bits are zero for any realistic dictionary.
36 size_t DetectFieldWidth(const char* data, size_t size) {
37 if (size < 8) {
38 return sizeof(uint32_t);
39 }
40 - const bool highBytesZero =
41 - data[4] == 0 && data[5] == 0 && data[6] == 0 && data[7] == 0;
42 - if (highBytesZero && size != 3 * sizeof(uint32_t)) {
43 + if (LooksLikeLegacy64Field(data) && size != 3 * sizeof(uint32_t)) {
44 return sizeof(uint64_t);
45 }
46 return sizeof(uint32_t);
47 diff --git a/src/BinaryDict.hpp b/src/BinaryDict.hpp
48 --- a/src/BinaryDict.hpp
49 +++ b/src/BinaryDict.hpp
50 @@ -18,10 +18,28 @@
51
52 #pragma once
53
54 +#include <cstdint>
55 +#include <cstring>
56 +
57 #include "Common.hpp"
58 #include "SerializableDict.hpp"
59
60 namespace opencc {
61 +// Returns true if the 8 bytes at `bytes` look like an 8-byte integer field
62 +// from the legacy 64-bit OCD layout rather than two fixed-width uint32_t
63 +// fields. Legacy files were written with native size_t fields in native byte
64 +// order, so the probe is loaded as a native uint64_t: a legacy field has
65 +// zero high 32 bits, while for a fixed-width file the high half is one of
66 +// the two uint32 fields (or the start of the darts array), non-zero for any
67 +// realistic dictionary. Works on both little- and big-endian hosts; legacy
68 +// files themselves remain readable only on hosts of the byte order that
69 +// wrote them.
70 +inline bool LooksLikeLegacy64Field(const void* bytes) {
71 + uint64_t word;
72 + std::memcpy(&word, bytes, sizeof(word));
73 + return (word >> 32) == 0;
74 +}
75 +
76 /**
77 * Binary dictionary for faster deserialization
78 * @ingroup opencc_cpp_api
79 diff --git a/src/DartsDict.cpp b/src/DartsDict.cpp
80 --- a/src/DartsDict.cpp
81 +++ b/src/DartsDict.cpp
82 @@ -234,20 +234,19 @@ DartsDictPtr DartsDict::NewFromFile(FILE* fp) {
83 ? static_cast<size_t>(fileEnd - currentOffset)
84 : 0;
85
86 - // Detect the dartsSize field width by reading 8 bytes and checking
87 - // whether bytes [4..7] are all zero.
88 + // Detect the dartsSize field width by reading 8 bytes.
89 // - Old 64-bit build: dartsSize field is uint64_t (8 bytes); high 32 bits
90 - // are zero for any realistic file size → bytes [4..7] == 0.
91 + // are zero for any realistic file size.
92 // - Current build (any word size) or old 32-bit build: dartsSize field is
93 - // uint32_t (4 bytes); bytes [4..7] are the first 4 bytes of the darts
94 - // array (non-zero for any valid array whose root unit has a non-zero
95 - // offset).
96 + // uint32_t (4 bytes); the other half of the probe is either the first
97 + // 4 bytes of the darts array (non-zero for any valid array whose root
98 + // unit has a non-zero offset) or dartsSize itself, so the native
99 + // uint64_t load has non-zero high bits.
100 uint8_t probe[8];
101 if (fread(probe, 1, 8, fp) != 8) {
102 throw InvalidFormat("Invalid OpenCC dictionary header (dartsSize)");
103 }
104 - bool is64bit =
105 - (probe[4] == 0 && probe[5] == 0 && probe[6] == 0 && probe[7] == 0);
106 + bool is64bit = LooksLikeLegacy64Field(probe);
107
108 if (is64bit) {
109 uint64_t dartsSize64;
110 @@ -340,8 +339,7 @@ DartsDictPtr DartsDict::NewFromBuffer(const char* data, size_t size) {
111 uint8_t probe[8];
112 memcpy(probe, data + offset, 8);
113 offset += 8;
114 - bool is64bit =
115 - (probe[4] == 0 && probe[5] == 0 && probe[6] == 0 && probe[7] == 0);
116 + bool is64bit = LooksLikeLegacy64Field(probe);
117
118 if (is64bit) {
119 uint64_t dartsSize64;