Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<? try-input redirection #10387

Closed
wants to merge 2 commits into from
Closed

Commits on Mar 21, 2024

  1. Add <? input redirection

    This tries to open the given file to use as stdin, and if it fails,
    for any reason, it uses /dev/null instead.
    
    This is useful in cases where we would otherwise do either of these:
    
    ```fish
    test -r /path/to/file
    and string match foo < /path/to/file
    
    cat /path/to/file 2>/dev/null | string match foo
    ```
    
    This both makes it nicer and shorter, *and* helps with TOCTTOU - what if the file is removed/changed after the check?
    
    The reason for reading /dev/null instead of a closed fd is that a closed fd will often cause an error.
    
    In case opening /dev/null fails, it still skips the command.
    That's really a last resort for when the operating system
    has turned out to be a platypus and not a unix.
    
    Fixes fish-shell#4865
    faho committed Mar 21, 2024
    Copy the full SHA
    df8b9b7 View commit details
    Browse the repository at this point in the history
  2. share/: Use <? redirection

    This is mostly used instead of `test -r foo && string match bar <foo`
    or `set bar (cat foo 2>/dev/null)`.
    
    In doing so it can avoid a weird race condition where the file changes
    between the check and the use, and it can avoid an external process or
    having to open the file twice.
    
    However it can also cause more work to happen - if you do
    
    ```fish
    if test -r file
       # do a lot of work with file
    end
    ```
    
    it might be prudent to keep doing that. In the cases here it's usually
    just a few `string` calls, which would operate on nothing and so be
    pretty quick.
    faho committed Mar 21, 2024
    Copy the full SHA
    de1e9ed View commit details
    Browse the repository at this point in the history