-
Notifications
You must be signed in to change notification settings - Fork 293
Factor our realloc handling to a separate function #344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
zip_realloc.c -- reallocate with additional elements | ||
Copyright (C) 2009-2022 Dieter Baron and Thomas Klausner | ||
|
||
This file is part of libzip, a library to manipulate ZIP archives. | ||
The authors can be contacted at <[email protected]> | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions | ||
are met: | ||
1. Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
2. Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in | ||
the documentation and/or other materials provided with the | ||
distribution. | ||
3. The names of the authors may not be used to endorse or promote | ||
products derived from this software without specific prior | ||
written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS | ||
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY | ||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | ||
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | ||
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | ||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN | ||
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#include <stdlib.h> | ||
|
||
#include "zipint.h" | ||
|
||
bool zip_realloc_implementation(void **inout_memory, zip_uint64_t *inout_alloced, zip_uint64_t element_size, zip_uint64_t additional_elements, zip_error_t *error) { | ||
void *old_memory = *inout_memory; | ||
zip_uint64_t old_alloced = *inout_alloced; | ||
zip_uint64_t new_alloced; | ||
size_t new_size; | ||
void *new_memory; | ||
|
||
if (additional_elements == 0) { | ||
return true; | ||
} | ||
|
||
if (old_alloced + additional_elements < old_alloced) { | ||
zip_error_set(error, ZIP_ER_MEMORY, 0); | ||
return false; | ||
} | ||
|
||
new_alloced = old_alloced + additional_elements; | ||
|
||
if (new_alloced > SIZE_MAX / element_size) { | ||
zip_error_set(error, ZIP_ER_MEMORY, 0); | ||
return false; | ||
} | ||
|
||
/* Cast explicitly to size_t to quiet msvc complaints about | ||
possible loss of data. At this point we know that the result of | ||
"new_alloced * element_size" should fit into size_t. */ | ||
new_size = (size_t)(new_alloced * element_size); | ||
|
||
if ((new_memory = realloc(old_memory, new_size)) == NULL) { | ||
zip_error_set(error, ZIP_ER_MEMORY, 0); | ||
return false; | ||
} | ||
|
||
*inout_memory = new_memory; | ||
*inout_alloced = new_alloced; | ||
|
||
return true; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -291,8 +291,8 @@ struct zip { | |
zip_uint64_t nentry_alloc; /* number of entries allocated */ | ||
zip_entry_t *entry; /* entries */ | ||
|
||
unsigned int nopen_source; /* number of open sources using archive */ | ||
unsigned int nopen_source_alloc; /* number of sources allocated */ | ||
zip_uint64_t nopen_source; /* number of open sources using archive */ | ||
zip_uint64_t nopen_source_alloc; /* number of sources allocated */ | ||
zip_source_t **open_source; /* open sources using archive */ | ||
|
||
zip_hash_t *names; /* hash table for name lookup */ | ||
|
@@ -486,6 +486,8 @@ typedef struct _zip_pkware_keys zip_pkware_keys_t; | |
#endif | ||
#endif | ||
|
||
#define zip_realloc(inout_memory, inout_alloced, element_size, additional_elements, error) zip_realloc_implementation((void **)inout_memory, inout_alloced, element_size, additional_elements, error) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking about dropping the |
||
bool zip_realloc_implementation(void **inout_memory, zip_uint64_t *inout_alloced, zip_uint64_t element_size, zip_uint64_t additional_elements, zip_error_t *error); | ||
|
||
zip_int64_t _zip_add_entry(zip_t *); | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.