Cramfs: "Data Error" on files with holes (mkfs.cramfs -z)

Open Beginner friendly
#311 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
backend

Research direction

Start with CramfsHandler.cpp and inspect CHandler::ReadBlock, where the issue says zero-length compressed blocks are passed to zlib. Compare the existing zero-block handling in SquashfsHandler.cpp and verify how CRAMFS_FLAG_HOLES represents holes. Done means 7-Zip extracts the reported sparse file correctly, including zero-filled blocks; the payload does not name a test file or command for the project suite.

Written by the indexing model from the issue text.

Description

7-Zip 26.06 (x64, Linux) fails on cramfs images made with mkfs.cramfs -z (CRAMFS_FLAG_HOLES) when a file has all-zero pages:

mkdir -p src
{ printf start; head -c 20480 /dev/zero; printf end; } > src/sparse.bin
mkfs.cramfs -z src holes.img
7z x holes.img -oout

Result: ERROR: Data Error : sparse.bin, and the output file stops at 4096 bytes (same on Windows 11 with 7z2606-x64). Without -z the same tree extracts fine. Same result for big-endian images (-N big). The Linux kernel mounts holes.img and reads the file correctly.

With -z, a hole is stored as a block of compressed length 0 (block pointer equal to the previous one). The Linux driver reads it as a page of zeros (fs/cramfs/inode.c, if (block_len == 0) ; /* hole */ then memset). CHandler::ReadBlock in CramfsHandler.cpp passes the empty block to the zlib decoder, which fails. SquashfsHandler.cpp already handles the same case in its ReadBlock (if (packBlockSize == 0) memset(dest, 0, blockSize)).

This change in CHandler::ReadBlock fixes it for me (extracted files are identical to the originals, also for all-zero files and when the first or last block is a hole):

   const UInt32 inSize = end - start;
 
+  if (inSize == 0)
+  {
+    // hole (CRAMFS_FLAG_HOLES): Linux reads a zero-length block as zeros
+    memset(dest, 0, blockSize);
+    return S_OK;
+  }
+
   if (_method == k_Flags_Method_LZMA)
Dominant language
C++
Stars
4.6k
Forks
519
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ip7z/7zip

All issues in ip7z/7zip

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.