Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(425)

Side by Side Diff: third_party/zlib/google/zip_internal.cc

Issue 683913009: Eliminate resource leaks from zip::ZipFiles and (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rename file closer, add comment, reorder ZipReader/FileWrapper, change test iteration count Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/zlib/google/zip.h ('k') | third_party/zlib/google/zip_reader.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "third_party/zlib/google/zip_internal.h" 5 #include "third_party/zlib/google/zip_internal.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 FILE* file = NULL; 83 FILE* file = NULL;
84 const char* mode_fopen = NULL; 84 const char* mode_fopen = NULL;
85 85
86 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ) 86 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
87 mode_fopen = "rb"; 87 mode_fopen = "rb";
88 else if (mode & ZLIB_FILEFUNC_MODE_EXISTING) 88 else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
89 mode_fopen = "r+b"; 89 mode_fopen = "r+b";
90 else if (mode & ZLIB_FILEFUNC_MODE_CREATE) 90 else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
91 mode_fopen = "wb"; 91 mode_fopen = "wb";
92 92
93 if ((filename != NULL) && (mode_fopen != NULL)) 93 if ((filename != NULL) && (mode_fopen != NULL)) {
94 file = fdopen(*static_cast<int*>(opaque), mode_fopen); 94 int fd = dup(*static_cast<int*>(opaque));
95 if (fd != -1)
96 file = fdopen(fd, mode_fopen);
97 }
95 98
96 return file; 99 return file;
97 } 100 }
98 101
99 // We don't actually close the file stream since that would close
100 // the underlying file descriptor, and we don't own it. However we do need to
101 // flush buffers and free |opaque| since we malloc'ed it in FillFdOpenFileFunc.
102 int CloseFileFunc(void* opaque, void* stream) { 102 int CloseFileFunc(void* opaque, void* stream) {
103 fflush(static_cast<FILE*>(stream)); 103 fclose(static_cast<FILE*>(stream));
104 free(opaque); 104 free(opaque);
105 return 0; 105 return 0;
106 } 106 }
107 107
108 // Fills |pzlib_filecunc_def| appropriately to handle the zip file 108 // Fills |pzlib_filecunc_def| appropriately to handle the zip file
109 // referred to by |fd|. 109 // referred to by |fd|.
110 void FillFdOpenFileFunc(zlib_filefunc_def* pzlib_filefunc_def, int fd) { 110 void FillFdOpenFileFunc(zlib_filefunc_def* pzlib_filefunc_def, int fd) {
111 fill_fopen_filefunc(pzlib_filefunc_def); 111 fill_fopen_filefunc(pzlib_filefunc_def);
112 pzlib_filefunc_def->zopen_file = FdOpenFileFunc; 112 pzlib_filefunc_def->zopen_file = FdOpenFileFunc;
113 pzlib_filefunc_def->zclose_file = CloseFileFunc; 113 pzlib_filefunc_def->zclose_file = CloseFileFunc;
(...skipping 10 matching lines...) Expand all
124 file_ret.hf = static_cast<HANDLE>(opaque); 124 file_ret.hf = static_cast<HANDLE>(opaque);
125 file_ret.error = 0; 125 file_ret.error = 0;
126 if (file_ret.hf == INVALID_HANDLE_VALUE) 126 if (file_ret.hf == INVALID_HANDLE_VALUE)
127 return NULL; 127 return NULL;
128 128
129 void* ret = malloc(sizeof(WIN32FILE_IOWIN)); 129 void* ret = malloc(sizeof(WIN32FILE_IOWIN));
130 if (ret != NULL) 130 if (ret != NULL)
131 *(static_cast<WIN32FILE_IOWIN*>(ret)) = file_ret; 131 *(static_cast<WIN32FILE_IOWIN*>(ret)) = file_ret;
132 return ret; 132 return ret;
133 } 133 }
134
135 int HandleCloseFileFunc(void* opaque, void* stream) {
James Hawkins 2014/11/17 23:10:38 Can we provide better clarity about what the diffe
jeremyspiegel 2014/11/17 23:13:50 Would renaming CloseFileFunc to FdCloseFileFunc he
James Hawkins 2014/11/17 23:28:39 Yeah, I think so. It'd also be good to document t
136 free(stream); // malloc'ed in HandleOpenFileFunc()
137 return 0;
138 }
134 #endif 139 #endif
135 140
136 // A struct that contains data required for zlib functions to extract files from 141 // A struct that contains data required for zlib functions to extract files from
137 // a zip archive stored in memory directly. The following I/O API functions 142 // a zip archive stored in memory directly. The following I/O API functions
138 // expect their opaque parameters refer to this struct. 143 // expect their opaque parameters refer to this struct.
139 struct ZipBuffer { 144 struct ZipBuffer {
140 const char* data; // weak 145 const char* data; // weak
141 size_t length; 146 size_t length;
142 size_t offset; 147 size_t offset;
143 }; 148 };
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 // Passing dummy "fd" filename to zlib. 280 // Passing dummy "fd" filename to zlib.
276 return unzOpen2("fd", &zip_funcs); 281 return unzOpen2("fd", &zip_funcs);
277 } 282 }
278 #endif 283 #endif
279 284
280 #if defined(OS_WIN) 285 #if defined(OS_WIN)
281 unzFile OpenHandleForUnzipping(HANDLE zip_handle) { 286 unzFile OpenHandleForUnzipping(HANDLE zip_handle) {
282 zlib_filefunc_def zip_funcs; 287 zlib_filefunc_def zip_funcs;
283 fill_win32_filefunc(&zip_funcs); 288 fill_win32_filefunc(&zip_funcs);
284 zip_funcs.zopen_file = HandleOpenFileFunc; 289 zip_funcs.zopen_file = HandleOpenFileFunc;
290 zip_funcs.zclose_file = HandleCloseFileFunc;
285 zip_funcs.opaque = zip_handle; 291 zip_funcs.opaque = zip_handle;
286 return unzOpen2("fd", &zip_funcs); 292 return unzOpen2("fd", &zip_funcs);
287 } 293 }
288 #endif 294 #endif
289 295
290 // static 296 // static
291 unzFile PrepareMemoryForUnzipping(const std::string& data) { 297 unzFile PrepareMemoryForUnzipping(const std::string& data) {
292 if (data.empty()) 298 if (data.empty())
293 return NULL; 299 return NULL;
294 300
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 0, // versionMadeBy 375 0, // versionMadeBy
370 LANGUAGE_ENCODING_FLAG)) { // flagBase 376 LANGUAGE_ENCODING_FLAG)) { // flagBase
371 DLOG(ERROR) << "Could not open zip file entry " << str_path; 377 DLOG(ERROR) << "Could not open zip file entry " << str_path;
372 return false; 378 return false;
373 } 379 }
374 return true; 380 return true;
375 } 381 }
376 382
377 } // namespace internal 383 } // namespace internal
378 } // namespace zip 384 } // namespace zip
OLDNEW
« no previous file with comments | « third_party/zlib/google/zip.h ('k') | third_party/zlib/google/zip_reader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698