vision icon indicating copy to clipboard operation
vision copied to clipboard

extract_archive and _decompress remove every compression suffix from the path, overwriting other files

Open mottopanikeiku opened this issue 1 month ago • 1 comments

🐛 Describe the bug

extract_archive() removes every occurrence of the compression suffix from a file name, not just the last one. Extracting sample.gz.backup.gz writes sample.backup instead of sample.gz.backup, and silently overwrites an existing sample.backup. _decompress(), which PCAM uses for its downloads, applies the replacement to the whole path, so a parent directory like data.gz/ is changed too.

import gzip
from pathlib import Path
from tempfile import TemporaryDirectory

from torchvision.datasets.utils import _decompress, extract_archive

for extract in (extract_archive, _decompress):
    with TemporaryDirectory() as directory:
        root = Path(directory)
        source_dir = root / "data.gz"
        source_dir.mkdir()
        (root / "data").mkdir()
        source = source_dir / "sample.gz.backup.gz"
        expected = source_dir / "sample.gz.backup"
        wrong = (source_dir if extract is extract_archive else root / "data") / "sample.backup"
        wrong.write_bytes(b"KEEP ME")
        with gzip.open(source, "wb") as stream:
            stream.write(b"EXTRACTED")
        actual = Path(extract(source))
        print(extract.__name__)
        print("  output:", actual.relative_to(root))
        print("  expected exists:", expected.exists())
        print("  unrelated file:", wrong.read_bytes())
extract_archive
  output: data.gz/sample.backup
  expected exists: False
  unrelated file: b'EXTRACTED'
_decompress
  output: data/sample.backup
  expected exists: False
  unrelated file: b'EXTRACTED'

Expected: data.gz/sample.gz.backup in both cases, and sample.backup left untouched.

Both call sites use str.replace(suffix, ...); removing only the final suffix fixes it. I have that fix with regression tests (gzip, bzip2 and xz; str and pathlib inputs) on my fork: https://github.com/pytorch/vision/compare/main...mottopanikeiku:vision:fix-decompress-final-suffix. Opening a PR is limited to collaborators right now, so I'm leaving it here in case it's useful.

I used AI assistance (Claude and GPT-6 Astra) to find, fix and test this bug.

Versions

torchvision main (268ddfce) Python sources on the 0.29.1+cpu wheel; torch 2.14.1+cpu; Python 3.12.13; Linux.

mottopanikeiku avatar Oct 17 '26 05:10 mottopanikeiku

for reference: the fix linked above has been on my fork since i filed this (https://github.com/mottopanikeiku/vision/commit/113ff7e005106a2e1603d5d5c3cefc7bf5c55e40, oct 17): removesuffix at both call sites, plus test_decompress_preserves_path for both functions with str and pathlib inputs and gzip, bzip2 and xz. its 12 cases fail on main and pass with the fix, and test/test_datasets_utils.py passes (69 passed). i still can't open it as a pr: github says my account "does not have the correct permissions to execute CreatePullRequest". #9721 makes the same change.

@NicolasHug if it helps, the commit can be cherry-picked as is, or i'm happy to open the pr if pr creation can be enabled for my account.

mottopanikeiku avatar Oct 24 '26 19:10 mottopanikeiku