From 6aa28616ffa6be8d8475134e5a387a04bd4770b2 Mon Sep 17 00:00:00 2001 From: "dbaron%dbaron.org" Date: Thu, 25 Jan 2007 23:05:22 +0000 Subject: [PATCH] Don't close the same file descriptors twice. b=367817 r+sr=darin --- modules/libjar/nsJAR.cpp | 12 +++++++++--- modules/libjar/nsJARInputStream.cpp | 6 +++--- modules/libjar/nsJARInputStream.h | 2 ++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/modules/libjar/nsJAR.cpp b/modules/libjar/nsJAR.cpp index edfe08dfb78..a8b1127da26 100644 --- a/modules/libjar/nsJAR.cpp +++ b/modules/libjar/nsJAR.cpp @@ -326,7 +326,6 @@ nsJAR::GetInputStreamWithSpec(const nsACString& aJarDirSpec, NS_ENSURE_ARG_POINTER(result); // Watch out for the jar:foo.zip!/ (aDir is empty) top-level special case! - PRFileDesc *fd = nsnull; nsZipItem *item = nsnull; if (*aEntryName) { // First check if item exists in jar @@ -343,12 +342,19 @@ nsJAR::GetInputStreamWithSpec(const nsACString& aJarDirSpec, rv = jis->InitDirectory(&mZip, aJarDirSpec, aEntryName); } else { // Open jarfile, to get its own filedescriptor for the stream + // XXX The file may have been overwritten, so |item| might not be + // valid. We really want to work from inode rather than file name. + PRFileDesc *fd = nsnull; fd = OpenFile(); - rv = fd ? jis->InitFile(&mZip, item, fd) : NS_ERROR_FAILURE; + if (fd) { + rv = jis->InitFile(&mZip, item, fd); + // |jis| now owns |fd| + } else { + rv = NS_ERROR_FAILURE; + } } if (NS_FAILED(rv)) { NS_RELEASE(*result); - if (fd) PR_Close(fd); } return rv; } diff --git a/modules/libjar/nsJARInputStream.cpp b/modules/libjar/nsJARInputStream.cpp index 9c6aab5ee4c..b587c97139d 100644 --- a/modules/libjar/nsJARInputStream.cpp +++ b/modules/libjar/nsJARInputStream.cpp @@ -61,6 +61,9 @@ nsJARInputStream::InitFile(nsZipArchive* aZip, nsZipItem *item, PRFileDesc *fd) { nsresult rv; + // Keep the file handle, even on failure + mFd = fd; + NS_ENSURE_ARG_POINTER(aZip); NS_ENSURE_ARG_POINTER(item); NS_ENSURE_ARG_POINTER(fd); @@ -68,9 +71,6 @@ nsJARInputStream::InitFile(nsZipArchive* aZip, nsZipItem *item, PRFileDesc *fd) // Mark it as closed, in case something fails in initialisation mClosed = PR_TRUE; - // Keep the file handle - mFd = fd; - // Keep the important bits of nsZipItem only mInSize = item->size; diff --git a/modules/libjar/nsJARInputStream.h b/modules/libjar/nsJARInputStream.h index 12cbd68f97f..604b0d6555c 100644 --- a/modules/libjar/nsJARInputStream.h +++ b/modules/libjar/nsJARInputStream.h @@ -60,7 +60,9 @@ class nsJARInputStream : public nsIInputStream NS_DECL_ISUPPORTS NS_DECL_NSIINPUTSTREAM + // takes ownership of |fd|, even on failure nsresult InitFile(nsZipArchive* aZip, nsZipItem *item, PRFileDesc *fd); + nsresult InitDirectory(nsZipArchive* aZip, const nsACString& aJarDirSpec, const char* aDir);