Skip to content

cli: fix a heap read with --output-dir-mirror=/ - #5051

Merged
Cyan4973 merged 1 commit into
meta:devfrom
hyc-alt:fix/cli-mirror-path-and-fnt-safety
Dec 4, 2026
Merged

Cyan4973 merged 1 commit into
meta:devfrom
hyc-alt:fix/cli-mirror-path-and-fnt-safety

Conversation

@hyc-alt

@hyc-alt hyc-alt commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What and why

zstd --output-dir-mirror=/ file reads one byte before a heap buffer (AddressSanitizer, in FIO_createFilename_fromOutDir()).
For a top-level file, convertPathnameToDirName() turned "/file" into "", and FIO_createFilename_fromOutDir() then read outDirName[-1].

convertPathnameToDirName() now keeps the root separator, as dirname() does: "/file" and "/" give "/".

Reduced and rebased onto dev by a maintainer: the other changes first proposed here are either already fixed on dev (#5015) or left for a separate PR (see the comment below).

Tests & Verification

  • New tests/cli-tests/file-handling/directory-mirror-root.sh: the input is missing, so nothing is written to /.
    It fails on dev under AddressSanitizer (as in the clang-asan-ubsan-cli-tests CI job), and passes with this fix.
  • The CLI tests pass with gcc, in normal and ASan+UBSan builds. util.c passes a C90 syntax check with gcc and MinGW-w64, and the MinGW-w64 build passes with -Werror.

Checklist

  • I searched open pull requests and issues: this problem is not already being addressed.
  • Bug fix: it comes with a reproducer or a test that fails without the fix.
  • Performance change: numbers from repeated runs are included, with CPU, compiler, flags and data.
    A gain in one configuration is not a general gain: see the Performance section of CONTRIBUTING.md.
  • I stated below whether AI or other automated tools were used.

Tools used

AddressSanitizer, UndefinedBehaviorSanitizer, MinGW-w64 cross-compiler, and an AI coding assistant (used for static code review, patch drafting, and regression test drafting).

@meta-cla meta-cla Bot added the CLA Signed label Nov 26, 2026
@hyc-alt
hyc-alt force-pushed the fix/cli-mirror-path-and-fnt-safety branch from 3827e3d to acc61d5 Compare December 2, 2026 08:25
@hyc-alt hyc-alt changed the title cli: fix --output-dir-mirror heap underflow/abort and FileNamesTable expansion bugs cli: fix --output-dir-mirror root underflow and FileNamesTable expansion bugs Dec 2, 2026
For a top-level file, convertPathnameToDirName() turned "/file" into "",
and FIO_createFilename_fromOutDir() then read outDirName[-1].
Keep the root separator, as dirname() does: "/file" and "/" give "/".
@Cyan4973
Cyan4973 force-pushed the fix/cli-mirror-path-and-fnt-safety branch from acc61d5 to 8032592 Compare December 4, 2026 01:03
@Cyan4973 Cyan4973 changed the title cli: fix --output-dir-mirror root underflow and FileNamesTable expansion bugs cli: fix a heap read with --output-dir-mirror=/ Dec 4, 2026
@Cyan4973

Cyan4973 commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Thanks for the report and the fix.
dev has changed a lot under this PR since it was opened (#5015 rewrote the file-list construction, #5107 added file identities), so I rebased it and reduced it to the part that is still needed:

  • The --output-dir-mirror=/ heap read is still there on dev: AddressSanitizer reports a 1-byte read before the buffer in FIO_createFilename_fromOutDir().
    The fix keeps your approach: convertPathnameToDirName() keeps the root separator, so "/file" gives "/". Test: directory-mirror-root.sh.
  • The FileNamesTable growth, the Win32 leak on ./.. and tableCapacity are fixed on dev by programs: consolidate recursive filename-list construction #5015: your tests 2 to 4 pass on dev unchanged.
  • The size_t overflow checks and the zero-size allocation guards cover sizes that do not occur in practice, so I left them out.
  • Accepting / as a separator on Windows is a real, separate issue.
    It would be better as its own PR, together with enabling the --output-dir-mirror section of playTests.sh on Windows (it is skipped there today), so that CI checks it.

The commit keeps you as its author.

@Cyan4973 Cyan4973 self-assigned this Dec 4, 2026
@hyc-alt

hyc-alt commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for rebasing and narrowing this down, @Cyan4973! The reduced commit looks very clean. I'll follow up with a separate PR for Windows / separator handling together with enabling the --output-dir-mirror section of playTests.sh on Windows.

@Cyan4973
Cyan4973 merged commit 42aeb1f into meta:dev Dec 4, 2026
115 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants