Skip to content

fix(scan): keep NUL-delimited pathnames from git ls-files intact - #1497

Merged
lizhengfeng101 merged 2 commits into
alibaba:mainfrom
basil-k-aji-dev:fix/scan-ls-files-nul-paths
Sep 21, 2026
Merged

lizhengfeng101 merged 2 commits into
alibaba:mainfrom
basil-k-aji-dev:fix/scan-ls-files-nul-paths

Conversation

@basil-k-aji-dev

Copy link
Copy Markdown
Contributor

Description

gitLs asks git for NUL-delimited paths and then runs strings.TrimSpace over every record. Leading and trailing spaces are filename bytes, not record framing — -z exists precisely so that they need no escaping. A tracked normal.go became normal.go, the later Lstat found nothing, and the file left the scan on a warning. A full scan reports success having reviewed one file fewer than the repository holds.

While fixing that I found a second instance of the same defect in the same function. gitLs takes stdout only on its p.runner == nil branch, with a comment explaining why it must:

// Use Output (stdout only), not CombinedOutput: with -z, git emits
// NUL-delimited paths on stdout, and merging stderr in would corrupt
// the filename parsing below.

The other branch — the one every real ocr scan takes, since agent.go:319 passes a.args.GitRunner — called Runner.Run, which is CombinedOutput. Runner.Output already existed and is now used instead.

That one is worse than it sounds. With -z there is no line structure to resynchronise on, so a warning git writes while still exiting 0 does not arrive as a stray record: it is glued to the front of the first real pathname. In the test fixture, one unreadable directory costs the scan every file, not one.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

How Has This Been Tested?

  • make test passes locally
  • Manual testing (described below)

Two new tests in internal/scan/provider_paths_test.go, both verified to fail without the patch:

test without the patch
TestProvider_Enumerate_PreservesWhitespaceInTrackedPaths enumerated ["plain.go"], want [" leading.go" "plain.go" "trailing.go "]
TestProvider_Enumerate_StderrWarningIsNotParsedAsAPath enumerated [], want exactly ["kept.go"]

The first runs against a real repository through both the nil and injected-Runner paths. The second makes git ls-files --others warn and still exit 0 by way of a mode-0000 directory, which is the shape that matters — on a non-zero exit gitLs returns the error and parses nothing.

Both skip on Windows, which normalises away the filenames the fixtures depend on; the second also skips as root, who reads a 0000 directory regardless of its mode. The parsing under test is platform-independent.

One caveat on my local run: make test uses -race, and this machine's kernel has a 47-bit VMA where ThreadSanitizer needs 48, so every package aborts before running. I ran the suite as go test -count=1 ./..., which is green, plus make check and make english-check. Race coverage on these changes is therefore untested on my side and rests on CI.

Checklist

  • My code follows the project's coding style (go fmt, go vet)
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly (if applicable) — no user-facing surface changes
  • I have signed the CLA
  • I did not use AI/LLM to create this PR, or I disclosed the tool/model below and reviewed its output; I did not attribute commits to AI and will answer maintainer questions and review comments myself without AI/LLM.

AI/LLM disclosure: written with AI assistance — Claude Code, model Claude Opus 5. I reviewed every line, and each of the two defects is backed by a test I ran against unpatched main to confirm it fails there first. The reasoning above and in any review thread is my own.

Related Issues

Closes #1493

gitLs asks git for NUL-delimited paths and then applies TrimSpace to every
record. Leading and trailing spaces are filename bytes, not record framing:
" normal.go" became "normal.go", the later Lstat found nothing, and the
tracked file left the scan with a warning and no error. A full scan reports
success having reviewed one file fewer than the repository holds.

The same function also took stdout and stderr combined on the branch every
real scan uses, while a comment on the other branch explained why it must not.
With -z there is no line structure to resynchronise on, so a warning that git
writes while still exiting 0 -- an unreadable directory, for instance -- is
glued to the front of the first pathname rather than arriving as a stray
record, and that file is renamed out of the scan too.

Fixes alibaba#1493
@github-actions

Copy link
Copy Markdown
Contributor

✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s).

…orced

os.Geteuid cannot identify root on Windows, where it returns -1, and a
filesystem mounted without permission support ignores the mode outright. In
both cases git reads the directory happily and writes no warning, so the test
asserted against a stream that had nothing wrong with it. Reading the directory
back answers that for every platform at once.

Found by @Qiyuanqiii on alibaba#1490, where the same assumption failed on Windows CI.

@lizhengfeng101 lizhengfeng101 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lizhengfeng101
lizhengfeng101 merged commit 070805f into alibaba:main Sep 21, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(scan): TrimSpace corrupts valid filenames from git ls-files -z

2 participants