Browse Source

#61809 - Infinite loop in SectionIDMap.get() and .put()

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1816205 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_4_0_0_FINAL
Andreas Beeker 6 years ago
parent
commit
cbfb179254

+ 7
- 3
src/examples/src/org/apache/poi/hpsf/examples/WriteTitle.java View File

@@ -21,9 +21,13 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hpsf.*;
import org.apache.poi.hpsf.Property;
import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.hpsf.Section;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hpsf.Variant;
import org.apache.poi.hpsf.WritingNotSupportedException;
import org.apache.poi.hpsf.wellknown.PropertyIDMap;
import org.apache.poi.hpsf.wellknown.SectionIDMap;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
@@ -64,7 +68,7 @@ public class WriteTitle
/* Turn the property set into a summary information property. This is
* done by setting the format ID of its first section to
* SectionIDMap.SUMMARY_INFORMATION_ID. */
ms.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
ms.setFormatID(SummaryInformation.FORMAT_ID);

/* Create an empty property. */
final Property p = new Property();

+ 15
- 3
src/java/org/apache/poi/hpsf/DocumentSummaryInformation.java View File

@@ -25,7 +25,6 @@ import java.util.List;
import java.util.Map;

import org.apache.poi.hpsf.wellknown.PropertyIDMap;
import org.apache.poi.hpsf.wellknown.SectionIDMap;

