Skip to content

Commit

Permalink
poi: Avoid two possible file-handle leaks when opening files fails wi…
Browse files Browse the repository at this point in the history
…th an exception
  • Loading branch information
antony-liu committed Mar 12, 2024
1 parent 6eaf5ec commit 2127eb6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
20 changes: 10 additions & 10 deletions openxml4Net/OPC/OPCPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ public static OPCPackage Open(ZipEntrySource zipEntry)
// pack.originalPackagePath = file.AbsolutePath;
return pack;
}
catch (InvalidFormatException e)
catch (InvalidFormatException)
{
IOUtils.CloseQuietly(pack);
throw;
}
catch (RuntimeException e)
catch (RuntimeException)
{
IOUtils.CloseQuietly(pack);
throw;
Expand All @@ -208,8 +208,8 @@ public static OPCPackage Open(ZipEntrySource zipEntry)
* @throws InvalidFormatException
* If the specified file doesn't exist, and a parsing error
* occur.
* @throws InvalidOperationException If the zip file cannot be opened.
* @throws InvalidFormatException if the package is not valid.
* @throws InvalidOperationException If the zip file cannot be opened.
* @throws InvalidFormatException if the package is not valid.
*/
public static OPCPackage Open(String path, PackageAccess access)
{
Expand Down Expand Up @@ -271,12 +271,12 @@ public static OPCPackage Open(FileInfo file, PackageAccess access)
pack.originalPackagePath = file.FullName;
return pack;
}
catch (InvalidFormatException e)
catch (InvalidFormatException)
{
IOUtils.CloseQuietly(pack);
throw;
}
catch (RuntimeException e)
catch (RuntimeException)
{
IOUtils.CloseQuietly(pack);
throw;
Expand Down Expand Up @@ -304,12 +304,12 @@ public static OPCPackage Open(Stream in1)
pack.GetParts();
}
}
catch (InvalidFormatException e)
catch (InvalidFormatException)
{
IOUtils.CloseQuietly(pack);
throw;
}
catch (RuntimeException e)
catch (RuntimeException)
{
IOUtils.CloseQuietly(pack);
throw;
Expand Down Expand Up @@ -456,7 +456,7 @@ public void Close()
{
FileInfo targetFile = new FileInfo(this.originalPackagePath);
if (!File.Exists(this.originalPackagePath)|| !(this.originalPackagePath
.Equals(targetFile.FullName, StringComparison.InvariantCultureIgnoreCase))) {
.Equals(targetFile.FullName, StringComparison.InvariantCultureIgnoreCase))) {

// Case of a package Created from scratch
Save(originalPackagePath);
Expand Down Expand Up @@ -589,7 +589,7 @@ internal void ThrowExceptionIfReadOnly()
* (PackageAccess.Write). This method is call when other methods need write
* right.
*
* @throws InvalidOperationException if a read operation is done on a write only package.
* @throws InvalidOperationException if a read operation is done on a write only package.
* @see org.apache.poi.OpenXml4Net.opc.PackageAccess
*/
internal void ThrowExceptionIfWriteOnly()
Expand Down
7 changes: 6 additions & 1 deletion testcases/main/POIFS/FileSystem/TestNPOIFSFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,15 @@ protected static NPOIFSFileSystem WriteOutFileAndReadBack(NPOIFSFileSystem origi
{
FileInfo file = TempFile.CreateTempFile("TestPOIFS", ".ole2");
using (FileStream fout = file.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite))
try
{
original.WriteFileSystem(fout);
original.Close();
}
finally
{
fout.Close();

This comment has been minimized.

Copy link
@Bykiev

Bykiev Mar 12, 2024

Collaborator

Do we need to call Close explicitly, because it was already managed by using statement?

}
original.Close();
return new NPOIFSFileSystem(file, false);
}

Expand Down

0 comments on commit 2127eb6

Please sign in to comment.