From dcdb14c5f2711c0fc6d5e3c268e7c0113f081564 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Tue, 28 Dec 2021 10:41:06 +0000 Subject: [PATCH] close cursors in finally blocks git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896471 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xssf/usermodel/XSSFGraphicFrame.java | 11 +- .../poi/xssf/usermodel/XSSFObjectData.java | 13 +- .../xssf/usermodel/XSSFRichTextString.java | 9 +- .../poi/xssf/usermodel/XSSFTableStyle.java | 40 ++++--- .../poi/xssf/usermodel/XSSFVMLDrawing.java | 112 +++++++++++------- .../usermodel/helpers/XSSFPasswordHelper.java | 108 +++++++++-------- .../XWPFAbstractFootnoteEndnote.java | 66 ++++++----- .../apache/poi/xwpf/usermodel/XWPFFooter.java | 68 ++++++----- 8 files changed, 243 insertions(+), 184 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFGraphicFrame.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFGraphicFrame.java index 8ba7848484..a62253aa32 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFGraphicFrame.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFGraphicFrame.java @@ -193,10 +193,13 @@ public final class XSSFGraphicFrame extends XSSFShape { String r_namespaceUri = STRelationshipId.type.getName().getNamespaceURI(); String c_namespaceUri = XSSFDrawing.NAMESPACE_C; XmlCursor cursor = data.newCursor(); - cursor.toNextToken(); - cursor.beginElement(new QName(c_namespaceUri, "chart", "c")); - cursor.insertAttributeWithValue(new QName(r_namespaceUri, "id", "r"), id); - cursor.dispose(); + try { + cursor.toNextToken(); + cursor.beginElement(new QName(c_namespaceUri, "chart", "c")); + cursor.insertAttributeWithValue(new QName(r_namespaceUri, "id", "r"), id); + } finally { + cursor.dispose(); + } data.setUri(c_namespaceUri); } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFObjectData.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFObjectData.java index 7001c88b5c..0aee53f44c 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFObjectData.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFObjectData.java @@ -83,11 +83,14 @@ public class XSSFObjectData extends XSSFSimpleShape implements ObjectData { CTOfficeArtExtension ext = extLst.addNewExt(); ext.setUri("{63B3BB69-23CF-44E3-9099-C40C66FF867C}"); XmlCursor cur = ext.newCursor(); - cur.toEndToken(); - cur.beginElement(new QName(drawNS, "compatExt", "a14")); - cur.insertNamespace("a14", drawNS); - cur.insertAttributeWithValue("spid", "_x0000_s1"); - cur.dispose(); + try { + cur.toEndToken(); + cur.beginElement(new QName(drawNS, "compatExt", "a14")); + cur.insertNamespace("a14", drawNS); + cur.insertAttributeWithValue("spid", "_x0000_s1"); + } finally { + cur.dispose(); + } nv.addNewCNvSpPr(); diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java index 3210dabebe..a88fae862c 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java @@ -490,9 +490,12 @@ public class XSSFRichTextString implements RichTextString { char lastChar = text.charAt(text.length() - 1); if(Character.isWhitespace(firstChar) || Character.isWhitespace(lastChar)) { XmlCursor c = xs.newCursor(); - c.toNextToken(); - c.insertAttributeWithValue(new QName("http://www.w3.org/XML/1998/namespace", "space"), "preserve"); - c.dispose(); + try { + c.toNextToken(); + c.insertAttributeWithValue(new QName("http://www.w3.org/XML/1998/namespace", "space"), "preserve"); + } finally { + c.dispose(); + } } } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTableStyle.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTableStyle.java index 6497f727b4..5b708d4f6c 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTableStyle.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTableStyle.java @@ -59,27 +59,31 @@ public class XSSFTableStyle implements TableStyle { List dxfList = new ArrayList<>(); // CT* classes don't handle "mc:AlternateContent" elements, so get the Dxf instances manually - XmlCursor cur = dxfs.newCursor(); - // sometimes there are namespaces sometimes not. - String xquery = "declare namespace x='"+XSSFRelation.NS_SPREADSHEETML+"' .//x:dxf | .//dxf"; - cur.selectPath(xquery); - while (cur.toNextSelection()) { - XmlObject obj = cur.getObject(); - String parentName = obj.getDomNode().getParentNode().getNodeName(); - // ignore alternate content choices, we won't know anything about their namespaces - if (parentName.equals("mc:Fallback") || parentName.equals("x:dxfs") || parentName.contentEquals("dxfs")) { - CTDxf dxf; - try { - if (obj instanceof CTDxf) { - dxf = (CTDxf) obj; - } else { - dxf = CTDxf.Factory.parse(obj.newXMLStreamReader(), new XmlOptions().setDocumentType(CTDxf.type)); + final XmlCursor cur = dxfs.newCursor(); + try { + // sometimes there are namespaces sometimes not. + String xquery = "declare namespace x='"+XSSFRelation.NS_SPREADSHEETML+"' .//x:dxf | .//dxf"; + cur.selectPath(xquery); + while (cur.toNextSelection()) { + XmlObject obj = cur.getObject(); + String parentName = obj.getDomNode().getParentNode().getNodeName(); + // ignore alternate content choices, we won't know anything about their namespaces + if (parentName.equals("mc:Fallback") || parentName.equals("x:dxfs") || parentName.contentEquals("dxfs")) { + CTDxf dxf; + try { + if (obj instanceof CTDxf) { + dxf = (CTDxf) obj; + } else { + dxf = CTDxf.Factory.parse(obj.newXMLStreamReader(), new XmlOptions().setDocumentType(CTDxf.type)); + } + if (dxf != null) dxfList.add(dxf); + } catch (XmlException e) { + LOG.atWarn().withThrowable(e).log("Error parsing XSSFTableStyle"); } - if (dxf != null) dxfList.add(dxf); - } catch (XmlException e) { - LOG.atWarn().withThrowable(e).log("Error parsing XSSFTableStyle"); } } + } finally { + cur.dispose(); } for (CTTableStyleElement element : tableStyle.getTableStyleElementList()) { diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFVMLDrawing.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFVMLDrawing.java index ba8d5849eb..2ed99ad900 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFVMLDrawing.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFVMLDrawing.java @@ -207,36 +207,46 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart { */ private void newDrawing(){ root = XmlDocument.Factory.newInstance(); - XmlCursor xml = root.addNewXml().newCursor(); - - ShapelayoutDocument layDoc = ShapelayoutDocument.Factory.newInstance(); - CTShapeLayout layout = layDoc.addNewShapelayout(); - layout.setExt(STExt.EDIT); - CTIdMap idmap = layout.addNewIdmap(); - idmap.setExt(STExt.EDIT); - idmap.setData("1"); + final XmlCursor xml = root.addNewXml().newCursor(); + try { + ShapelayoutDocument layDoc = ShapelayoutDocument.Factory.newInstance(); + CTShapeLayout layout = layDoc.addNewShapelayout(); + layout.setExt(STExt.EDIT); + CTIdMap idmap = layout.addNewIdmap(); + idmap.setExt(STExt.EDIT); + idmap.setData("1"); + + xml.toEndToken(); + final XmlCursor layCur = layDoc.newCursor(); + try { + layCur.copyXmlContents(xml); + } finally { + layCur.dispose(); + } - xml.toEndToken(); - XmlCursor layCur = layDoc.newCursor(); - layCur.copyXmlContents(xml); - layCur.dispose(); + CTGroup grp = CTGroup.Factory.newInstance(); + CTShapetype shapetype = grp.addNewShapetype(); + _shapeTypeId = COMMENT_SHAPE_TYPE_ID; + shapetype.setId(_shapeTypeId); + shapetype.setCoordsize("21600,21600"); + shapetype.setSpt(202); + shapetype.setPath2("m,l,21600r21600,l21600,xe"); + shapetype.addNewStroke().setJoinstyle(STStrokeJoinStyle.MITER); + CTPath path = shapetype.addNewPath(); + path.setGradientshapeok(STTrueFalse.T); + path.setConnecttype(STConnectType.RECT); + + xml.toEndToken(); + final XmlCursor grpCur = grp.newCursor(); + try { + grpCur.copyXmlContents(xml); + } finally { + grpCur.dispose(); + } + } finally { + xml.dispose(); + } - CTGroup grp = CTGroup.Factory.newInstance(); - CTShapetype shapetype = grp.addNewShapetype(); - _shapeTypeId = COMMENT_SHAPE_TYPE_ID; - shapetype.setId(_shapeTypeId); - shapetype.setCoordsize("21600,21600"); - shapetype.setSpt(202); - shapetype.setPath2("m,l,21600r21600,l21600,xe"); - shapetype.addNewStroke().setJoinstyle(STStrokeJoinStyle.MITER); - CTPath path = shapetype.addNewPath(); - path.setGradientshapeok(STTrueFalse.T); - path.setConnecttype(STConnectType.RECT); - - xml.toEndToken(); - XmlCursor grpCur = grp.newCursor(); - grpCur.copyXmlContents(xml); - grpCur.dispose(); } /** @@ -269,13 +279,19 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart { cldata.addNewColumn().setBigIntegerValue(BigInteger.valueOf(0)); XmlCursor xml = root.getXml().newCursor(); - xml.toEndToken(); - XmlCursor grpCur = grp.newCursor(); - grpCur.copyXmlContents(xml); - xml.toPrevSibling(); - shape = (CTShape)xml.getObject(); - grpCur.dispose(); - xml.dispose(); + try { + xml.toEndToken(); + XmlCursor grpCur = grp.newCursor(); + try { + grpCur.copyXmlContents(xml); + xml.toPrevSibling(); + shape = (CTShape)xml.getObject(); + } finally { + grpCur.dispose(); + } + } finally { + xml.dispose(); + } return shape; } @@ -287,11 +303,15 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart { */ public CTShape findCommentShape(int row, int col){ XmlCursor cur = root.getXml().newCursor(); - for (boolean found = cur.toFirstChild(); found; found = cur.toNextSibling()) { - XmlObject itm = cur.getObject(); - if (matchCommentShape(itm, row, col)) { - return (CTShape)itm; + try { + for (boolean found = cur.toFirstChild(); found; found = cur.toNextSibling()) { + XmlObject itm = cur.getObject(); + if (matchCommentShape(itm, row, col)) { + return (CTShape)itm; + } } + } finally { + cur.dispose(); } return null; } @@ -318,12 +338,16 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart { protected boolean removeCommentShape(int row, int col){ XmlCursor cur = root.getXml().newCursor(); - for (boolean found = cur.toFirstChild(); found; found = cur.toNextSibling()) { - XmlObject itm = cur.getObject(); - if (matchCommentShape(itm, row, col)) { - cur.removeXml(); - return true; + try { + for (boolean found = cur.toFirstChild(); found; found = cur.toNextSibling()) { + XmlObject itm = cur.getObject(); + if (matchCommentShape(itm, row, col)) { + cur.removeXml(); + return true; + } } + } finally { + cur.dispose(); } return false; } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/helpers/XSSFPasswordHelper.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/helpers/XSSFPasswordHelper.java index 2933bc8c44..fafac08ee9 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/helpers/XSSFPasswordHelper.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/helpers/XSSFPasswordHelper.java @@ -50,41 +50,43 @@ public final class XSSFPasswordHelper { */ public static void setPassword(XmlObject xobj, String password, HashAlgorithm hashAlgo, String prefix) { XmlCursor cur = xobj.newCursor(); + try { + if (password == null) { + cur.removeAttribute(getAttrName(prefix, "password")); + cur.removeAttribute(getAttrName(prefix, "algorithmName")); + cur.removeAttribute(getAttrName(prefix, "hashValue")); + cur.removeAttribute(getAttrName(prefix, "saltValue")); + cur.removeAttribute(getAttrName(prefix, "spinCount")); + return; + } - if (password == null) { - cur.removeAttribute(getAttrName(prefix, "password")); - cur.removeAttribute(getAttrName(prefix, "algorithmName")); - cur.removeAttribute(getAttrName(prefix, "hashValue")); - cur.removeAttribute(getAttrName(prefix, "saltValue")); - cur.removeAttribute(getAttrName(prefix, "spinCount")); - return; - } - - cur.toFirstContentToken(); - if (hashAlgo == null) { - int hash = CryptoFunctions.createXorVerifier1(password); - cur.insertAttributeWithValue(getAttrName(prefix, "password"), - String.format(Locale.ROOT, "%04X", hash).toUpperCase(Locale.ROOT)); - } else { - byte[] salt = RandomSingleton.getInstance().generateSeed(16); - - // Iterations specifies the number of times the hashing function shall be iteratively run (using each - // iteration's result as the input for the next iteration). - int spinCount = 100000; + cur.toFirstContentToken(); + if (hashAlgo == null) { + int hash = CryptoFunctions.createXorVerifier1(password); + cur.insertAttributeWithValue(getAttrName(prefix, "password"), + String.format(Locale.ROOT, "%04X", hash).toUpperCase(Locale.ROOT)); + } else { + byte[] salt = RandomSingleton.getInstance().generateSeed(16); + + // Iterations specifies the number of times the hashing function shall be iteratively run (using each + // iteration's result as the input for the next iteration). + int spinCount = 100000; - // Implementation Notes List: - // --> In this third stage, the reversed byte order legacy hash from the second stage shall - // be converted to Unicode hex string representation - byte[] hash = CryptoFunctions.hashPassword(password, hashAlgo, salt, spinCount, false); + // Implementation Notes List: + // --> In this third stage, the reversed byte order legacy hash from the second stage shall + // be converted to Unicode hex string representation + byte[] hash = CryptoFunctions.hashPassword(password, hashAlgo, salt, spinCount, false); - Base64.Encoder enc64 = Base64.getEncoder(); + Base64.Encoder enc64 = Base64.getEncoder(); - cur.insertAttributeWithValue(getAttrName(prefix, "algorithmName"), hashAlgo.jceId); - cur.insertAttributeWithValue(getAttrName(prefix, "hashValue"), enc64.encodeToString(hash)); - cur.insertAttributeWithValue(getAttrName(prefix, "saltValue"), enc64.encodeToString(salt)); - cur.insertAttributeWithValue(getAttrName(prefix, "spinCount"), ""+spinCount); + cur.insertAttributeWithValue(getAttrName(prefix, "algorithmName"), hashAlgo.jceId); + cur.insertAttributeWithValue(getAttrName(prefix, "hashValue"), enc64.encodeToString(hash)); + cur.insertAttributeWithValue(getAttrName(prefix, "saltValue"), enc64.encodeToString(salt)); + cur.insertAttributeWithValue(getAttrName(prefix, "spinCount"), ""+spinCount); + } + } finally { + cur.dispose(); } - cur.dispose(); } /** @@ -103,30 +105,32 @@ public final class XSSFPasswordHelper { if (password == null) return false; XmlCursor cur = xobj.newCursor(); - String xorHashVal = cur.getAttributeText(getAttrName(prefix, "password")); - String algoName = cur.getAttributeText(getAttrName(prefix, "algorithmName")); - String hashVal = cur.getAttributeText(getAttrName(prefix, "hashValue")); - String saltVal = cur.getAttributeText(getAttrName(prefix, "saltValue")); - String spinCount = cur.getAttributeText(getAttrName(prefix, "spinCount")); - cur.dispose(); + try { + String xorHashVal = cur.getAttributeText(getAttrName(prefix, "password")); + String algoName = cur.getAttributeText(getAttrName(prefix, "algorithmName")); + String hashVal = cur.getAttributeText(getAttrName(prefix, "hashValue")); + String saltVal = cur.getAttributeText(getAttrName(prefix, "saltValue")); + String spinCount = cur.getAttributeText(getAttrName(prefix, "spinCount")); + if (xorHashVal != null) { + int hash1 = Integer.parseInt(xorHashVal, 16); + int hash2 = CryptoFunctions.createXorVerifier1(password); + return hash1 == hash2; + } else { + if (hashVal == null || algoName == null || saltVal == null || spinCount == null) { + return false; + } - if (xorHashVal != null) { - int hash1 = Integer.parseInt(xorHashVal, 16); - int hash2 = CryptoFunctions.createXorVerifier1(password); - return hash1 == hash2; - } else { - if (hashVal == null || algoName == null || saltVal == null || spinCount == null) { - return false; - } + Base64.Decoder dec64 = Base64.getDecoder(); - Base64.Decoder dec64 = Base64.getDecoder(); - - byte[] hash1 = dec64.decode(hashVal); - HashAlgorithm hashAlgo = HashAlgorithm.fromString(algoName); - byte[] salt = dec64.decode(saltVal); - int spinCnt = Integer.parseInt(spinCount); - byte[] hash2 = CryptoFunctions.hashPassword(password, hashAlgo, salt, spinCnt, false); - return Arrays.equals(hash1, hash2); + byte[] hash1 = dec64.decode(hashVal); + HashAlgorithm hashAlgo = HashAlgorithm.fromString(algoName); + byte[] salt = dec64.decode(saltVal); + int spinCnt = Integer.parseInt(spinCount); + byte[] hash2 = CryptoFunctions.hashPassword(password, hashAlgo, salt, spinCnt, false); + return Arrays.equals(hash1, hash2); + } + } finally { + cur.dispose(); } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFAbstractFootnoteEndnote.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFAbstractFootnoteEndnote.java index e1e0df9bb3..7c97b908cd 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFAbstractFootnoteEndnote.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFAbstractFootnoteEndnote.java @@ -73,26 +73,29 @@ public abstract class XWPFAbstractFootnoteEndnote implements Iterable