/**
* Convenience class representing a DocumentSummary Information stream in a
@@ -34,6 +33,7 @@ import org.apache.poi.hpsf.wellknown.SectionIDMap;
* @see SummaryInformation
*/
public class DocumentSummaryInformation extends PropertySet {

/**
* The document name a document summary information stream
* usually has in a POIFS filesystem.
@@ -41,6 +41,18 @@ public class DocumentSummaryInformation extends PropertySet {
public static final String DEFAULT_STREAM_NAME =
"\005DocumentSummaryInformation";

/**
* The DocumentSummaryInformation's first and second sections' format ID.
*/
private static final ClassID DOC_SUMMARY_INFORMATION =
new ClassID("{D5CDD502-2E9C-101B-9397-08002B2CF9AE}");
private static final ClassID USER_DEFINED_PROPERTIES =
new ClassID("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}");
public static final ClassID[] FORMAT_ID = {
DOC_SUMMARY_INFORMATION, USER_DEFINED_PROPERTIES
};
@Override
public PropertyIDMap getPropertySetIDMap() {
return PropertyIDMap.getDocumentSummaryInformationProperties();
@@ -51,7 +63,7 @@ public class DocumentSummaryInformation extends PropertySet {
* Creates an empty {@link DocumentSummaryInformation}.
*/
public DocumentSummaryInformation() {
getFirstSection().setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[0]);
getFirstSection().setFormatID(DOC_SUMMARY_INFORMATION);
}

@@ -812,7 +824,7 @@ public class DocumentSummaryInformation extends PropertySet {
private void ensureSection2() {
if (getSectionCount() < 2) {
Section s2 = new Section();
s2.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[1]);
s2.setFormatID(USER_DEFINED_PROPERTIES);
addSection(s2);
}
}

+ 2
- 3
src/java/org/apache/poi/hpsf/PropertySet.java View File

@@ -29,7 +29,6 @@ import java.util.List;

import org.apache.poi.EmptyFileException;
import org.apache.poi.hpsf.wellknown.PropertyIDMap;
import org.apache.poi.hpsf.wellknown.SectionIDMap;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.Entry;
import org.apache.poi.util.CodePageUtil;
@@ -659,7 +658,7 @@ public class PropertySet {
* represents a Summary Information, else {@code false}.
*/
public boolean isSummaryInformation() {
return !sections.isEmpty() && matchesSummary(getFirstSection().getFormatID(), SectionIDMap.SUMMARY_INFORMATION_ID);
return !sections.isEmpty() && matchesSummary(getFirstSection().getFormatID(), SummaryInformation.FORMAT_ID);
}

/**
@@ -669,7 +668,7 @@ public class PropertySet {
* represents a Document Summary Information, else {@code false}.
*/
public boolean isDocumentSummaryInformation() {
return !sections.isEmpty() && matchesSummary(getFirstSection().getFormatID(), SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID);
return !sections.isEmpty() && matchesSummary(getFirstSection().getFormatID(), DocumentSummaryInformation.FORMAT_ID);
}
/* package */ static boolean matchesSummary(ClassID actual, ClassID... expected) {

+ 2
- 3
src/java/org/apache/poi/hpsf/PropertySetFactory.java View File

@@ -22,7 +22,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.poi.hpsf.wellknown.SectionIDMap;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
@@ -110,9 +109,9 @@ public class PropertySetFactory {
stream.reset();
ClassID clsId = new ClassID(clsIdBuf, 0);
if (sectionCount > 0 && PropertySet.matchesSummary(clsId, SectionIDMap.SUMMARY_INFORMATION_ID)) {
if (sectionCount > 0 && PropertySet.matchesSummary(clsId, SummaryInformation.FORMAT_ID)) {
return new SummaryInformation(stream);
} else if (sectionCount > 0 && PropertySet.matchesSummary(clsId, SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID)) {
} else if (sectionCount > 0 && PropertySet.matchesSummary(clsId, DocumentSummaryInformation.FORMAT_ID)) {
return new DocumentSummaryInformation(stream);
} else {
return new PropertySet(stream);

+ 17
- 14
src/java/org/apache/poi/hpsf/Section.java View File

@@ -31,7 +31,6 @@ import java.util.TreeMap;

import org.apache.commons.collections4.bidimap.TreeBidiMap;
import org.apache.poi.hpsf.wellknown.PropertyIDMap;
import org.apache.poi.hpsf.wellknown.SectionIDMap;
import org.apache.poi.util.CodePageUtil;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndian;
@@ -597,29 +596,33 @@ public class Section {

/**
* Returns the PID string associated with a property ID. The ID
* is first looked up in the {@link Section}'s private
* dictionary. If it is not found there, the method calls {@link
* SectionIDMap#getPIDString}.
* is first looked up in the {@link Section Sections} private dictionary.
* If it is not found there, the property PID string is taken
* from sections format IDs namespace.
* If the PID is also undefined there, i.e. it is not well-known,
* {@code "[undefined]"} is returned.
*
* @param pid The property ID
*
* @return The property ID's string value
* @return The well-known property ID string associated with the
* property ID {@code pid}
*/
public String getPIDString(final long pid) {
String s = null;
Map<Long,String> dic = getDictionary();
if (dic != null) {
s = dic.get(pid);
}
if (s == null) {
s = SectionIDMap.getPIDString(getFormatID(), pid);
if (dic == null || !dic.containsKey(pid)) {
ClassID fmt = getFormatID();
if (SummaryInformation.FORMAT_ID.equals(fmt)) {
dic = PropertyIDMap.getSummaryInformationProperties();
} else if (DocumentSummaryInformation.FORMAT_ID[0].equals(fmt)) {
dic = PropertyIDMap.getDocumentSummaryInformationProperties();
}
}
return s;
return (dic != null && dic.containsKey(pid)) ? dic.get(pid) : PropertyIDMap.UNDEFINED;
}

/**
* Removes all properties from the section including 0 (dictionary) and
* 1 (codepage).
* Removes all properties from the section including 0 (dictionary) and 1 (codepage).
*/
public void clear() {
for (Property p : getProperties()) {

+ 11
- 3
src/java/org/apache/poi/hpsf/SummaryInformation.java View File

@@ -23,7 +23,6 @@ import java.io.UnsupportedEncodingException;
import java.util.Date;

import org.apache.poi.hpsf.wellknown.PropertyIDMap;
import org.apache.poi.hpsf.wellknown.SectionIDMap;

/**
* Convenience class representing a Summary Information stream in a
@@ -38,6 +37,13 @@ public final class SummaryInformation extends PropertySet {
*/
public static final String DEFAULT_STREAM_NAME = "\005SummaryInformation";

/**
* The SummaryInformation's section's format ID.
*/
public static final ClassID FORMAT_ID =
new ClassID("{F29F85E0-4FF9-1068-AB91-08002B27B3D9}");

@Override
public PropertyIDMap getPropertySetIDMap() {
return PropertyIDMap.getSummaryInformationProperties();
}
@@ -46,7 +52,7 @@ public final class SummaryInformation extends PropertySet {
* Creates an empty {@link SummaryInformation}.
*/
public SummaryInformation() {
getFirstSection().setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
getFirstSection().setFormatID(FORMAT_ID);
}
/**
@@ -594,7 +600,9 @@ public final class SummaryInformation extends PropertySet {
*/
public Thumbnail getThumbnailThumbnail() {
byte[] data = getThumbnail();
if (data == null) return null;
if (data == null) {
return null;
}
return new Thumbnail(data);
}


+ 5
- 0
src/java/org/apache/poi/hpsf/wellknown/PropertyIDMap.java View File

@@ -316,6 +316,11 @@ public class PropertyIDMap implements Map<Long,String> {
*/
public static final int PID_BEHAVIOUR = 0x80000003;
/**
* A property without a known name is described by this string.
*/
public static final String UNDEFINED = "[undefined]";
/**
* Contains the summary information property ID values and
* associated strings. See the overall HPSF documentation for

+ 22
- 114
src/java/org/apache/poi/hpsf/wellknown/SectionIDMap.java View File

@@ -17,136 +17,44 @@

package org.apache.poi.hpsf.wellknown;

import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hpsf.ClassID;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.util.Internal;
import org.apache.poi.util.Removal;

/**
* <p>Maps section format IDs to {@link PropertyIDMap}s. It is
* initialized with two well-known section format IDs: those of the
* <tt>\005SummaryInformation</tt> stream and the
* <tt>\005DocumentSummaryInformation</tt> stream.</p>
*
* <p>If you have a section format ID you can use it as a key to query
* this map. If you get a {@link PropertyIDMap} returned your section
* is well-known and you can query the {@link PropertyIDMap} for PID
* strings. If you get back <code>null</code> you are on your own.</p>
*
* <p>This {@link java.util.Map} expects the byte arrays of section format IDs
* as keys. A key maps to a {@link PropertyIDMap} describing the
* property IDs in sections with the specified section format ID.</p>
* This classed used to map section format IDs to {@link PropertyIDMap PropertyIDMaps},
* but there's no way to use custom PropertyIDMaps.<p>
*
* It is only kept for its ClassIDs until removal.
*
* @deprecated in 4.0.0, there's no way to create custom PropertyIDMaps, therefore
* this class is obsolete
*/
@Internal
@Deprecated
@Removal(version="4.2.0")
public class SectionIDMap {

/**
* The default section ID map. It maps section format IDs to {@link PropertyIDMap PropertyIDMaps}
*/
private static ThreadLocal<Map<ClassID,PropertyIDMap>> defaultMap =
new ThreadLocal<>();
/**
* <p>The SummaryInformation's section's format ID.</p>
* The SummaryInformation's section's format ID.
* @deprecated use {@link SummaryInformation#FORMAT_ID}
*/
public static final ClassID SUMMARY_INFORMATION_ID =
new ClassID("{F29F85E0-4FF9-1068-AB91-08002B27B3D9}");
@Deprecated
public static final ClassID SUMMARY_INFORMATION_ID = SummaryInformation.FORMAT_ID;

/**
* The DocumentSummaryInformation's first and second sections' format ID.
* @deprecated use {@link DocumentSummaryInformation#FORMAT_ID}
*/
private static final ClassID DOC_SUMMARY_INFORMATION =
new ClassID("{D5CDD502-2E9C-101B-9397-08002B2CF9AE}");
private static final ClassID USER_DEFINED_PROPERTIES =
new ClassID("{D5CDD505-2E9C-101B-9397-08002B2CF9AE}");
public static final ClassID[] DOCUMENT_SUMMARY_INFORMATION_ID = {
DOC_SUMMARY_INFORMATION, USER_DEFINED_PROPERTIES
};
@Deprecated
public static final ClassID[] DOCUMENT_SUMMARY_INFORMATION_ID = DocumentSummaryInformation.FORMAT_ID;

/**
* A property without a known name is described by this string.
* @deprecated use {@link PropertyIDMap#UNDEFINED}
*/
public static final String UNDEFINED = "[undefined]";

/**
* <p>Returns the singleton instance of the default {@link
* SectionIDMap}.</p>
*
* @return The instance value
*/
public static SectionIDMap getInstance() {
Map<ClassID,PropertyIDMap> m = defaultMap.get();
if (m == null) {
m = new HashMap<>();
m.put(SUMMARY_INFORMATION_ID, PropertyIDMap.getSummaryInformationProperties());
m.put(DOCUMENT_SUMMARY_INFORMATION_ID[0], PropertyIDMap.getDocumentSummaryInformationProperties());
defaultMap.set(m);
}
return new SectionIDMap();
}



/**
* <p>Returns the property ID string that is associated with a
* given property ID in a section format ID's namespace.</p>
*
* @param sectionFormatID Each section format ID has its own name
* space of property ID strings and thus must be specified.
* @param pid The property ID
* @return The well-known property ID string associated with the
* property ID <var>pid</var> in the name space spanned by <var>
* sectionFormatID</var> . If the <var>pid</var>
* /<var>sectionFormatID </var> combination is not well-known, the
* string "[undefined]" is returned.
*/
public static String getPIDString(ClassID sectionFormatID, long pid) {
final PropertyIDMap m = getInstance().get(sectionFormatID);
if (m == null) {
return UNDEFINED;
}
final String s = m.get(pid);
if (s == null) {
return UNDEFINED;
}
return s;
}



/**
* <p>Returns the {@link PropertyIDMap} for a given section format
* ID.</p>
*
* @param sectionFormatID the section format ID
* @return the property ID map
*/
public PropertyIDMap get(final ClassID sectionFormatID) {
return getInstance().get(sectionFormatID);
}

/**
* Associates a section format ID with a {@link PropertyIDMap}.
*
* @param sectionFormatID the section format ID
* @param propertyIDMap the property ID map
* @return as defined by {@link java.util.Map#put}
*/
public PropertyIDMap put(ClassID sectionFormatID, PropertyIDMap propertyIDMap) {
return getInstance().put(sectionFormatID, propertyIDMap);
}

/**
* Associates the string representation of a section format ID with a {@link PropertyIDMap}
*
* @param key the key of the PropertyIDMap
* @param value the PropertyIDMap itself
*
* @return the previous PropertyIDMap stored under this key, or {@code null} if there wasn't one
*/
protected PropertyIDMap put(String key, PropertyIDMap value) {
return put(new ClassID(key), value);
}
@Deprecated
public static final String UNDEFINED = PropertyIDMap.UNDEFINED;
}

+ 20
- 4
src/testcases/org/apache/poi/hpsf/basic/TestBasic.java View File

@@ -41,7 +41,7 @@ import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.hpsf.Section;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hpsf.wellknown.SectionIDMap;
import org.apache.poi.hpsf.wellknown.PropertyIDMap;
import org.junit.Before;
import org.junit.Test;

@@ -171,13 +171,13 @@ public final class TestBasic {
final SummaryInformation si = (SummaryInformation)PropertySetFactory.create(is);
final List<Section> sections = si.getSections();
final Section s = sections.get(0);
assertEquals(s.getFormatID(), SectionIDMap.SUMMARY_INFORMATION_ID);
assertEquals(s.getFormatID(), SummaryInformation.FORMAT_ID);
assertNotNull(s.getProperties());
assertEquals(17, s.getPropertyCount());
assertEquals("Titel", s.getProperty(2));
assertEquals("Titel", s.getProperty(PropertyIDMap.PID_TITLE));
assertEquals(1764, s.getSize());
}
@Test
public void bug52117LastPrinted() throws IOException, HPSFException {
File f = samples.getFile("TestBug52117.doc");
@@ -189,4 +189,20 @@ public final class TestBasic {
assertTrue(Filetime.isUndefined(lastPrinted));
assertEquals(1800000000L, editTime);
}

@Test
public void bug61809() throws IOException, HPSFException {
InputStream is_si = new ByteArrayInputStream(poiFiles.get(0).getBytes());
final SummaryInformation si = (SummaryInformation)PropertySetFactory.create(is_si);
final Section s_si = si.getSections().get(0);

assertEquals("PID_TITLE", s_si.getPIDString(PropertyIDMap.PID_TITLE));
assertEquals(PropertyIDMap.UNDEFINED, s_si.getPIDString(4711));

InputStream is_dsi = new ByteArrayInputStream(poiFiles.get(1).getBytes());
final DocumentSummaryInformation dsi = (DocumentSummaryInformation)PropertySetFactory.create(is_dsi);
final Section s_dsi = dsi.getSections().get(0);

assertEquals("PID_MANAGER", s_dsi.getPIDString(PropertyIDMap.PID_MANAGER));
}
}

+ 21
- 14
src/testcases/org/apache/poi/hpsf/basic/TestWrite.java View File

@@ -42,9 +42,23 @@ import java.util.Locale;
import java.util.Map;

import org.apache.poi.POIDataSamples;
import org.apache.poi.hpsf.*;
import org.apache.poi.hpsf.ClassID;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.HPSFException;
import org.apache.poi.hpsf.IllegalPropertySetDataException;
import org.apache.poi.hpsf.NoFormatIDException;
import org.apache.poi.hpsf.NoPropertySetStreamException;
import org.apache.poi.hpsf.Property;
import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.hpsf.ReadingNotSupportedException;
import org.apache.poi.hpsf.Section;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hpsf.UnsupportedVariantTypeException;
import org.apache.poi.hpsf.Variant;
import org.apache.poi.hpsf.VariantSupport;
import org.apache.poi.hpsf.WritingNotSupportedException;
import org.apache.poi.hpsf.wellknown.PropertyIDMap;
import org.apache.poi.hpsf.wellknown.SectionIDMap;
import org.apache.poi.poifs.eventfilesystem.POIFSReader;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
@@ -72,13 +86,6 @@ public class TestWrite {

private static final String POI_FS = "TestHPSFWritingFunctionality.doc";

private static final int BYTE_ORDER = 0xfffe;
private static final int FORMAT = 0x0000;
private static final int OS_VERSION = 0x00020A04;
private static final int[] SECTION_COUNT = {1, 2};
private static final boolean[] IS_SUMMARY_INFORMATION = {true, false};
private static final boolean[] IS_DOCUMENT_SUMMARY_INFORMATION = {false, true};

private static final String IMPROPER_DEFAULT_CHARSET_MESSAGE =
"Your default character set is " + getDefaultCharsetName() +
". However, this testcase must be run in an environment " +
@@ -147,7 +154,7 @@ public class TestWrite {
final POIFSFileSystem poiFs = new POIFSFileSystem();
final PropertySet ps = new PropertySet();
final Section s = ps.getSections().get(0);
s.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
s.setFormatID(SummaryInformation.FORMAT_ID);

final ByteArrayOutputStream psStream = new ByteArrayOutputStream();
ps.write(psStream);
@@ -194,7 +201,7 @@ public class TestWrite {

final PropertySet ps = new PropertySet();
final Section si = new Section();
si.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
si.setFormatID(SummaryInformation.FORMAT_ID);
ps.clearSections();
ps.addSection(si);

@@ -414,7 +421,7 @@ public class TestWrite {
final String TITLE = "This is a sample title";
final PropertySet mps = new PropertySet();
final Section ms = mps.getSections().get(0);
ms.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
ms.setFormatID(SummaryInformation.FORMAT_ID);
final Property p = new Property();
p.setID(PropertyIDMap.PID_TITLE);
p.setType(Variant.VT_LPSTR);
@@ -485,7 +492,7 @@ public class TestWrite {
m.put(Long.valueOf(2), "String 2");
m.put(Long.valueOf(3), "String 3");
s.setDictionary(m);
s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[0]);
s.setFormatID(DocumentSummaryInformation.FORMAT_ID[0]);
int codepage = CodePageUtil.CP_UNICODE;
s.setProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2, codepage);
poiFs.createDocument(ps1.toInputStream(), "Test");
@@ -751,7 +758,7 @@ public class TestWrite {

try {
s.setDictionary(m);
s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[0]);
s.setFormatID(DocumentSummaryInformation.FORMAT_ID[0]);
int codepage = 12345;
s.setProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2,
Integer.valueOf(codepage));

+ 15
- 3
src/testcases/org/apache/poi/hpsf/basic/TestWriteWellKnown.java View File

@@ -34,8 +34,20 @@ import java.util.HashMap;
import java.util.Map;

import org.apache.poi.POIDataSamples;
import org.apache.poi.hpsf.*;
import org.apache.poi.hpsf.wellknown.SectionIDMap;
import org.apache.poi.hpsf.CustomProperties;
import org.apache.poi.hpsf.CustomProperty;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.MarkUnsupportedException;
import org.apache.poi.hpsf.NoPropertySetStreamException;
import org.apache.poi.hpsf.Property;
import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.hpsf.Section;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hpsf.UnexpectedPropertySetTypeException;
import org.apache.poi.hpsf.Variant;
import org.apache.poi.hpsf.VariantSupport;
import org.apache.poi.hpsf.WritingNotSupportedException;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.util.IOUtils;
@@ -562,7 +574,7 @@ public class TestWriteWellKnown {

/* Test an empty custom properties set. */
s = new Section();
s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[1]);
s.setFormatID(DocumentSummaryInformation.FORMAT_ID[1]);
// s.setCodepage(CodePageUtil.CP_UNICODE);
dsi.addSection(s);
cps = dsi.getCustomProperties();

Loading…
Cancel
Save