Skip to content

Commit 57a4bc8

Browse files
Federico FrascarelliiText-CI
authored andcommitted
Quit the serialization when there is an object referencing itself in SmartModePdfObjectsSerializer
Test for the corresponding case was added to functional-tests repo: KERNEL_SmartMode_XObjectSelfReference#xObjectIterativeReference with input file name "checkboxes_XObject_iterative_reference.pdf". DEVSIX-2137 Autoported commit. Original commit hash: [680a4df24]
1 parent fca6834 commit 57a4bc8

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

itext/itext.kernel/itext/kernel/pdf/SmartModePdfObjectsSerializer.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,21 @@ public virtual SerializedObjectContent SerializeObject(PdfObject obj) {
8787
if (content == null) {
8888
ByteBuffer bb = new ByteBuffer();
8989
int level = 100;
90-
SerObject(obj, bb, level, serializedCache);
90+
try {
91+
SerObject(obj, bb, level, serializedCache);
92+
}
93+
catch (SmartModePdfObjectsSerializer.SelfReferenceException) {
94+
return null;
95+
}
9196
content = bb.ToByteArray();
9297
}
9398
return new SerializedObjectContent(content);
9499
}
95100

101+
private class SelfReferenceException : Exception {
102+
}
103+
104+
/// <exception cref="iText.Kernel.Pdf.SmartModePdfObjectsSerializer.SelfReferenceException"/>
96105
private void SerObject(PdfObject obj, ByteBuffer bb, int level, IDictionary<PdfIndirectReference, byte[]>
97106
serializedCache) {
98107
if (level <= 0) {
@@ -112,6 +121,11 @@ private void SerObject(PdfObject obj, ByteBuffer bb, int level, IDictionary<PdfI
112121
return;
113122
}
114123
else {
124+
if (serializedCache.Keys.Contains(reference)) {
125+
//referencing itself
126+
throw new SmartModePdfObjectsSerializer.SelfReferenceException();
127+
}
128+
serializedCache.Put(reference, null);
115129
savedBb = bb;
116130
bb = new ByteBuffer();
117131
obj = reference.GetRefersTo();
@@ -155,6 +169,7 @@ private void SerObject(PdfObject obj, ByteBuffer bb, int level, IDictionary<PdfI
155169
}
156170
}
157171

172+
/// <exception cref="iText.Kernel.Pdf.SmartModePdfObjectsSerializer.SelfReferenceException"/>
158173
private void SerDic(PdfDictionary dic, ByteBuffer bb, int level, IDictionary<PdfIndirectReference, byte[]>
159174
serializedCache) {
160175
bb.Append("$D");
@@ -171,6 +186,7 @@ private void SerDic(PdfDictionary dic, ByteBuffer bb, int level, IDictionary<Pdf
171186
bb.Append("$\\D");
172187
}
173188

189+
/// <exception cref="iText.Kernel.Pdf.SmartModePdfObjectsSerializer.SelfReferenceException"/>
174190
private void SerArray(PdfArray array, ByteBuffer bb, int level, IDictionary<PdfIndirectReference, byte[]>
175191
serializedCache) {
176192
bb.Append("$A");

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3f9bf568b8609a5307de7de96bbb5c1765a84394
1+
680a4df24ec910b99f69f92ed2867a2bd25a494a

0 commit comments

Comments
 (0)