Skip to content

dirname/basename fast path: leading // and empty operand mishandled (string edge cases) #709

Closed

Description

@caomengxuan666

Summary

The inline dirname/basename fast paths (src/executor/printf_path_builtins.rs)
implement pure POSIX string semantics by hand and diverge on two edge cases:
a leading // must be preserved, and an empty operand must yield . /
the empty string. These are independent of the /a/b -> A: translation tracked
in niubash#346 (fixed by rubash#649 + niubash#371) and remain in
dirname_value/basename_value on origin/master.

Classification: string edge case.

Minimal repro

$ niu -c "dirname //"                     # -> "/"
$ niu -c "dirname //a"                    # -> "/"
$ <install>/winuxcmd/usr/bin/dirname.exe //   # -> "//"
$ D:/Git/bin/bash.exe -c "dirname //"         # -> "//"

$ niu -c "dirname ''"                     # -> "/"
$ <install>/winuxcmd/usr/bin/dirname.exe ''   # -> "."
$ D:/Git/bin/bash.exe -c "dirname ''"         # -> "."

$ niu -c "basename ''"                    # -> "/"
$ <install>/winuxcmd/usr/bin/basename.exe ''  # -> "" (empty line)
$ D:/Git/bin/bash.exe -c "basename ''"        # -> "" (empty line)
repro niu fast external / GNU
dirname // / //
dirname //a / //
dirname '' / .
basename '' / `` (empty)

Cause: trim_trailing_slashes maps an all-slash value to the empty string and
dirname_value/basename_value then return /; an empty operand hits the same
fallback.

Not the same as

  • niubash#346 item (c): dirname /a/b -> A: — operand pre-translation, fixed by
    rubash#649; reproduces only on rubash e785c3c8 and is not re-filed here.
  • basename // returns / in the fast path and WinuxCmd (GNU //): a
    shared divergence, recorded in the audit but not part of this bug.

Found by the fast-path differential audit (#706).

Activity

  1. added
    bug
    area:fastpath
    on Oct 17, 2026
  2. caomengxuan666 commented on Oct 17, 2026

    @caomengxuan666
    MemberAuthor

    已在 d4f0b67(PR #718)修掉,只是 issue 没跟着关。当前 master d248d9f 复验:

    repro rubash issue 的 oracle
    dirname // // //
    dirname //a // //
    dirname '' . .
    basename '' (空) (空)
    basename // / /(共享差异,本就不在范围)

    注:沙箱里的 GNU coreutils dirname 对 // 输出 /,POSIX 把 // 留给实现定义,所以这里按 issue 里的 Git Bash oracle 判。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:fastpathbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions