Skip to content

Commit

Permalink
Merge pull request #191 from ctabin/fix-zero-file-read
Browse files Browse the repository at this point in the history
Fixes read of zero-file length
  • Loading branch information
ctabin authored Apr 29, 2023
2 parents c65d0b0 + 854fb7a commit 8b7a1fc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/libzippp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ int ZipArchive::readEntry(const ZipEntry& zipEntry, std::function<bool(const voi
char* data = NEW_CHAR_ARRAY(maxSize)
if (data!=nullptr) {
libzippp_int64 result = zip_fread(zipFile, data, maxSize);
if (result>0) {
if (result>=0) {
if (result != static_cast<libzippp_int64>(maxSize)) {
iRes = LIBZIPPP_ERROR_OWRITE_INDEX_FAILURE;
} else if (!writeFunc(data, maxSize)) {
Expand All @@ -926,7 +926,7 @@ int ZipArchive::readEntry(const ZipEntry& zipEntry, std::function<bool(const voi
string::size_type nbChunks = maxSize/chunksize;
for (libzippp_uint32 uiChunk=0 ; uiChunk<nbChunks ; ++uiChunk) {
result = zip_fread(zipFile, data, chunksize);
if (result>0) {
if (result>=0) {
if (result!=static_cast<libzippp_int64>(chunksize)) {
iRes = LIBZIPPP_ERROR_OWRITE_INDEX_FAILURE;
break;
Expand All @@ -952,7 +952,7 @@ int ZipArchive::readEntry(const ZipEntry& zipEntry, std::function<bool(const voi
char* data = NEW_CHAR_ARRAY(leftOver);
if (data!=nullptr) {
result = zip_fread(zipFile, data, leftOver);
if (result>0) {
if (result>=0) {
if (result!=static_cast<libzippp_int64>(leftOver)) {
iRes = LIBZIPPP_ERROR_OWRITE_INDEX_FAILURE;
} else {
Expand Down
15 changes: 14 additions & 1 deletion tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,12 +566,14 @@ void test19() {
ZipArchive z1("test.zip");
z1.open(ZipArchive::Write);
z1.addData("somedata", txtFile, len);
z1.addData("emptydata", "", 0);
z1.close();

ZipArchive z2("test.zip");
z2.open(ZipArchive::ReadOnly);
assert(z2.getNbEntries()==1);
assert(z2.getNbEntries()==2);
assert(z2.hasEntry("somedata"));
assert(z2.hasEntry("emptydata"));

ZipEntry entry = z2.getEntry("somedata");
assert(!entry.isNull());
Expand All @@ -591,6 +593,17 @@ void test19() {
assert(clen3==len);
assert(strncmp("this is some data", data3.c_str(), len)==0);

ZipEntry entry2 = z2.getEntry("emptydata");
assert(!entry2.isNull());

std::ofstream file;
file.open("empty", std::ios_base::out | std::ios_base::binary);
int ret = z2.readEntry(entry2, file);
assert(ret == LIBZIPPP_OK);
assert(file.tellp() == 0);
file.close();
remove("empty");

z2.close();
z2.unlink();

Expand Down

0 comments on commit 8b7a1fc

Please sign in to comment.