Bug 437778

Summary: KArchiveFile data() returns less data then size()
Product: [Frameworks and Libraries] frameworks-karchive Reporter: kpopv <koen.poppe>
Component: generalAssignee: David Faure <faure>
Status: RESOLVED NOT A BUG    
Severity: normal CC: a.samirh78, aacid, kdelibs-bugs-null
Priority: NOR    
Version First Reported In: 5.73.0   
Target Milestone: ---   
Platform: Other   
OS: Other   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description kpopv 2021-05-28 13:26:55 UTC
SUMMARY
data() and size() aren't consistent: the data() contains less bytes than indicated by size() for file 1 in this archive.

CODE TO REPRODUCE

KZip zip("datalessthansize.zip"); // see below
zip.open(QIODevice::ReadOnly);
auto dir = zip.directory();
auto dir1 = dir->entry("dir1");

const KArchiveFile *file1 = dir1->file("file1");
const QByteArray data1 = file1->data();
qDebug() << "file1" << file1->position() << file1->size() << data1.size();

const KArchiveFile *file2 = dir1->file("file2");
const QByteArray data2 = file2->data();
qDebug() << "file2" << file2->position() << file2->size() << data2.size();


OBSERVED RESULT
      position()   size()   data().size() 
file1         68  5757690   <=    5750784 ???
file2    5758706   566784          566784


EXPECTED RESULT
      position()   size()   data().size() 
file1         68  5757690         5757690
file2    5758706   566784          566784


SOFTWARE/OS VERSIONS
Linux 5.4.90-intel-pk-standard x86_64
Qt Version: 5.15

ADDITIONAL INFORMATION
Not seen with 5.30 on x86 platform.

Creating the test archive (6MB is too much for an attachment)

mkdir dir1
head -c 5757690 < /dev/urandom > dir1/file1
head -c  566784 < /dev/urandom > dir1/file2
zip datalessthansize.zip dir1/file1 dir1/file2
Comment 1 Ahmad Samir 2021-06-03 11:26:05 UTC
There is even an assert in KArchiveFile::data():
Q_ASSERT(arr.size() == d->size);

I am not sure if this is normal behaviour with the zip spec, or a bug on our side (I don't see how, but... ).
Comment 2 Albert Astals Cid 2021-06-03 20:10:24 UTC
Works fine for me in KArchive 5.82 (well when i made your code compile)
auto dir1 = dir->entry("dir1");
   needs to be
auto dir1 = dynamic_cast<const KArchiveDirectory*>(dir->entry("dir1"));

Can you please try in something newer?
Comment 3 kpopv 2021-06-07 08:55:12 UTC
Apologies for the code error, obviously, that cast is in my code. 

I've tried the test again with libKF5Archive.so.5.82.0 and it still trigger this bug. 

Any suggestion on how to diagnose this further? Versions of libz?
Comment 4 Albert Astals Cid 2021-06-07 17:54:03 UTC
Honestly besides you debugging it yourself or providing a 100% scenario where we can reproduce it (docker recipe?) I am not sure what to suggest.
Comment 5 kpopv 2021-06-08 12:52:37 UTC
As this KArchive version is built through a Yocto meta layer, I also asked for assistence there:

https://github.com/schnitzeltony/meta-qt5-extra/issues/86

I've included a minimal test case which does not depend on randomness. If you would be able to confirm this test runs correctly with a standalone build of KArchive, that would be helpful in determining whether the issue resides in KArchive or the specific configuration inside the Yocto build.
Comment 6 Albert Astals Cid 2021-06-08 19:28:33 UTC
tsdgeos@xps:~/test:$ ./test 
********* Start testing of KArchiveTest *********
Config: Using QtTest library 5.15.2, Qt 5.15.2 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 11.1.0), arch unknown
PASS   : KArchiveTest::initTestCase()
PASS   : KArchiveTest::sizeVsDataDiscrepancy()
PASS   : KArchiveTest::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted, 75ms
********* Finished testing of KArchiveTest *********
Comment 7 kpopv 2021-06-09 06:10:13 UTC
Thank you for the confirmation, I'll chase it further on the Yocto meta layer issue and keep you posted.
Comment 8 kpopv 2021-06-21 14:33:14 UTC
As we've verified https://github.com/jtkukunas/zlib/issues/33 is the root cause, this issue can be closed. Thank you for the support!
Comment 9 Ahmad Samir 2021-06-21 15:04:34 UTC
Thanks for the feedback o/